get and opt methods for accessing
* the values by name, and put methods for adding or replacing
* values by name. The values can be any of these types: Boolean,
* JSONArray, JSONObject, Number,
* String, or the JSONObject.NULL object. A
* JSONObject constructor can be used to convert an external form JSON text
* into an internal form whose values can be retrieved with the
* get and opt methods, or to convert values into a
* JSON text using the put and toString methods. A
* get method returns a value if one can be found, and throws an
* exception if one cannot be found. An opt method returns a
* default value instead of throwing an exception, and so is useful for
* obtaining optional values.
*
* The generic get() and opt() methods return an
* object, which you can cast or query for type. There are also typed
* get and opt methods that do type checking and type
* coercion for you. The opt methods differ from the get methods in that they
* do not throw. Instead, they return a specified value, such as null.
*
* The put methods add or replace values in an object. For
* example,
*
*
* myString = new JSONObject()
* .put("JSON", "Hello, World!").toString();
*
*
* produces the string {"JSON": "Hello, World"}.
*
* The texts produced by the toString methods strictly conform to
* the JSON syntax rules. The constructors are more forgiving in the texts they
* will accept:
*
, (comma) may appear just
* before the closing brace.' (single
* quote).{ } [ ] / \ : , = ; # and if they do not look like numbers and
* if they are not the reserved words true, false,
* or null.= or => as well as by
* :.; (semicolon) as
* well as by , (comma).NULL object than to use Java's null value.
* JSONObject.NULL.equals(null) returns true.
* JSONObject.NULL.toString() returns "null".
*/
public static final Object NULL = new Null();
/**
* Construct an empty JSONObject.
*/
public JSONObject() {
this.map = new HashMap"get" or
* "is" followed by an uppercase letter, the method is invoked,
* and a key and the value returned from the getter method are put into the
* new JSONObject.
*
* The key is formed by removing the "get" or "is"
* prefix. If the second remaining character is not upper case, then the
* first character is converted to lower case.
*
* For example, if an object has a method named "getName", and
* if the result of calling object.getName() is
* "Larry Fine", then the JSONObject will contain
* "name": "Larry Fine".
*
* @param bean
* An object that has getter methods that should be used to make
* a JSONObject.
*/
public JSONObject(Object bean) {
this();
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 from
* the names array, and the values will be the field values associated with
* those keys in the object. If a key is not found or not visible, then it
* will not be copied into the new JSONObject.
*
* @param object
* An object that has fields that should be used to make a
* JSONObject.
* @param names
* An array of strings, the names of the fields to be obtained
* from the object.
*/
public JSONObject(Object object, String names[]) {
this();
Class c = object.getClass();
for (int i = 0; i < names.length; i += 1) {
String name = names[i];
try {
this.putOpt(name, c.getField(name).get(object));
} catch (Exception ignore) {
}
}
}
/**
* Construct a JSONObject from a source JSON text string. This is the most
* commonly used JSONObject constructor.
* * This performs a lenient parse using the {@code JSONTokener} parser. * For a strict parse, use the {@code JSONStrictTokener} class. *
* * @param source * A string beginning with{ (left
* brace) and ending with }
* (right brace).
* @exception JSONException
* If there is a syntax error in the source string or a
* duplicated key.
*/
public JSONObject(String source) throws JSONException {
this(new JSONTokener(source));
}
/**
* Construct a JSONObject from a ResourceBundle.
*
* @param baseName
* The ResourceBundle base name.
* @param locale
* The Locale to load the ResourceBundle for.
* @throws JSONException
* If any JSONExceptions are detected.
*/
public JSONObject(String baseName, Locale locale) throws JSONException {
this();
ResourceBundle bundle = ResourceBundle.getBundle(baseName, locale,
Thread.currentThread().getContextClassLoader());
// Iterate through the keys in the bundle.
Enumeration * {
* "a":{"b":"c"}
* }
*
* and this JSONPointer string:
* * "/a/b"
*
* Then this method will return the String "c".
* A JSONPointerException may be thrown from code called by this method.
*
* @param jsonPointer string that can be used to create a JSONPointer
* @return the item matched by the JSONPointer, otherwise null
*/
public Object query(String jsonPointer) {
return new JSONPointer(jsonPointer).queryFrom(this);
}
/**
* Queries and returns a value from this object using {@code jsonPointer}, or
* returns null if the query fails due to a missing key.
*
* @param jsonPointer the string representation of the JSON pointer
* @return the queried value or {@code null}
* @throws IllegalArgumentException if {@code jsonPointer} has invalid syntax
*/
public Object optQuery(String jsonPointer) {
JSONPointer pointer = new JSONPointer(jsonPointer);
try {
return pointer.queryFrom(this);
} catch (JSONPointerException e) {
return null;
}
}
/**
* Produce a string in double quotes with backslash sequences in all the
* right places. A backslash will be inserted within </, producing <\/,
* allowing JSON text to be delivered in HTML. In JSON text, a string cannot
* contain a control character or an unescaped quote or backslash.
*
* @param string
* A String
* @return A String correctly formatted for insertion in a JSON text.
*/
public static String quote(CharSequence string) {
StringBuilder sb = new StringBuilder();
WriterUtil.writeString(string, sb);
return sb.toString();
}
/**
* Produce a string in double quotes with backslash sequences in all the
* right places. A backslash will be inserted within </, producing <\/,
* allowing JSON text to be delivered in HTML. In JSON text, a string cannot
* contain a control character or an unescaped quote or backslash.
*
* @param string
* A character sequence to be quoted
* @param w
* the Appendable to which the quoted character sequence will be
* written
* @param * This is a lenient parse. Special words are treated as case-insensitive, * and numbers are parsed as Double.valueOf() or Long.valueOf() allows. * Any other bare words are treated as strings. *
* * @param string * A String. * @return A simple JSON value. */ public static Object stringToValue(String string) { if (string.equals("")) { return string; } if (string.equalsIgnoreCase("true")) { return Boolean.TRUE; } if (string.equalsIgnoreCase("false")) { return Boolean.FALSE; } if (string.equalsIgnoreCase("null")) { return JSONObject.NULL; } /* * If it might be a number, try converting it. If a number cannot be * produced, then the value will just be a string. */ char initial = string.charAt(0); if ((initial >= '0' && initial <= '9') || initial == '-') { try { if (string.indexOf('.') > -1 || string.indexOf('e') > -1 || string.indexOf('E') > -1 || "-0".equals(string)) { Double d = Double.valueOf(string); if (!d.isInfinite() && !d.isNaN()) { return d; } } else { Long myLong = Long.valueOf(string); if (string.equals(myLong.toString())) { if (myLong.longValue() == myLong.intValue()) { return Integer.valueOf(myLong.intValue()); } return myLong; } } } catch (Exception ignore) { } } return string; } /** * Throw an exception if the object is a NaN or infinite number. * * @param o * The object to test. * @throws JSONException * If o is a non-finite number. */ public static void testValidity(Object o) throws JSONException { if (o != null) { if (o instanceof Double) { if (((Double) o).isInfinite() || ((Double) o).isNaN()) { throw new JSONException( "JSON does not allow non-finite numbers."); } } else if (o instanceof Float) { if (((Float) o).isInfinite() || ((Float) o).isNaN()) { throw new JSONException( "JSON does not allow non-finite numbers."); } } } } /** * Produce a JSONArray containing the values of the members of this * JSONObject. * * @param names * A JSONArray containing a list of key strings. This determines * the sequence of the values in the result. * @return A JSONArray of values. * @throws JSONException * If any of the values are non-finite numbers. */ public JSONArray toJSONArray(JSONArray names) throws JSONException { if (names == null || names.length() == 0) { return null; } JSONArray ja = new JSONArray(); for (int i = 0; i < names.length(); i += 1) { ja.put(this.opt(names.getString(i))); } return ja; } /** * 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. *
* Warning: This method assumes that the data structure is acyclical.
*
* @return a printable, displayable, portable, transmittable representation
* of the object, beginning with { (left
* brace) and ending with } (right
* brace).
*/
@Override
public String toString() {
try {
return writeValue(this, new StringBuilder(), 0, 0).toString();
} catch (Exception e) {
return null;
}
}
/**
* Make a prettyprinted JSON text of this JSONObject.
*
* Warning: This method assumes that the data structure is acyclical.
*
* @param indentFactor
* The number of spaces to add to each level of indentation.
* @return a printable, displayable, portable, transmittable representation
* of the object, beginning with { (left
* brace) and ending with } (right
* brace).
* @throws JSONException
* If the object contains an invalid number.
*/
public String toString(int indentFactor) throws JSONException {
return writeValue(this, new StringBuilder(), indentFactor, 0).toString();
}
/**
* 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.
*
*
* 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 { (left
* brace) and ending with } (right
* brace).
* @throws JSONException
* If the value is or contains an invalid number.
*/
public static String valueToString(Object value) throws JSONException {
if((value instanceof JSONString) && !(value instanceof JSONAppendable)) {
String result;
try {
result = ((JSONString)value).toJSONString();
} catch (Exception e) {
throw new JSONException(e);
}
if(result == null) {
throw new JSONException("Bad value from toJSONString: " + result);
} else {
return result;
}
} else {
return writeValue(value, new StringBuilder(), 0, 0).toString();
}
}
/**
* Wrap an object, if necessary. If the object is null, return the NULL
* object. If it is an array or collection, wrap it in a JSONArray. If it is
* a map, wrap it in a JSONObject. If it is a standard property (Double,
* String, et al) then it is already wrapped. Otherwise, if it comes from
* one of the java packages, turn it into a string. And if it doesn't, try
* to wrap it in a JSONObject. If the wrapping fails, then null is returned.
*
* @param object
* The object to wrap
* @return The wrapped value
*/
public static Object wrap(Object object) {
try {
if (object == null) {
return NULL;
}
if ((object instanceof Double) || (object instanceof Float)) {
double d = ((Number)object).doubleValue();
if (Double.isInfinite(d) || Double.isNaN(d)) {
return NULL;
}
return object;
}
if (object instanceof JSONObject || object instanceof JSONArray
|| object instanceof Boolean || object instanceof String
|| object instanceof Number || object instanceof Enum
|| object instanceof JSONString || object instanceof JSONAppendable
|| object instanceof Character || NULL.equals(object)) {
return object;
}
if (object instanceof Map) {
return new JSONObject((Map) object);
}
if (object instanceof Iterable) {
return new JSONArray((Iterable) object);
}
if (object.getClass().isArray()) {
return new JSONArray(object);
}
if (objectIsBean(object)) {
return new JSONObject(object);
}
return object.toString();
} catch (Exception exception) {
return null;
}
}
/**
* Determine whether this is a JavaBean for the purposes of writing
* or populating a JSON object.
*
* @param object the object to be tested
* @return {@code true} if this object can be written as a JSON Object
* using JavaBean getter methods, otherwise {@code false}
*/
static boolean objectIsBean(Object object) {
Package objectPackage = object.getClass().getPackage();
String objectPackageName = objectPackage != null ? objectPackage
.getName() : "";
return !objectPackageName.startsWith("java.")
&& !objectPackageName.startsWith("javax.")
&& object.getClass().getClassLoader() != null;
}
/**
* Write the contents of the JSONObject as JSON text to a writer. For
* compactness, no whitespace is added.
*
* Warning: This method assumes that the data structure is acyclical.
*
* @param writer
* Writes the serialized JSON
* @param
* Warning: This method assumes that the data structure is acyclical.
*
* @param writer
* Writes the serialized JSON
* @param indentFactor
* The number of spaces to add to each level of indentation.
* @param indent
* The indention of the top level.
* @param
* Warning: This method assumes that the data structure is acyclical.
*
* @param value
* The Object to be written
* @param writer
* Writes the serialized JSON
* @param indentFactor
* The number of spaces to add to each level of indentation.
* @param indent
* The indention of the top level.
* @param
* Warning: This method assumes that the data structure is acyclical.
*
* @return a java.util.Map containing the entries of this object
*/
public Map