예제 #1
0
  /**
   * Write the contents of the JSONObject as JSON text to a writer. For compactness, no whitespace
   * is added.
   *
   * <p>Warning: This method assumes that the data structure is acyclical.
   *
   * @return The writer.
   * @throws JSONException
   */
  public Writer write(Writer writer) throws JSONException {
    try {
      boolean b = false;
      Iterator keys = keys();
      writer.write('{');

      while (keys.hasNext()) {
        if (b) {
          writer.write(',');
        }
        Object k = keys.next();
        writer.write(quote(k.toString()));
        writer.write(':');
        Object v = this.map.get(k);
        if (v instanceof JSONObject) {
          ((JSONObject) v).write(writer);
        } else if (v instanceof JSONArray) {
          ((JSONArray) v).write(writer);
        } else {
          writer.write(valueToString(v));
        }
        b = true;
      }
      writer.write('}');
      return writer;
    } catch (IOException e) {
      throw new JSONException(e);
    }
  }
예제 #2
0
  /**
   * Write the contents of the JSONObject as JSON text to a writer. For compactness, no whitespace
   * is added.
   *
   * <p>Warning: This method assumes that the data structure is acyclical.
   *
   * @return The writer.
   * @throws JSONException
   */
  public Writer write(Writer writer) throws JSONException {
    try {
      boolean commanate = false;
      final Iterator keys = this.keys();
      writer.write('{');

      while (keys.hasNext()) {
        if (commanate) {
          writer.write(',');
        }
        final Object key = keys.next();
        writer.write(JSONObject.quote(key.toString()));
        writer.write(':');
        final Object value = this.map.get(key);
        if (value instanceof JSONObject) {
          ((JSONObject) value).write(writer);
        } else if (value instanceof JSONArray) {
          ((JSONArray) value).write(writer);
        } else {
          writer.write(JSONObject.valueToString(value));
        }
        commanate = true;
      }
      writer.write('}');
      return writer;
    } catch (final IOException exception) {
      throw new JSONException(exception);
    }
  }
예제 #3
0
 static Writer writeValue(Writer writer, Object value) throws JSONException, IOException {
   if (value == null) {
     writer.write("null");
   } else if (value instanceof JSONObject) {
     ((JSONObject) value).write(writer);
   } else if (value instanceof JSONArray) {
     ((JSONArray) value).write(writer);
   } else if (value instanceof Map) {
     Map<?, ?> map = (Map<?, ?>) value;
     new JSONObject(map).write(writer);
   } else if (value instanceof Collection) {
     Collection<?> coll = (Collection<?>) value;
     new JSONArray(coll).write(writer);
   } else if (value.getClass().isArray()) {
     new JSONArray(value).write(writer);
   } else if (value instanceof Number) {
     writer.write(numberToString((Number) value));
   } else if (value instanceof Boolean) {
     writer.write(value.toString());
   } else {
     quote(value.toString(), writer);
   }
   return writer;
 }