Пример #1
0
  /**
   * Convert an object to JSON text.
   *
   * <p>If this object is a Map or a List, and it's also a JSONAware, JSONAware will be considered
   * firstly.
   *
   * <p>DO NOT call this method from toJSONString() of a class that implements both JSONAware and
   * Map or List with "this" as the parameter, use JSONObject.toJSONString(Map) or
   * JSONArray.toJSONString(List) instead.
   *
   * @see org.json.simpleForBukkit.JSONObject#toJSONString(Map)
   * @see org.json.simpleForBukkit.JSONArray#toJSONString(List)
   * @param value
   * @return JSON text, or "null" if value is null or it's an NaN or an INF number.
   */
  @SuppressWarnings("unchecked")
  public static String toJSONString(Object value) {
    if (value == null) return "null";

    if (value instanceof String) return "\"" + escape((String) value) + "\"";

    if (value instanceof Double) {
      if (((Double) value).isInfinite() || ((Double) value).isNaN()) return "null";
      else return value.toString();
    }

    if (value instanceof Float) {
      if (((Float) value).isInfinite() || ((Float) value).isNaN()) return "null";
      else return value.toString();
    }

    if (value instanceof Number) return value.toString();

    if (value instanceof Boolean) return value.toString();

    if ((value instanceof JSONAware)) return ((JSONAware) value).toJSONString();

    if (value instanceof Map<?, ?>) return JSONObject.toJSONString((Map<Object, Object>) value);

    if (value instanceof List<?>) return JSONArray.toJSONString((List<Object>) value);

    if (BukkitStringifier.canHandle(value.getClass())) {
      return toJSONString(BukkitStringifier.handle(value));
    }

    return value.toString();
  }
Пример #2
0
  /**
   * Encode an object into JSON text and write it to out.
   *
   * <p>If this object is a Map or a List, and it's also a JSONStreamAware or a JSONAware,
   * JSONStreamAware or JSONAware will be considered firstly.
   *
   * <p>DO NOT call this method from writeJSONString(Writer) of a class that implements both
   * JSONStreamAware and (Map or List) with "this" as the first parameter, use
   * JSONObject.writeJSONString(Map, Writer) or JSONArray.writeJSONString(List, Writer) instead.
   *
   * @see org.json.simpleForBukkit.JSONObject#writeJSONString(Map, Writer)
   * @see org.json.simpleForBukkit.JSONArray#writeJSONString(List, Writer)
   * @param value
   * @param writer
   */
  @SuppressWarnings("unchecked")
  public static void writeJSONString(Object value, Writer out) throws IOException {
    if (value == null) {
      out.write("null");
      return;
    }

    if (value instanceof String) {
      out.write('\"');
      out.write(escape((String) value));
      out.write('\"');
      return;
    }

    if (value instanceof Double) {
      if (((Double) value).isInfinite() || ((Double) value).isNaN()) out.write("null");
      else out.write(value.toString());
      return;
    }

    if (value instanceof Float) {
      if (((Float) value).isInfinite() || ((Float) value).isNaN()) out.write("null");
      else out.write(value.toString());
      return;
    }

    if (value instanceof Number) {
      out.write(value.toString());
      return;
    }

    if (value instanceof Boolean) {
      out.write(value.toString());
      return;
    }

    if ((value instanceof JSONStreamAware)) {
      ((JSONStreamAware) value).writeJSONString(out);
      return;
    }

    if ((value instanceof JSONAware)) {
      out.write(((JSONAware) value).toJSONString());
      return;
    }

    if (value instanceof Map<?, ?>) {
      JSONObject.writeJSONString((Map<Object, Object>) value, out);
      return;
    }

    if (value instanceof List<?>) {
      JSONArray.writeJSONString((List<Object>) value, out);
      return;
    }

    if (BukkitStringifier.canHandle(value.getClass())) {
      writeJSONString(BukkitStringifier.handle(value), out);
      return;
    }

    out.write(value.toString());
  }