Пример #1
0
 /**
  * Make a JSON text of an Object value. If the object has an value.toJSONString() method, then
  * that method will be used to produce the JSON text. The method is required to produce a strictly
  * conforming text. If the object does not contain a toJSONString method (which is the most common
  * case), then a text will be produced by other means. If the value is an array or Collection,
  * then a JSONArray will be made from it and its toJSONString method will be called. If the value
  * is a MAP, then a JSONObject will be made from it and its toJSONString method will be called.
  * Otherwise, the value's toString method will be called, and the result will be quoted.
  *
  * <p>Warning: This method assumes that the data structure is acyclical.
  *
  * @param value The value to be serialized.
  * @return a printable, displayable, transmittable representation of the object, beginning with
  *     <code>{</code>&nbsp;<small>(left brace)</small> and ending with <code>}</code>
  *     &nbsp;<small>(right brace)</small>.
  * @throws JSONException If the value is or contains an invalid number.
  */
 public static String valueToString(Object value) throws JSONException {
   if ((value == null) || value.equals(null)) {
     return "null";
   }
   if (value instanceof JSONString) {
     Object object;
     try {
       object = ((JSONString) value).toJSONString();
     } catch (final Exception e) {
       throw new JSONException(e);
     }
     if (object instanceof String) {
       return (String) object;
     }
     throw new JSONException("Bad value from toJSONString: " + object);
   }
   if (value instanceof Number) {
     return JSONObject.numberToString((Number) value);
   }
   if ((value instanceof Boolean)
       || (value instanceof JSONObject)
       || (value instanceof JSONArray)) {
     return value.toString();
   }
   if (value instanceof Map) {
     return new JSONObject((Map) value).toString();
   }
   if (value instanceof Collection) {
     return new JSONArray((Collection) value).toString();
   }
   if (value.getClass().isArray()) {
     return new JSONArray(value).toString();
   }
   return JSONObject.quote(value.toString());
 }
Пример #2
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;
  }
Пример #3
0
 /**
  * Encodes {@code value} to this stringer.
  *
  * @param value a finite value. May not be {@link Double#isNaN() NaNs} or {@link
  *     Double#isInfinite() infinities}.
  * @return this stringer.
  */
 public JSONStringer value(double value) throws JSONException {
   if (stack.isEmpty()) {
     throw new JSONException("Nesting problem");
   }
   beforeValue();
   out.append(JSONObject.numberToString(value));
   return this;
 }
Пример #4
0
 public static String getJsonRepresentation(Object value) throws JSONException {
   if (value == null || value.equals(null)) {
     return "null";
   }
   if (value instanceof FString) {
     return JSONObject.quote(value.toString());
   }
   if (value instanceof YailList) {
     return ((YailList) value).toJSONString();
   }
   // The Json tokener used in getOnjectFromJson cannot handle
   // fractions.  So we Json encode fractions by first converting
   // them to doubles. This is an example of value with Kawa type any
   // being exposed to the rest of App Inventor by the value being
   // passed to a compoent method, in this case TinyDB or TinyWebDB
   // StoreValue.  See the "warning" comment in runtime.scm at
   // call-component-method.
   if (value instanceof IntFraction) {
     return JSONObject.numberToString((Number) ((IntFraction) value).doubleValue());
   }
   if (value instanceof Number) {
     return JSONObject.numberToString((Number) value);
   }
   if (value instanceof Boolean) {
     return value.toString();
   }
   if (value.getClass().isArray()) {
     StringBuilder sb = new StringBuilder();
     sb.append("[");
     String separator = "";
     for (Object o : (Object[]) value) {
       sb.append(separator).append(getJsonRepresentation(o));
       separator = ",";
     }
     sb.append("]");
     return sb.toString();
   }
   return JSONObject.quote(value.toString());
 }
Пример #5
0
 /**
  * Make a prettyprinted JSON text of an object value.
  *
  * <p>Warning: This method assumes that the data structure is acyclical.
  *
  * @param value The value to be serialized.
  * @param indentFactor The number of spaces to add to each level of indentation.
  * @param indent The indentation of the top level.
  * @return a printable, displayable, transmittable representation of the object, beginning with
  *     <code>{</code>&nbsp;<small>(left brace)</small> and ending with <code>}</code>
  *     &nbsp;<small>(right brace)</small>.
  * @throws JSONException If the object contains an invalid number.
  */
 static String valueToString(Object value, int indentFactor, int indent) throws JSONException {
   if ((value == null) || value.equals(null)) {
     return "null";
   }
   try {
     if (value instanceof JSONString) {
       final Object o = ((JSONString) value).toJSONString();
       if (o instanceof String) {
         return (String) o;
       }
     }
   } catch (final Exception ignore) {
   }
   if (value instanceof Number) {
     return JSONObject.numberToString((Number) value);
   }
   if (value instanceof Boolean) {
     return value.toString();
   }
   if (value instanceof JSONObject) {
     return ((JSONObject) value).toString(indentFactor, indent);
   }
   if (value instanceof JSONArray) {
     return ((JSONArray) value).toString(indentFactor, indent);
   }
   if (value instanceof Map) {
     return new JSONObject((Map) value).toString(indentFactor, indent);
   }
   if (value instanceof Collection) {
     return new JSONArray((Collection) value).toString(indentFactor, indent);
   }
   if (value.getClass().isArray()) {
     return new JSONArray(value).toString(indentFactor, indent);
   }
   return JSONObject.quote(value.toString());
 }