Ejemplo n.º 1
0
 /**
  * Make a prettyprinted JSON text of this JsonArray. 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 a printable, displayable, transmittable representation of the array.
  * @throws JsonException
  */
 String toString(int indentFactor, int indent) {
   int len = length();
   if (len == 0) {
     return "[]";
   }
   int i;
   StringBuilder sb = new StringBuilder("[");
   if (len == 1) {
     sb.append(JsonObject.valueToString(this.myArrayList.get(0), indentFactor, indent));
   } else {
     int newindent = indent + indentFactor;
     sb.append('\n');
     for (i = 0; i < len; i += 1) {
       if (i > 0) {
         sb.append(",\n");
       }
       for (int j = 0; j < newindent; j += 1) {
         sb.append(' ');
       }
       sb.append(JsonObject.valueToString(this.myArrayList.get(i), indentFactor, newindent));
     }
     sb.append('\n');
     for (i = 0; i < indent; i += 1) {
       sb.append(' ');
     }
   }
   sb.append(']');
   return sb.toString();
 }
Ejemplo n.º 2
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.
   *
   * @return The writer.
   * @throws JsonException
   */
  public Writer write(Writer writer) {
    try {
      boolean b = false;
      int len = length();

      writer.write('[');

      for (int i = 0; i < len; i += 1) {
        if (b) {
          writer.write(',');
        }
        Object v = this.myArrayList.get(i);
        if (v instanceof JsonObject) {
          ((JsonObject) v).write(writer);
        } else if (v instanceof JsonArray) {
          ((JsonArray) v).write(writer);
        } else {
          writer.write(JsonObject.valueToString(v));
        }
        b = true;
      }
      writer.write(']');
      return writer;
    } catch (IOException e) {
      throw new JsonException(e);
    }
  }
Ejemplo n.º 3
0
  /**
   * Make a string from the contents of this JsonArray. The <code>separator</code> string is
   * inserted between each element. Warning: This method assumes that the data structure is
   * acyclical.
   *
   * @param separator A string that will be inserted between the elements.
   * @return a string.
   * @throws JsonException If the array contains an invalid number.
   */
  public String join(String separator) {
    int len = length();
    StringBuilder sb = new StringBuilder();

    for (int i = 0; i < len; i += 1) {
      if (i > 0) {
        sb.append(separator);
      }
      sb.append(JsonObject.valueToString(this.myArrayList.get(i)));
    }
    return sb.toString();
  }