예제 #1
0
  /**
   * Encodes {@code value}.
   *
   * @param value a {@link JSONObject}, {@link JSONArray}, String, Boolean, Integer, Long, Double or
   *     null. May not be {@link Double#isNaN() NaNs} or {@link Double#isInfinite() infinities}.
   * @return this stringer.
   */
  public JSONStringer value(Object value) throws JSONException {
    if (stack.isEmpty()) {
      throw new JSONException("Nesting problem");
    }

    if (value instanceof JSONArray) {
      ((JSONArray) value).writeTo(this);
      return this;

    } else if (value instanceof JSONObject) {
      ((JSONObject) value).writeTo(this);
      return this;
    }

    beforeValue();

    if (value == null || value instanceof Boolean || value == JSONObject.NULL) {
      out.append(value);

    } else if (value instanceof Number) {
      out.append(JSONObject.numberToString((Number) value));

    } else {
      string(value.toString());
    }

    return this;
  }
예제 #2
0
 /**
  * Encodes this object as a compact JSON string, such as:
  *
  * <pre>{"query":"Pizza","locations":[94043,90210]}</pre>
  */
 @Override
 public String toString() {
   try {
     JSONStringer stringer = new JSONStringer();
     writeTo(stringer);
     return stringer.toString();
   } catch (JSONException e) {
     return null;
   }
 }
예제 #3
0
 /**
  * Encodes this object as a human readable JSON string for debugging, such as:
  *
  * <pre>
  * {
  *     "query": "Pizza",
  *     "locations": [
  *         94043,
  *         90210
  *     ]
  * }</pre>
  *
  * @param indentSpaces the number of spaces to indent for each level of nesting.
  */
 public String toString(int indentSpaces) throws JSONException {
   JSONStringer stringer = new JSONStringer(indentSpaces);
   writeTo(stringer);
   return stringer.toString();
 }