@Override
  public JsonElement serialize(
      Map<GuildLocation, Set<String>> src, Type typeOfSrc, JsonSerializationContext context) {
    JsonObject obj = new JsonObject();

    try {
      if (src != null) {
        GuildLocation loc;
        String locWorld;
        Set<String> nameSet;
        Iterator<String> iter;
        JsonArray nameArray;
        JsonPrimitive nameElement;

        for (Entry<GuildLocation, Set<String>> entry : src.entrySet()) {
          loc = entry.getKey();
          locWorld = loc.getWorldName();
          nameSet = entry.getValue();

          if (nameSet == null || nameSet.isEmpty()) {
            continue;
          }

          nameArray = new JsonArray();
          iter = nameSet.iterator();
          while (iter.hasNext()) {
            nameElement = new JsonPrimitive(iter.next());
            nameArray.add(nameElement);
          }

          if (!obj.has(locWorld)) {
            obj.add(locWorld, new JsonObject());
          }

          obj.get(locWorld).getAsJsonObject().add(loc.getCoordString(), nameArray);
        }
      }
      return obj;

    } catch (Exception ex) {
      ex.printStackTrace();
      GuildsMain.p.log(
          Level.WARNING, "Error encountered while serializing a Map of FLocations to String Sets.");
      return obj;
    }
  }
  @Override
  public Map<GuildLocation, Set<String>> deserialize(
      JsonElement json, Type typeOfT, JsonDeserializationContext context)
      throws JsonParseException {
    try {
      JsonObject obj = json.getAsJsonObject();
      if (obj == null) {
        return null;
      }

      Map<GuildLocation, Set<String>> locationMap =
          new ConcurrentHashMap<GuildLocation, Set<String>>();
      Set<String> nameSet;
      Iterator<JsonElement> iter;
      String worldName;
      String[] coords;
      int x, z;

      for (Entry<String, JsonElement> entry : obj.entrySet()) {
        worldName = entry.getKey();
        for (Entry<String, JsonElement> entry2 : entry.getValue().getAsJsonObject().entrySet()) {
          coords = entry2.getKey().trim().split("[,\\s]+");
          x = Integer.parseInt(coords[0]);
          z = Integer.parseInt(coords[1]);

          nameSet = new HashSet<String>();
          iter = entry2.getValue().getAsJsonArray().iterator();
          while (iter.hasNext()) {
            nameSet.add(iter.next().getAsString());
          }

          locationMap.put(new GuildLocation(worldName, x, z), nameSet);
        }
      }

      return locationMap;

    } catch (Exception ex) {
      ex.printStackTrace();
      GuildsMain.p.log(
          Level.WARNING,
          "Error encountered while deserializing a Map of FLocations to String Sets.");
      return null;
    }
  }
Beispiel #3
0
 /**
  * Gets the definition of a word
  *
  * @param word the word to get
  * @param dictionary the dictionary to lookup with
  * @return the definition of the word
  * @throws IOException if an error occurred whilst reading the definition
  */
 public static Dictionary.Definition getDefinition(String word, Dictionary dictionary)
     throws IOException {
   if (dictionary == Dictionary.URBAN_DICTIONARY) {
     JsonObject obj =
         new JsonParser()
             .parse(readURLToString(dictionary.getFormattedURL(word)))
             .getAsJsonObject();
     if (!obj.get("result_type").getAsString().equals("no_results")
         && !obj.getAsJsonArray("list").isJsonNull())
       return new Dictionary.Definition(
           obj.getAsJsonArray("list").get(0).getAsJsonObject().get("definition").getAsString(),
           shortenURL("http://www.urbandictionary.com/define.php?term=" + word));
   } else if (dictionary == Dictionary.DUCK_DUCK_GO) {
     JsonObject obj =
         new JsonParser()
             .parse(readURLToString(dictionary.getFormattedURL(word)))
             .getAsJsonObject();
     if (!obj.get("AbstractText").getAsString().equals(""))
       return new Dictionary.Definition(
           obj.get("AbstractText").getAsString(),
           shortenURL("http://www.thefreedictionary.com/" + word));
   }
   return null;
 }