예제 #1
0
 /**
  * Get the JSONObject value associated with a key.
  *
  * @param key A key string.
  * @return A JSONObject which is the value.
  * @throws JSONException if the key is not found or if the value is not a JSONObject.
  */
 public JSONObject getJSONObject(String key) throws JSONException {
   final Object object = this.get(key);
   if (object instanceof JSONObject) {
     return (JSONObject) object;
   }
   throw new JSONException("JSONObject[" + JSONObject.quote(key) + "] is not a JSONObject.");
 }
예제 #2
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());
 }
예제 #3
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);
    }
  }
예제 #4
0
 /**
  * Get the long value associated with a key.
  *
  * @param key A key string.
  * @return The long value.
  * @throws JSONException if the key is not found or if the value cannot be converted to a long.
  */
 public long getLong(String key) throws JSONException {
   final Object object = this.get(key);
   try {
     return object instanceof Number
         ? ((Number) object).longValue()
         : Long.parseLong((String) object);
   } catch (final Exception e) {
     throw new JSONException("JSONObject[" + JSONObject.quote(key) + "] is not a long.");
   }
 }
예제 #5
0
 /**
  * Get the int value associated with a key.
  *
  * @param key A key string.
  * @return The integer value.
  * @throws JSONException if the key is not found or if the value cannot be converted to an
  *     integer.
  */
 public int getInt(String key) throws JSONException {
   final Object object = this.get(key);
   try {
     return object instanceof Number
         ? ((Number) object).intValue()
         : Integer.parseInt((String) object);
   } catch (final Exception e) {
     throw new JSONException("JSONObject[" + JSONObject.quote(key) + "] is not an int.");
   }
 }
예제 #6
0
 /**
  * Get the double value associated with a key.
  *
  * @param key A key string.
  * @return The numeric value.
  * @throws JSONException if the key is not found or if the value is not a Number object and cannot
  *     be converted to a number.
  */
 public double getDouble(String key) throws JSONException {
   final Object object = this.get(key);
   try {
     return object instanceof Number
         ? ((Number) object).doubleValue()
         : Double.parseDouble((String) object);
   } catch (final Exception e) {
     throw new JSONException("JSONObject[" + JSONObject.quote(key) + "] is not a number.");
   }
 }
예제 #7
0
 /**
  * Get the value object associated with a key.
  *
  * @param key A key string.
  * @return The object associated with the key.
  * @throws JSONException if the key is not found.
  */
 public Object get(String key) throws JSONException {
   if (key == null) {
     throw new JSONException("Null key.");
   }
   final Object object = this.opt(key);
   if (object == null) {
     throw new JSONException("JSONObject[" + JSONObject.quote(key) + "] not found.");
   }
   return object;
 }
예제 #8
0
 /**
  * Get the boolean value associated with a key.
  *
  * @param key A key string.
  * @return The truth.
  * @throws JSONException if the value is not a Boolean or the String "true" or "false".
  */
 public boolean getBoolean(String key) throws JSONException {
   final Object object = this.get(key);
   if (object.equals(Boolean.FALSE)
       || ((object instanceof String) && ((String) object).equalsIgnoreCase("false"))) {
     return false;
   } else if (object.equals(Boolean.TRUE)
       || ((object instanceof String) && ((String) object).equalsIgnoreCase("true"))) {
     return true;
   }
   throw new JSONException("JSONObject[" + JSONObject.quote(key) + "] is not a Boolean.");
 }
예제 #9
0
 /**
  * Make a prettyprinted JSON text of this JSONObject.
  *
  * <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 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.
  */
 String toString(int indentFactor, int indent) throws JSONException {
   int i;
   final int length = this.length();
   if (length == 0) {
     return "{}";
   }
   final Iterator keys = this.keys();
   final int newindent = indent + indentFactor;
   Object object;
   final StringBuffer sb = new StringBuffer("{");
   if (length == 1) {
     object = keys.next();
     sb.append(JSONObject.quote(object.toString()));
     sb.append(": ");
     sb.append(JSONObject.valueToString(this.map.get(object), indentFactor, indent));
   } else {
     while (keys.hasNext()) {
       object = keys.next();
       if (sb.length() > 1) {
         sb.append(",\n");
       } else {
         sb.append('\n');
       }
       for (i = 0; i < newindent; i += 1) {
         sb.append(' ');
       }
       sb.append(JSONObject.quote(object.toString()));
       sb.append(": ");
       sb.append(JSONObject.valueToString(this.map.get(object), indentFactor, newindent));
     }
     if (sb.length() > 1) {
       sb.append('\n');
       for (i = 0; i < indent; i += 1) {
         sb.append(' ');
       }
     }
   }
   sb.append('}');
   return sb.toString();
 }
예제 #10
0
 /**
  * Increment a property of a JSONObject. If there is no such property, create one with a value of
  * 1. If there is such a property, and if it is an Integer, Long, Double, or Float, then add one
  * to it.
  *
  * @param key A key string.
  * @return this.
  * @throws JSONException If there is already a property with this name that is not an Integer,
  *     Long, Double, or Float.
  */
 public JSONObject increment(String key) throws JSONException {
   final Object value = this.opt(key);
   if (value == null) {
     this.put(key, 1);
   } else if (value instanceof Integer) {
     this.put(key, ((Integer) value).intValue() + 1);
   } else if (value instanceof Long) {
     this.put(key, ((Long) value).longValue() + 1);
   } else if (value instanceof Double) {
     this.put(key, ((Double) value).doubleValue() + 1);
   } else if (value instanceof Float) {
     this.put(key, ((Float) value).floatValue() + 1);
   } else {
     throw new JSONException("Unable to increment [" + JSONObject.quote(key) + "].");
   }
   return this;
 }
예제 #11
0
  /**
   * Make a JSON text of this JSONObject. For compactness, no whitespace is added. If this would not
   * result in a syntactically correct JSON text, then null will be returned instead.
   *
   * <p>Warning: This method assumes that the data structure is acyclical.
   *
   * @return a printable, displayable, portable, transmittable representation of the object,
   *     beginning with <code>{</code>&nbsp;<small>(left brace)</small> and ending with <code>}
   *     </code>&nbsp;<small>(right brace)</small>.
   */
  @Override
  public String toString() {
    try {
      final Iterator keys = this.keys();
      final StringBuffer sb = new StringBuffer("{");

      while (keys.hasNext()) {
        if (sb.length() > 1) {
          sb.append(',');
        }
        final Object o = keys.next();
        sb.append(JSONObject.quote(o.toString()));
        sb.append(':');
        sb.append(JSONObject.valueToString(this.map.get(o)));
      }
      sb.append('}');
      return sb.toString();
    } catch (final Exception e) {
      return null;
    }
  }
예제 #12
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());
 }