package org.json;
/*
Public Domain.
*/
import java.io.Closeable;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.ResourceBundle;
import java.util.Set;
import java.util.regex.Pattern;
/**
* A JSONObject is an unordered collection of name/value pairs. Its external
* form is a string wrapped in curly braces with colons between the names and
* values, and commas between the values and names. The internal form is an
* object having 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:
*
*
An extra , (comma) may appear just
* before the closing brace.
*
Strings may be quoted with ' (single
* quote).
*
Strings do not need to be quoted at all if they do not begin with a
* quote or single quote, and if they do not contain leading or trailing
* spaces, and if they do not contain any of these characters:
* { } [ ] / \ : , # and if they do not look like numbers and
* if they are not the reserved words true, false,
* or null.
*
*
* @author JSON.org
* @version 2016-08-15
*/
public class JSONObject {
/**
* JSONObject.NULL is equivalent to the value that JavaScript calls null,
* whilst Java's null is equivalent to the value that JavaScript calls
* undefined.
*/
private static final class Null {
/**
* There is only intended to be a single instance of the NULL object,
* so the clone method returns itself.
*
* @return NULL.
*/
@Override
protected final Object clone() {
return this;
}
/**
* A Null object is equal to the null value and to itself.
*
* @param object
* An object to test for nullness.
* @return true if the object parameter is the JSONObject.NULL object or
* null.
*/
@Override
@SuppressWarnings("lgtm[java/unchecked-cast-in-equals]")
public boolean equals(Object object) {
return object == null || object == this;
}
/**
* A Null object is equal to the null value and to itself.
*
* @return always returns 0.
*/
@Override
public int hashCode() {
return 0;
}
/**
* Get the "null" string value.
*
* @return The string "null".
*/
@Override
public String toString() {
return "null";
}
}
/**
* Regular Expression Pattern that matches JSON Numbers. This is primarily used for
* output to guarantee that we are always writing valid JSON.
*/
static final Pattern NUMBER_PATTERN = Pattern.compile("-?(?:0|[1-9]\\d*)(?:\\.\\d+)?(?:[eE][+-]?\\d+)?");
/**
* The map where the JSONObject's properties are kept.
*/
private final Map map;
/**
* Retrieves the type of the underlying Map in this class.
*
* @return The class object representing the type of the underlying Map.
*/
public Class extends Map> getMapType() {
return map.getClass();
}
/**
* It is sometimes more convenient and less ambiguous to have a
* 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() {
// HashMap is used on purpose to ensure that elements are unordered by
// the specification.
// JSON tends to be a portable transfer format to allows the container
// implementations to rearrange their items for a faster element
// retrieval based on associative access.
// Therefore, an implementation mustn't rely on the order of the item.
this.map = new HashMap();
}
/**
* Construct a JSONObject from a subset of another JSONObject. An array of
* strings is used to identify the keys that should be copied. Missing keys
* are ignored.
*
* @param jo
* A JSONObject.
* @param names
* An array of strings.
*/
public JSONObject(JSONObject jo, String ... names) {
this(names.length);
for (int i = 0; i < names.length; i += 1) {
try {
this.putOnce(names[i], jo.opt(names[i]));
} catch (Exception ignore) {
}
}
}
/**
* Construct a JSONObject from a JSONTokener.
*
* @param x
* A JSONTokener object containing the source string.
* @throws JSONException
* If there is a syntax error in the source string or a
* duplicated key.
*/
public JSONObject(JSONTokener x) throws JSONException {
this();
char c;
String key;
if (x.nextClean() != '{') {
throw x.syntaxError("A JSONObject text must begin with '{'");
}
for (;;) {
c = x.nextClean();
switch (c) {
case 0:
throw x.syntaxError("A JSONObject text must end with '}'");
case '}':
return;
default:
key = x.nextSimpleValue(c).toString();
}
// The key is followed by ':'.
c = x.nextClean();
if (c != ':') {
throw x.syntaxError("Expected a ':' after a key");
}
// Use syntaxError(..) to include error location
if (key != null) {
// Check if key exists
if (this.opt(key) != null) {
// key already exists
throw x.syntaxError("Duplicate key \"" + key + "\"");
}
// Only add value if non-null
Object value = x.nextValue();
if (value!=null) {
this.put(key, value);
}
}
// Pairs are separated by ','.
switch (x.nextClean()) {
case ';':
case ',':
if (x.nextClean() == '}') {
return;
}
if (x.end()) {
throw x.syntaxError("A JSONObject text must end with '}'");
}
x.back();
break;
case '}':
return;
default:
throw x.syntaxError("Expected a ',' or '}'");
}
}
}
/**
* Construct a JSONObject from a Map.
*
* @param m
* A map object that can be used to initialize the contents of
* the JSONObject.
* @throws JSONException
* If a value in the map is non-finite number.
* @throws NullPointerException
* If a key in the map is null
*/
public JSONObject(Map, ?> m) {
this(m, 0, new JSONParserConfiguration());
}
/**
* Construct a JSONObject from a Map with custom json parse configurations.
*
* @param m
* A map object that can be used to initialize the contents of
* the JSONObject.
* @param jsonParserConfiguration
* Variable to pass parser custom configuration for json parsing.
*/
public JSONObject(Map, ?> m, JSONParserConfiguration jsonParserConfiguration) {
this(m, 0, jsonParserConfiguration);
}
/**
* Construct a JSONObject from a map with recursion depth.
*
*/
private JSONObject(Map, ?> m, int recursionDepth, JSONParserConfiguration jsonParserConfiguration) {
if (recursionDepth > jsonParserConfiguration.getMaxNestingDepth()) {
throw new JSONException("JSONObject has reached recursion depth limit of " + jsonParserConfiguration.getMaxNestingDepth());
}
if (m == null) {
this.map = new HashMap();
} else {
this.map = new HashMap(m.size());
for (final Entry, ?> e : m.entrySet()) {
if(e.getKey() == null) {
throw new NullPointerException("Null key.");
}
final Object value = e.getValue();
if (value != null) {
testValidity(value);
this.map.put(String.valueOf(e.getKey()), wrap(value, recursionDepth + 1, jsonParserConfiguration));
}
}
}
}
/**
* Construct a JSONObject from an Object using bean getters. It reflects on
* all of the public methods of the object. For each of the methods with no
* parameters and a name starting with "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.
*
* Methods that are static, return void,
* have parameters, or are "bridge" methods, are ignored.
*
* 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".
*
* The {@link JSONPropertyName} annotation can be used on a bean getter to
* override key name used in the JSONObject. For example, using the object
* above with the getName method, if we annotated it with:
*
* The resulting JSON object would contain "FullName": "Larry Fine"
*
* Similarly, the {@link JSONPropertyName} annotation can be used on non-
* get and is methods. We can also override key
* name used in the JSONObject as seen below even though the field would normally
* be ignored:
*
* The resulting JSON object would contain "FullName": "Larry Fine"
*
* The {@link JSONPropertyIgnore} annotation can be used to force the bean property
* to not be serialized into JSON. If both {@link JSONPropertyIgnore} and
* {@link JSONPropertyName} are defined on the same method, a depth comparison is
* performed and the one closest to the concrete class being serialized is used.
* If both annotations are at the same level, then the {@link JSONPropertyIgnore}
* annotation takes precedent and the field is not serialized.
* For example, the following declaration would prevent the getName
* method from being serialized:
*
*
* @param bean
* An object that has getter methods that should be used to make
* a JSONObject.
* @throws JSONException
* If a getter returned a non-finite number.
*/
public JSONObject(Object bean) {
this();
this.populateMap(bean);
}
private JSONObject(Object bean, Set