Example #1
0
  /**
   * Write the contents of the JSONArray as JSON text to a writer. For compactness, no whitespace is
   * added.
   *
   * <p>Warning: This method assumes that the data structure is acyclical.
   *
   * @param indentFactor The number of spaces to add to each level of indentation.
   * @param indent The indention of the top level.
   * @return The writer.
   * @throws JSONException
   */
  Writer write(Writer writer, int indentFactor, int indent) throws JSONException {
    try {
      boolean commanate = false;
      int length = this.length();
      writer.write('[');

      if (length == 1) {
        JSONObject.writeValue(writer, this.myArrayList.get(0), indentFactor, indent);
      } else if (length != 0) {
        final int newindent = indent + indentFactor;

        for (int i = 0; i < length; i += 1) {
          if (commanate) {
            writer.write(',');
          }
          if (indentFactor > 0) {
            writer.write('\n');
          }
          JSONObject.indent(writer, newindent);
          JSONObject.writeValue(writer, this.myArrayList.get(i), indentFactor, newindent);
          commanate = true;
        }
        if (indentFactor > 0) {
          writer.write('\n');
        }
        JSONObject.indent(writer, indent);
      }
      writer.write(']');
      return writer;
    } catch (IOException e) {
      throw new JSONException(e);
    }
  }
Example #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
   */
  Writer write(Writer writer, int indentFactor, int indent) throws JSONException {
    try {
      boolean commanate = false;
      final int length = this.length();
      Iterator keys = this.keys();
      writer.write('{');

      if (length == 1) {
        Object key = keys.next();
        writer.write(quote(key.toString()));
        writer.write(':');
        if (indentFactor > 0) {
          writer.write(' ');
        }
        writeValue(writer, this.map.get(key), indentFactor, indent);
      } else if (length != 0) {
        final int newindent = indent + indentFactor;
        while (keys.hasNext()) {
          Object key = keys.next();
          if (commanate) {
            writer.write(',');
          }
          if (indentFactor > 0) {
            writer.write('\n');
          }
          indent(writer, newindent);
          writer.write(quote(key.toString()));
          writer.write(':');
          if (indentFactor > 0) {
            writer.write(' ');
          }
          writeValue(writer, this.map.get(key), indentFactor, newindent);
          commanate = true;
        }
        if (indentFactor > 0) {
          writer.write('\n');
        }
        indent(writer, indent);
      }
      writer.write('}');
      return writer;
    } catch (IOException exception) {
      throw new JSONException(exception);
    }
  }