}
}
-
/**
* The map where the JSONObject's properties are kept.
*/
private final Map<String, Object> map;
-
/**
* It is sometimes more convenient and less ambiguous to have a
* <code>NULL</code> object than to use Java's <code>null</code> value.
*/
public static final Object NULL = new Null();
-
/**
* Construct an empty JSONObject.
*/
this.map = new LinkedHashMap<>();
}
-
/**
* Construct a JSONObject from a subset of another JSONObject.
* An array of strings is used to identify the keys that should be copied.
}
}
-
/**
* Construct a JSONObject from a JSONTokener.
*
}
}
-
/**
* Construct a JSONObject from a Map.
*
}
}
-
/**
* Construct a JSONObject from an Object using bean getters.
* It reflects on all of the public methods of the object.
this.populateMap(bean);
}
-
/**
* Construct a JSONObject from an Object, using reflection to find the
* public members. The resulting JSONObject's keys will be the strings
}
}
-
/**
* Construct a JSONObject from a source JSON text string.
* This is the most commonly used JSONObject constructor.
this(new JSONTokener(source));
}
-
/**
* Construct a JSONObject from a ResourceBundle.
*
}
}
-
/**
* Accumulate values under a key. It is similar to the put method except
* that if there is already an object stored under the key then a
return this;
}
-
/**
* Append values to the array under a key. If the key does not exist in the
* JSONObject, then the key is put in the JSONObject with its value being a
return this;
}
-
/**
* Produce a string from a double. The string "null" will be returned if
* the number is not finite.
return string;
}
-
/**
* Get the value object associated with a key.
*
return object;
}
-
/**
* Get the boolean value associated with a key.
*
"] is not a Boolean.");
}
-
/**
* Get the double value associated with a key.
*
}
}
-
/**
* Get the int value associated with a key.
*
}
}
-
/**
* Get the JSONArray value associated with a key.
*
"] is not a JSONArray.");
}
-
/**
* Get the JSONObject value associated with a key.
*
"] is not a JSONObject.");
}
-
/**
* Get the long value associated with a key.
*
}
}
-
/**
* Get an array of field names from a JSONObject.
*
return names;
}
-
- /**
- * Get an array of field names from an Object.
- *
- * @return An array of field names, or null if there are no names.
- */
- public static String[] getNames(Object object) {
- if (object == null) {
- return null;
- }
- Class<? extends Object> klass = object.getClass();
- Field[] fields = klass.getFields();
- int length = fields.length;
- if (length == 0) {
- return null;
- }
- String[] names = new String[length];
- for (int i = 0; i < length; i += 1) {
- names[i] = fields[i].getName();
- }
- return names;
- }
-
-
/**
* Get the string associated with a key.
*
"] not a string.");
}
-
/**
* Determine if the JSONObject contains a specific key.
*
return this.map.containsKey(key);
}
-
/**
* 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
return this;
}
-
- /**
- * Determine if the value associated with the key is null or if there is
- * no value.
- *
- * @param key A key string.
- * @return true if there is no value associated with the key or if
- * the value is the JSONObject.NULL object.
- */
- public boolean isNull(String key) {
- return LOGJSONObject.NULL.equals(this.opt(key));
- }
-
-
/**
* Get an enumeration of the keys of the JSONObject.
*
return this.keySet().iterator();
}
-
/**
* Get a set of keys of the JSONObject.
*
return this.map.keySet();
}
-
/**
* Get the number of keys stored in the JSONObject.
*
return this.map.size();
}
-
/**
* Produce a JSONArray containing the names of the elements of this
* JSONObject.
return string;
}
-
/**
* Get an optional value associated with a key.
*
return key == null ? null : this.map.get(key);
}
-
- /**
- * Get an optional boolean associated with a key.
- * It returns false if there is no such key, or if the value is not
- * Boolean.TRUE or the String "true".
- *
- * @param key A key string.
- * @return The truth.
- */
- public boolean optBoolean(String key) {
- return this.optBoolean(key, false);
- }
-
-
/**
* Get an optional boolean associated with a key.
* It returns the defaultValue if there is no such key, or if it is not
}
}
-
- /**
- * Get an optional double associated with a key,
- * or NaN if there is no such key or if its value is not a number.
- * If the value is a string, an attempt will be made to evaluate it as
- * a number.
- *
- * @param key A string which is the key.
- * @return An object which is the value.
- */
- public double optDouble(String key) {
- return this.optDouble(key, Double.NaN);
- }
-
-
/**
* Get an optional double associated with a key, or the
* defaultValue if there is no such key or if its value is not a number.
}
}
-
- /**
- * Get an optional int value associated with a key,
- * or zero if there is no such key or if the value is not a number.
- * If the value is a string, an attempt will be made to evaluate it as
- * a number.
- *
- * @param key A key string.
- * @return An object which is the value.
- */
- public int optInt(String key) {
- return this.optInt(key, 0);
- }
-
-
/**
* Get an optional int value associated with a key,
* or the default if there is no such key or if the value is not a number.
}
}
-
- /**
- * Get an optional JSONArray associated with a key.
- * It returns null if there is no such key, or if its value is not a
- * JSONArray.
- *
- * @param key A key string.
- * @return A JSONArray which is the value.
- */
- public JSONArray optJSONArray(String key) {
- Object o = this.opt(key);
- return o instanceof JSONArray ? (JSONArray) o : null;
- }
-
-
/**
* Get an optional JSONObject associated with a key.
* It returns null if there is no such key, or if its value is not a
return object instanceof LOGJSONObject ? (LOGJSONObject) object : null;
}
-
- /**
- * Get an optional long value associated with a key,
- * or zero if there is no such key or if the value is not a number.
- * If the value is a string, an attempt will be made to evaluate it as
- * a number.
- *
- * @param key A key string.
- * @return An object which is the value.
- */
- public long optLong(String key) {
- return this.optLong(key, 0);
- }
-
-
/**
* Get an optional long value associated with a key,
* or the default if there is no such key or if the value is not a number.
}
}
-
- /**
- * Get an optional string associated with a key.
- * It returns an empty string if there is no such key. If the value is not
- * a string and is not null, then it is converted to a string.
- *
- * @param key A key string.
- * @return A string which is the value.
- */
- public String optString(String key) {
- return this.optString(key, "");
- }
-
-
/**
* Get an optional string associated with a key.
* It returns the defaultValue if there is no such key.
return NULL.equals(object) ? defaultValue : object.toString();
}
-
private void populateMap(Object bean) {
Class<? extends Object> klass = bean.getClass();
}
}
-
/**
* Put a key/boolean pair in the JSONObject.
*
return this;
}
-
/**
* Put a key/value pair in the JSONObject, where the value will be a
* JSONArray which is produced from a Collection.
return this;
}
-
/**
* Put a key/double pair in the JSONObject.
*
return this;
}
-
/**
* Put a key/int pair in the JSONObject.
*
return this;
}
-
/**
* Put a key/long pair in the JSONObject.
*
return this;
}
-
/**
* Put a key/value pair in the JSONObject, where the value will be a
* JSONObject which is produced from a Map.
return this;
}
-
/**
* Put a key/value pair in the JSONObject. If the value is null,
* then the key will be removed from the JSONObject if it is present.
return this;
}
-
/**
* Put a key/value pair in the JSONObject, but only if the key and the
* value are both non-null, and only if there is not already a member
return this;
}
-
/**
* Put a key/value pair in the JSONObject, but only if the
* key and the value are both non-null.
return this;
}
-
/**
* Produce a string in double quotes with backslash sequences in all the
* right places. A backslash will be inserted within </, producing <\/,
return string;
}
-
/**
* Throw an exception if the object is a NaN or infinite number.
*
}
}
-
/**
* Produce a JSONArray containing the values of the members of this
* JSONObject.
}
}
-
/**
* Make a prettyprinted JSON text of this JSONObject.
* <p>
}
}
-
- /**
- * 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 {
- return this.write(writer, 0, 0);
- }
-
-
@SuppressWarnings("unchecked")
static final Writer writeValue(Writer writer, Object value,
int indentFactor, int indent) throws JSONException, IOException {