2531 lines
78 KiB
Java
2531 lines
78 KiB
Java
package org.json;
|
|
|
|
/*
|
|
Public Domain.
|
|
*/
|
|
|
|
import java.io.IOException;
|
|
import java.io.Writer;
|
|
import java.lang.reflect.Field;
|
|
import java.lang.reflect.GenericArrayType;
|
|
import java.lang.reflect.Method;
|
|
import java.lang.reflect.Modifier;
|
|
import java.lang.reflect.ParameterizedType;
|
|
import java.lang.reflect.Type;
|
|
import java.math.BigDecimal;
|
|
import java.math.BigInteger;
|
|
import java.util.ArrayList;
|
|
import java.util.Collection;
|
|
import java.util.HashMap;
|
|
import java.util.HashSet;
|
|
import java.util.Iterator;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.Map.Entry;
|
|
import java.util.Set;
|
|
|
|
/**
|
|
* 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 <code>get</code> and <code>opt</code> methods
|
|
* for accessing the values by name, and <code>put</code> methods for adding or replacing values by
|
|
* name. The values can be any of these types: <code>Boolean</code>, <code>JSONArray</code>,
|
|
* <code>JSONObject</code>, <code>Number</code>, <code>String</code>, or the
|
|
* <code>JSONObject.NULL</code> 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 <code>get</code> and
|
|
* <code>opt</code> methods, or to convert values into a JSON text using the <code>put</code> and
|
|
* <code>toString</code> methods. A <code>get</code> method returns a value if one can be found, and
|
|
* throws an exception if one cannot be found. An <code>opt</code> method returns a default value
|
|
* instead of throwing an exception, and so is useful for obtaining optional values.
|
|
* <p>
|
|
* The generic <code>get()</code> and <code>opt()</code> methods return an object, which you can
|
|
* cast or query for type. There are also typed <code>get</code> and <code>opt</code> 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.
|
|
* <p>
|
|
* The <code>put</code> methods add or replace values in an object. For example,
|
|
*
|
|
* <pre>
|
|
* myString = new JSONObject()
|
|
* .put("JSON", "Hello, World!").toString();
|
|
* </pre>
|
|
*
|
|
* produces the string <code>{"JSON": "Hello, World"}</code>.
|
|
* <p>
|
|
* The texts produced by the <code>toString</code> methods strictly conform to the JSON syntax
|
|
* rules. The constructors are more forgiving in the texts they will accept:
|
|
* <ul>
|
|
* <li>An extra <code>,</code> <small>(comma)</small> may appear just before the closing
|
|
* brace.</li>
|
|
* <li>Strings may be quoted with <code>'</code> <small>(single quote)</small>.</li>
|
|
* <li>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: <code>{ } [ ] / \ : , #</code> and if they do not look like numbers and if they are
|
|
* not the reserved words <code>true</code>, <code>false</code>, or <code>null</code>.</li>
|
|
* </ul>
|
|
*
|
|
* @author JSON.org
|
|
* @version 2016-08-15
|
|
*/
|
|
|
|
public class JSONObject extends JSONBase {
|
|
|
|
/**
|
|
* The map where the JSONObject's properties are kept.
|
|
*/
|
|
|
|
private final Map<String, Object> map;
|
|
|
|
//TODO: Remove dead code
|
|
/*
|
|
* Retrieves the type of the underlying Map in this class.
|
|
*
|
|
* @return The class object representing the type of the underlying Map.
|
|
*/
|
|
|
|
//TODO: Remove dead code
|
|
/*
|
|
public Class<? extends Map> getMapType() {
|
|
return map.getClass();
|
|
}
|
|
*/
|
|
|
|
/**
|
|
* 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<String, Object>();
|
|
}
|
|
|
|
/**
|
|
* 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 (String name : names) {
|
|
try {
|
|
this.putOnce(name, jo.opt(name));
|
|
}
|
|
catch (Exception ignore) { // exception thrown for missing key
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Construct a JSONObject from a JSONTokener.
|
|
*
|
|
* @param jtok 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 jtok) throws JSONException {
|
|
this(jtok, jtok.getJsonParserConfiguration());
|
|
}
|
|
|
|
/**
|
|
* Construct a JSONObject from a JSONTokener with custom json parse configurations. The supplied
|
|
* configuration applies to all values during construction, including nested objects and arrays. The
|
|
* tokener's original configuration is restored afterward, even if parsing fails.
|
|
*
|
|
* @param jtok A JSONTokener object containing the source string.
|
|
* @param pconfig Variable to pass parser custom configuration for json parsing.
|
|
* @throws JSONException If there is a syntax error in the source string or a duplicated key.
|
|
*/
|
|
|
|
public JSONObject(JSONTokener jtok, JSONParserConfiguration pconfig) throws JSONException {
|
|
this(jtok, pconfig, true);
|
|
}
|
|
|
|
/**
|
|
* Construct a JSONObject from a JSONTokener with custom json parse configurations, for internal
|
|
* use. Never call this instead of using withStrictMode(boolean).
|
|
*
|
|
* @param jtok A JSONTokener object containing the source string.
|
|
* @param pconfig Variable to pass parser custom configuration for json parsing.
|
|
* @param isInitial A boolean that determines whether this object is the root.
|
|
* @throws JSONException If there is a syntax error in the source string or a duplicated key.
|
|
*/
|
|
|
|
JSONObject(JSONTokener jtok, JSONParserConfiguration pconfig, boolean isInitial)
|
|
throws JSONException {
|
|
this();
|
|
JSONParserConfiguration originalConfiguration = jtok.getJsonParserConfiguration();
|
|
jtok.setJsonParserConfigurationInternal(pconfig);
|
|
try {
|
|
if (jtok.nextClean() != '{') {
|
|
throw jtok.syntaxError("A JSONObject text must begin with '{'");
|
|
}
|
|
for (;;) {
|
|
if (parseJSONObject(jtok, pconfig, isInitial)) {
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
finally {
|
|
jtok.setJsonParserConfigurationInternal(originalConfiguration);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Parses entirety of JSON object
|
|
*
|
|
* @param jtok Parses text as tokens
|
|
* @param pconfig Variable to pass parser custom configuration for json parsing.
|
|
* @param isInitial True if start of document, else false
|
|
* @return True if done building object, else false
|
|
*/
|
|
|
|
private boolean parseJSONObject(
|
|
JSONTokener jtok, JSONParserConfiguration pconfig, boolean isInitial) {
|
|
Object obj;
|
|
String key;
|
|
boolean doneParsing = false;
|
|
char c = jtok.nextClean();
|
|
|
|
switch (c) {
|
|
case 0:
|
|
throw jtok.syntaxError("A JSONObject text must end with '}'");
|
|
case '}':
|
|
if (isInitial && pconfig.isStrictMode() && jtok.nextClean() != 0) {
|
|
throw jtok.syntaxError("Strict mode error: Unparsed characters found at end " +
|
|
"of input text");
|
|
}
|
|
return true;
|
|
default:
|
|
obj = jtok.nextSimpleValue(c);
|
|
key = obj.toString();
|
|
}
|
|
|
|
checkKeyForStrictMode(jtok, pconfig, obj);
|
|
|
|
// The key is followed by ':'.
|
|
c = jtok.nextClean();
|
|
if (c != ':') {
|
|
throw jtok.syntaxError("Expected a ':' after a key");
|
|
}
|
|
|
|
// Use syntaxError(..) to include error location
|
|
if (key != null) {
|
|
// Check if key exists
|
|
boolean keyExists = this.opt(key) != null;
|
|
if (keyExists && !pconfig.isOverwriteDuplicateKey()) {
|
|
throw jtok.syntaxError("Duplicate key \"" + key + "\"");
|
|
}
|
|
|
|
Object value = jtok.nextValue();
|
|
// Only add value if non-null
|
|
if (value != null) {
|
|
this.put(key, value);
|
|
}
|
|
}
|
|
|
|
// Pairs are separated by ','.
|
|
if (parseEndOfKeyValuePair(jtok, pconfig, isInitial)) {
|
|
doneParsing = true;
|
|
}
|
|
|
|
return doneParsing;
|
|
}
|
|
|
|
/**
|
|
* Checks for valid end of key:value pair
|
|
* @param jtok Parses text as tokens
|
|
* @param pconfig Variable to pass parser custom configuration for json parsing.
|
|
* @param isInitial True if end of JSON object, else false
|
|
* @return True if end of JSON object, else false
|
|
*/
|
|
|
|
private static boolean parseEndOfKeyValuePair(
|
|
JSONTokener jtok, JSONParserConfiguration pconfig, boolean isInitial) {
|
|
switch (jtok.nextClean()) {
|
|
case ';':
|
|
// In strict mode semicolon is not a valid separator
|
|
if (pconfig.isStrictMode()) {
|
|
throw jtok.syntaxError("Strict mode error: Invalid character ';' found");
|
|
}
|
|
break;
|
|
case ',':
|
|
if (jtok.nextClean() == '}') {
|
|
// trailing commas are not allowed in strict mode
|
|
if (pconfig.isStrictMode()) {
|
|
throw jtok.syntaxError(
|
|
"Strict mode error: Expected another object element");
|
|
}
|
|
// End of JSON object
|
|
return true;
|
|
}
|
|
if (jtok.end()) {
|
|
throw jtok.syntaxError("A JSONObject text must end with '}'");
|
|
}
|
|
jtok.back();
|
|
break;
|
|
case '}':
|
|
if (isInitial && pconfig.isStrictMode() && jtok.nextClean() != 0) {
|
|
throw jtok.syntaxError(
|
|
"Strict mode error: Unparsed characters found at end of input text");
|
|
}
|
|
// End of JSON object
|
|
return true;
|
|
default:
|
|
throw jtok.syntaxError("Expected a ',' or '}'");
|
|
}
|
|
// Not at end of JSON object
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Throws error if key violates strictMode
|
|
* @param jtok Parses text as tokens
|
|
* @param pconfig Variable to pass parser custom configuration for json parsing.
|
|
* @param obj Value to be checked
|
|
*/
|
|
|
|
private static void checkKeyForStrictMode(
|
|
JSONTokener jtok, JSONParserConfiguration pconfig, Object obj) {
|
|
if (pconfig != null && pconfig.isStrictMode()) {
|
|
if(obj instanceof Boolean) {
|
|
throw jtok.syntaxError(String.format(
|
|
"Strict mode error: key '%s' cannot be boolean", obj.toString()));
|
|
}
|
|
if(obj == Null.TYPE) {
|
|
throw jtok.syntaxError(String.format(
|
|
"Strict mode error: key '%s' cannot be null", obj.toString()));
|
|
}
|
|
if(obj instanceof Number) {
|
|
throw jtok.syntaxError(String.format(
|
|
"Strict mode error: key '%s' cannot be number", obj.toString()));
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 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 <code>null</code>
|
|
*/
|
|
|
|
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 pconfig Variable to pass parser custom configuration for json parsing.
|
|
*/
|
|
|
|
public JSONObject(Map<?, ?> m, JSONParserConfiguration pconfig) {
|
|
this(m, 0, pconfig);
|
|
}
|
|
|
|
/**
|
|
* Construct a JSONObject from a map with recursion depth.
|
|
*/
|
|
|
|
public JSONObject(Map<?, ?> m, int recursionDepth, JSONParserConfiguration pconfig) {
|
|
if (recursionDepth > pconfig.getMaxNestingDepth()) {
|
|
throw new JSONException(
|
|
"JSONObject has reached recursion depth limit of " + pconfig.getMaxNestingDepth());
|
|
}
|
|
if (m == null) {
|
|
this.map = new HashMap<String, Object>();
|
|
}
|
|
else {
|
|
this.map = new HashMap<String, Object>(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 || pconfig.isUseNativeNulls()) {
|
|
JSONUtils.testValidity(value);
|
|
this.map.put(String.valueOf(e.getKey()),
|
|
wrapper.wrap(value, recursionDepth + 1, pconfig));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Construct a JSONObject from a source JSON text string. This is the most commonly used JSONObject
|
|
* constructor.
|
|
*
|
|
* @param source A string beginning with <code>{</code> <small>(left brace)</small> and ending
|
|
* with <code>}</code> <small>(right brace)</small>.
|
|
* @exception JSONException If there is a syntax error in the source string or a duplicated key.
|
|
*/
|
|
|
|
public JSONObject(String source) throws JSONException {
|
|
this(source, new JSONParserConfiguration());
|
|
}
|
|
|
|
/**
|
|
* Construct a JSONObject from a source JSON text string with custom json parse configurations.
|
|
* This is the most commonly used JSONObject constructor.
|
|
*
|
|
* @param source A string beginning with <code>{</code> <small>(left brace)</small> and ending
|
|
* with <code>}</code> <small>(right brace)</small>.
|
|
* @param pconfig Variable to pass parser custom configuration for json parsing.
|
|
* @exception JSONException If there is a syntax error in the source string or a duplicated key.
|
|
*/
|
|
|
|
public JSONObject(String source, JSONParserConfiguration pconfig) throws JSONException {
|
|
this(new JSONTokener(source, pconfig), pconfig);
|
|
}
|
|
|
|
/**
|
|
* Constructor to specify an initial capacity of the internal map. Useful for library internal calls
|
|
* where we know, or at least can best guess, how big this JSONObject will be.
|
|
*
|
|
* @param initialCapacity initial capacity of the internal map.
|
|
*/
|
|
|
|
protected JSONObject(int initialCapacity){
|
|
this.map = new HashMap<String, Object>(initialCapacity);
|
|
}
|
|
|
|
/**
|
|
* 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 JSONArray is stored under the key to hold all of the
|
|
* accumulated values. If there is already a JSONArray, then the new value is appended to it. In
|
|
* contrast, the put method replaces the previous value.
|
|
* <p>
|
|
* If only one value is accumulated that is not a JSONArray, then the result will be the same as
|
|
* using put. But if multiple values are accumulated, then the result will be like append.
|
|
*
|
|
* @param key A key string.
|
|
* @param value An object to be accumulated under the key.
|
|
* @return this.
|
|
* @throws JSONException If the value is non-finite number.
|
|
* @throws NullPointerException If the key is <code>null</code>.
|
|
*/
|
|
|
|
public JSONObject accumulate(String key, Object value) throws JSONException {
|
|
JSONUtils.testValidity(value);
|
|
Object object = this.opt(key);
|
|
if (object == null) {
|
|
this.put(key,
|
|
value instanceof JSONArray ? new JSONArray().put(value) : value);
|
|
}
|
|
else if (object instanceof JSONArray) {
|
|
((JSONArray) object).put(value);
|
|
}
|
|
else {
|
|
this.put(key, new JSONArray().put(object).put(value));
|
|
}
|
|
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 * JSONArray containing the value parameter. If
|
|
* the key was already associated with a JSONArray, then the value parameter is appended to it.
|
|
*
|
|
* @param key A key string.
|
|
* @param value An object to be accumulated under the key.
|
|
* @return this.
|
|
* @throws JSONException If the value is non-finite number or if the current value associated with
|
|
* the key is not a JSONArray.
|
|
* @throws NullPointerException If the key is <code>null</code>.
|
|
*/
|
|
|
|
public JSONObject append(String key, Object value) throws JSONException {
|
|
JSONUtils.testValidity(value);
|
|
Object object = this.opt(key);
|
|
if (object == null) {
|
|
this.put(key, new JSONArray().put(value));
|
|
} else if (object instanceof JSONArray) {
|
|
this.put(key, ((JSONArray) object).put(value));
|
|
} else {
|
|
throw wrongValueFormatException(key, "JSONArray", null, null);
|
|
}
|
|
return this;
|
|
}
|
|
|
|
/**
|
|
* Produce a string from a double. The string "null" will be returned if the number is not finite.
|
|
*
|
|
* @param d A double.
|
|
* @return A String.
|
|
*/
|
|
|
|
public static String doubleToString(double d) {
|
|
if (Double.isInfinite(d) || Double.isNaN(d)) {
|
|
return "null";
|
|
}
|
|
|
|
// Shave off trailing zeros and decimal point, if possible.
|
|
String string = Double.toString(d);
|
|
// idx = 0 case is covered by behavior of Double.toString()
|
|
if (string.indexOf('.') > 0 && string.indexOf('e') < 0
|
|
&& string.indexOf('E') < 0) {
|
|
while (string.endsWith("0")) {
|
|
string = string.substring(0, string.length() - 1);
|
|
}
|
|
if (string.endsWith(".")) {
|
|
string = string.substring(0, string.length() - 1);
|
|
}
|
|
}
|
|
return string;
|
|
}
|
|
|
|
/**
|
|
* 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.");
|
|
}
|
|
Object object = this.opt(key);
|
|
if (object == null) {
|
|
throw new JSONException("JSONObject[" + JSONUtils.quote(key) + "] not found.");
|
|
}
|
|
return object;
|
|
}
|
|
|
|
/**
|
|
* Get the enum value associated with a key.
|
|
*
|
|
* @param <E> Enum Type
|
|
* @param clazz The type of enum to retrieve.
|
|
* @param key A key string.
|
|
* @return The enum value associated with the key
|
|
* @throws JSONException if the key is not found or if the value cannot be converted to an enum.
|
|
*/
|
|
|
|
public <E extends Enum<E>> E getEnum(Class<E> clazz, String key) throws JSONException {
|
|
E val = optEnum(clazz, key);
|
|
if(val==null) {
|
|
// JSONException should really take a throwable argument.
|
|
// If it did, I would re-implement this with the Enum.valueOf
|
|
// method and place any thrown exception in the JSONException
|
|
throw wrongValueFormatException(key, "enum of type "
|
|
+ JSONUtils.quote(clazz.getSimpleName()), opt(key), null);
|
|
}
|
|
return val;
|
|
}
|
|
|
|
/**
|
|
* 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 {
|
|
Object object = this.get(key);
|
|
if (Boolean.FALSE.equals(object)
|
|
|| (object instanceof String && "false".equalsIgnoreCase((String) object))) {
|
|
return false;
|
|
} else if (Boolean.TRUE.equals(object)
|
|
|| (object instanceof String && "true".equalsIgnoreCase((String) object))) {
|
|
return true;
|
|
}
|
|
throw wrongValueFormatException(key, "Boolean", object, null);
|
|
}
|
|
|
|
/**
|
|
* Get the BigInteger 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 cannot be converted to BigInteger.
|
|
*/
|
|
|
|
public BigInteger getBigInteger(String key) throws JSONException {
|
|
return this.getBigInteger(key, new JSONParserConfiguration());
|
|
}
|
|
|
|
/**
|
|
* Get the BigInteger value associated with a key.
|
|
*
|
|
* @param key A key string.
|
|
* @param pconfig A configuration whose {@code maxNumberLength} bounds the number of decimal digits
|
|
* in the returned integer. Values exceeding this length are treated as unconvertible. Pass a
|
|
* configuration with {@link ParserConfiguration#UNDEFINED_MAXIMUM_NUMBER_LENGTH} to disable this
|
|
* check.
|
|
*
|
|
* @return The numeric value.
|
|
* @throws JSONException if the key is not found or if the value cannot be converted to BigInteger.
|
|
*/
|
|
|
|
public BigInteger getBigInteger(String key, JSONParserConfiguration pconfig)
|
|
throws JSONException {
|
|
Object object = this.get(key);
|
|
BigInteger ret = JSONUtils.objectToBigInteger(object, null, pconfig);
|
|
if (ret != null) {
|
|
return ret;
|
|
}
|
|
throw wrongValueFormatException(key, "BigInteger", object, null);
|
|
}
|
|
|
|
/**
|
|
* Get the BigDecimal value associated with a key. If the value is float or double, the
|
|
* {@link BigDecimal#BigDecimal(double)} constructor will be used. See notes on the constructor for
|
|
* conversion issues that may arise.
|
|
*
|
|
* @param key A key string.
|
|
* @return The numeric value.
|
|
* @throws JSONException if the key is not found or if the value cannot be converted to BigDecimal.
|
|
*/
|
|
|
|
public BigDecimal getBigDecimal(String key) throws JSONException {
|
|
Object object = this.get(key);
|
|
BigDecimal ret = JSONUtils.objectToBigDecimal(object, null);
|
|
if (ret != null) {
|
|
return ret;
|
|
}
|
|
throw wrongValueFormatException(key, "BigDecimal", object, null);
|
|
}
|
|
|
|
/**
|
|
* 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);
|
|
if(object instanceof Number) {
|
|
return ((Number)object).doubleValue();
|
|
}
|
|
try {
|
|
return Double.parseDouble(object.toString());
|
|
}
|
|
catch (Exception e) {
|
|
throw wrongValueFormatException(key, "double", object, e);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get the float 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 float getFloat(String key) throws JSONException {
|
|
final Object object = this.get(key);
|
|
if(object instanceof Number) {
|
|
return ((Number)object).floatValue();
|
|
}
|
|
try {
|
|
return Float.parseFloat(object.toString());
|
|
}
|
|
catch (Exception e) {
|
|
throw wrongValueFormatException(key, "float", object, e);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get the Number 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 Number getNumber(String key) throws JSONException {
|
|
Object object = this.get(key);
|
|
try {
|
|
if (object instanceof Number) {
|
|
return (Number)object;
|
|
}
|
|
return JSONUtils.stringToNumber(object.toString());
|
|
}
|
|
catch (Exception e) {
|
|
throw wrongValueFormatException(key, "number", object, e);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 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);
|
|
if(object instanceof Number) {
|
|
return ((Number)object).intValue();
|
|
}
|
|
try {
|
|
return Integer.parseInt(object.toString());
|
|
}
|
|
catch (Exception e) {
|
|
throw wrongValueFormatException(key, "int", object, e);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get the JSONArray value associated with a key.
|
|
*
|
|
* @param key A key string.
|
|
* @return A JSONArray which is the value.
|
|
* @throws JSONException if the key is not found or if the value is not a JSONArray.
|
|
*/
|
|
|
|
public JSONArray getJSONArray(String key) throws JSONException {
|
|
Object object = this.get(key);
|
|
if (object instanceof JSONArray) {
|
|
return (JSONArray) object;
|
|
}
|
|
throw wrongValueFormatException(key, "JSONArray", object, null);
|
|
}
|
|
|
|
/**
|
|
* 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 {
|
|
Object object = this.get(key);
|
|
if (object instanceof JSONObject) {
|
|
return (JSONObject) object;
|
|
}
|
|
throw wrongValueFormatException(key, "JSONObject", object, null);
|
|
}
|
|
|
|
/**
|
|
* 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);
|
|
if(object instanceof Number) {
|
|
return ((Number)object).longValue();
|
|
}
|
|
try {
|
|
return Long.parseLong(object.toString());
|
|
}
|
|
catch (Exception e) {
|
|
throw wrongValueFormatException(key, "long", object, e);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get an array of field names from a JSONObject.
|
|
*
|
|
* @param jo JSON object
|
|
* @return An array of field names, or null if there are no names.
|
|
*/
|
|
|
|
public static String[] getNames(JSONObject jo) {
|
|
if (jo.isEmpty()) {
|
|
return null;
|
|
}
|
|
return jo.keySet().toArray(new String[jo.length()]);
|
|
}
|
|
|
|
/**
|
|
* Get an array of public field names from an Object.
|
|
*
|
|
* @param object object to read
|
|
* @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<?> 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.
|
|
*
|
|
* @param key A key string.
|
|
* @return A string which is the value.
|
|
* @throws JSONException if there is no string value for the key.
|
|
*/
|
|
|
|
public String getString(String key) throws JSONException {
|
|
Object object = this.get(key);
|
|
if (object instanceof String) {
|
|
return (String) object;
|
|
}
|
|
throw wrongValueFormatException(key, "string", object, null);
|
|
}
|
|
|
|
/**
|
|
* Determine if the JSONObject contains a specific key.
|
|
*
|
|
* @param key A key string.
|
|
* @return true if the key exists in the JSONObject.
|
|
*/
|
|
|
|
public boolean has(String 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
|
|
* (Integer). If there is such a property, and if it is an Integer, Long, Double, Float, BigInteger,
|
|
* or BigDecimal then add one to it. No overflow bounds checking is performed, so callers should
|
|
* initialize the key prior to this call with an appropriate type that can handle the maximum
|
|
* expected value.
|
|
*
|
|
* @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 {
|
|
Object value = this.opt(key);
|
|
switch (value) {
|
|
case
|
|
null -> this.put(key, 1);
|
|
case
|
|
Integer i -> this.put(key, i + 1);
|
|
case
|
|
Long l -> this.put(key, l + 1L);
|
|
case
|
|
BigInteger bigInteger -> this.put(key, bigInteger.add(BigInteger.ONE));
|
|
case
|
|
Float v -> this.put(key, v + 1.0f);
|
|
case
|
|
Double v -> this.put(key, v + 1.0d);
|
|
case
|
|
BigDecimal bigDecimal -> this.put(key, bigDecimal.add(BigDecimal.ONE));
|
|
default ->
|
|
throw new JSONException("Unable to increment [" + JSONUtils.quote(key) + "].");
|
|
}
|
|
return this;
|
|
}
|
|
|
|
/**
|
|
* Determine if the value associated with the key is <code>null</code> 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 Null.TYPE
|
|
* object.
|
|
*/
|
|
|
|
public boolean isNull(String key) {
|
|
return Null.TYPE.equals(this.opt(key));
|
|
}
|
|
|
|
/**
|
|
* Get an enumeration of the keys of the JSONObject. Modifying this key Set will also modify the
|
|
* JSONObject. Use with caution.
|
|
*
|
|
* @see Set#iterator()
|
|
*
|
|
* @return An iterator of the keys.
|
|
*/
|
|
|
|
public Iterator<String> keys() {
|
|
return this.keySet().iterator();
|
|
}
|
|
|
|
/**
|
|
* Get a set of keys of the JSONObject. Modifying this key Set will also modify the JSONObject. Use
|
|
* with caution.
|
|
*
|
|
* @see Map#keySet()
|
|
*
|
|
* @return A keySet.
|
|
*/
|
|
|
|
public Set<String> keySet() {
|
|
return this.map.keySet();
|
|
}
|
|
|
|
/**
|
|
* Get a set of entries of the JSONObject. These are raw values and may not match what is returned
|
|
* by the JSONObject get* and opt* functions. Modifying the returned EntrySet or the Entry objects
|
|
* contained therein will modify the backing JSONObject. This does not return a clone or a read-only
|
|
* view. Use with caution.
|
|
*
|
|
* @see Map#entrySet()
|
|
*
|
|
* @return An Entry Set
|
|
*/
|
|
|
|
protected Set<Entry<String, Object>> entrySet() {
|
|
return this.map.entrySet();
|
|
}
|
|
|
|
/**
|
|
* Get the number of keys stored in the JSONObject.
|
|
*
|
|
* @return The number of keys in the JSONObject.
|
|
*/
|
|
|
|
public int length() {
|
|
return this.map.size();
|
|
}
|
|
|
|
/**
|
|
* Removes all of the elements from this JSONObject. The JSONObject will be empty after this call
|
|
* returns.
|
|
*/
|
|
|
|
public void clear() {
|
|
this.map.clear();
|
|
}
|
|
|
|
/**
|
|
* Check if JSONObject is empty.
|
|
*
|
|
* @return true if JSONObject is empty, otherwise false.
|
|
*/
|
|
|
|
public boolean isEmpty() {
|
|
return this.map.isEmpty();
|
|
}
|
|
|
|
/**
|
|
* Produce a JSONArray containing the names of the elements of this JSONObject.
|
|
*
|
|
* @return A JSONArray containing the key strings, or null if the JSONObject is empty.
|
|
*/
|
|
|
|
public JSONArray names() {
|
|
if(this.map.isEmpty()) {
|
|
return null;
|
|
}
|
|
return new JSONArray(this.map.keySet());
|
|
}
|
|
|
|
/**
|
|
* Get an optional value associated with a key.
|
|
*
|
|
* @param key A key string.
|
|
* @return An object which is the value, or null if there is no value.
|
|
*/
|
|
|
|
public Object opt(String key) {
|
|
return key == null ? null : this.map.get(key);
|
|
}
|
|
|
|
/**
|
|
* Get the enum value associated with a key.
|
|
*
|
|
* @param <E> Enum Type
|
|
* @param clazz The type of enum to retrieve.
|
|
* @param key A key string.
|
|
* @return The enum value associated with the key or null if not found
|
|
*/
|
|
|
|
public <E extends Enum<E>> E optEnum(Class<E> clazz, String key) {
|
|
return this.optEnum(clazz, key, null);
|
|
}
|
|
|
|
/**
|
|
* Get the enum value associated with a key.
|
|
*
|
|
* @param <E> Enum Type
|
|
* @param clazz The type of enum to retrieve.
|
|
* @param key A key string.
|
|
* @param defaultValue The default in case the value is not found
|
|
* @return The enum value associated with the key or defaultValue if the value is not found or
|
|
* cannot be assigned to <code>clazz</code>
|
|
*/
|
|
|
|
public <E extends Enum<E>> E optEnum(Class<E> clazz, String key, E defaultValue) {
|
|
try {
|
|
Object val = this.opt(key);
|
|
if (Null.TYPE.equals(val)) {
|
|
return defaultValue;
|
|
}
|
|
if (clazz.isAssignableFrom(val.getClass())) {
|
|
// we just checked it!
|
|
@SuppressWarnings("unchecked")
|
|
E myE = (E) val;
|
|
return myE;
|
|
}
|
|
return Enum.valueOf(clazz, val.toString());
|
|
}
|
|
catch (
|
|
IllegalArgumentException | NullPointerException e) {
|
|
return defaultValue;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 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 a Boolean or the String "true" or "false" (case-insensitive).
|
|
*
|
|
* @param key A key string.
|
|
* @param defaultValue The default.
|
|
* @return The truth.
|
|
*/
|
|
|
|
public boolean optBoolean(String key, boolean defaultValue) {
|
|
Object val = this.opt(key);
|
|
if (Null.TYPE.equals(val)) {
|
|
return defaultValue;
|
|
}
|
|
if (val instanceof Boolean){
|
|
return (Boolean) val;
|
|
}
|
|
try {
|
|
// we'll use the get anyway because it does string conversion.
|
|
return this.getBoolean(key);
|
|
}
|
|
catch (Exception e) {
|
|
return defaultValue;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get an optional boolean object 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 optBooleanObject(String key) {
|
|
return this.optBooleanObject(key, false);
|
|
}
|
|
|
|
/**
|
|
* Get an optional boolean object associated with a key. It returns the defaultValue if there is no
|
|
* such key, or if it is not a Boolean or the String "true" or "false" (case-insensitive).
|
|
*
|
|
* @param key A key string.
|
|
* @param defaultValue The default.
|
|
* @return The truth.
|
|
*/
|
|
|
|
public Boolean optBooleanObject(String key, Boolean defaultValue) {
|
|
Object val = this.opt(key);
|
|
if (Null.TYPE.equals(val)) {
|
|
return defaultValue;
|
|
}
|
|
if (val instanceof Boolean){
|
|
return (Boolean) val;
|
|
}
|
|
try {
|
|
// we'll use the get anyway because it does string conversion.
|
|
return this.getBoolean(key);
|
|
}
|
|
catch (Exception e) {
|
|
return defaultValue;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get an optional BigDecimal associated with a key, or the defaultValue 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. If the value is float or double, then the {@link BigDecimal#BigDecimal(double)}
|
|
* constructor will be used. See notes on the constructor for conversion issues that may arise.
|
|
*
|
|
* @param key A key string.
|
|
* @param defaultValue The default.
|
|
* @return An object which is the value.
|
|
*/
|
|
|
|
public BigDecimal optBigDecimal(String key, BigDecimal defaultValue) {
|
|
Object val = this.opt(key);
|
|
return JSONUtils.objectToBigDecimal(val, defaultValue);
|
|
}
|
|
|
|
/**
|
|
* Get an optional BigInteger associated with a key, or the defaultValue 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 key string.
|
|
* @param defaultValue The default.
|
|
* @return An object which is the value.
|
|
*/
|
|
public BigInteger optBigInteger(String key, BigInteger defaultValue) {
|
|
return this.optBigInteger(key, defaultValue, new JSONParserConfiguration());
|
|
}
|
|
|
|
/**
|
|
* Get an optional BigInteger associated with a key, or the defaultValue 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 key string.
|
|
* @param defaultValue The default.
|
|
* @param pconfig A configuration whose {@code maxNumberLength} bounds the number of decimal digits
|
|
* in the returned integer. Values exceeding this length are treated as unconvertible and
|
|
* {@code defaultValue} is returned. Pass a configuration with
|
|
* {@link ParserConfiguration#UNDEFINED_MAXIMUM_NUMBER_LENGTH} to disable this check.
|
|
* @return An object which is the value.
|
|
*/
|
|
|
|
public BigInteger optBigInteger(String key, BigInteger defaultValue,
|
|
JSONParserConfiguration pconfig) {
|
|
Object val = this.opt(key);
|
|
return JSONUtils.objectToBigInteger(val, defaultValue, pconfig);
|
|
}
|
|
|
|
/**
|
|
* 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. If the value is a string, an attempt will be made to evaluate it as a
|
|
* number.
|
|
*
|
|
* @param key A key string.
|
|
* @param defaultValue The default.
|
|
* @return An object which is the value.
|
|
*/
|
|
|
|
public double optDouble(String key, double defaultValue) {
|
|
Number val = this.optNumber(key);
|
|
if (val == null) {
|
|
return defaultValue;
|
|
}
|
|
return val.doubleValue();
|
|
}
|
|
|
|
/**
|
|
* Get an optional Double object 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 optDoubleObject(String key) {
|
|
return this.optDoubleObject(key, Double.NaN);
|
|
}
|
|
|
|
/**
|
|
* Get an optional Double object associated with a key, or the defaultValue 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 key string.
|
|
* @param defaultValue The default.
|
|
* @return An object which is the value.
|
|
*/
|
|
|
|
public Double optDoubleObject(String key, Double defaultValue) {
|
|
Number val = this.optNumber(key);
|
|
if (val == null) {
|
|
return defaultValue;
|
|
}
|
|
return val.doubleValue();
|
|
}
|
|
|
|
/**
|
|
* Get the optional float value associated with an index. NaN is returned if there is no value for
|
|
* the index, or if the value is not a number and cannot be converted to a number.
|
|
*
|
|
* @param key A key string.
|
|
* @return The value.
|
|
*/
|
|
|
|
public float optFloat(String key) {
|
|
return this.optFloat(key, Float.NaN);
|
|
}
|
|
|
|
/**
|
|
* Get the optional float value associated with an index. The defaultValue is returned if there is
|
|
* no value for the index, or if the value is not a number and cannot be converted to a number.
|
|
*
|
|
* @param key A key string.
|
|
* @param defaultValue The default value.
|
|
* @return The value.
|
|
*/
|
|
|
|
public float optFloat(String key, float defaultValue) {
|
|
Number val = this.optNumber(key);
|
|
if (val == null) {
|
|
return defaultValue;
|
|
}
|
|
return val.floatValue();
|
|
}
|
|
|
|
/**
|
|
* Get the optional Float object associated with an index. NaN is returned if there is no value for
|
|
* the index, or if the value is not a number and cannot be converted to a number.
|
|
*
|
|
* @param key A key string.
|
|
* @return The object.
|
|
*/
|
|
|
|
public Float optFloatObject(String key) {
|
|
return this.optFloatObject(key, Float.NaN);
|
|
}
|
|
|
|
/**
|
|
* Get the optional Float object associated with an index. The defaultValue is returned if there is
|
|
* no value for the index, or if the value is not a number and cannot be converted to a number.
|
|
*
|
|
* @param key A key string.
|
|
* @param defaultValue The default object.
|
|
* @return The object.
|
|
*/
|
|
|
|
public Float optFloatObject(String key, Float defaultValue) {
|
|
Number val = this.optNumber(key);
|
|
if (val == null) {
|
|
return defaultValue;
|
|
}
|
|
return val.floatValue();
|
|
}
|
|
|
|
/**
|
|
* 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. If the value is a string, an attempt will be made to evaluate it as a
|
|
* number.
|
|
*
|
|
* @param key A key string.
|
|
* @param defaultValue The default.
|
|
* @return An object which is the value.
|
|
*/
|
|
|
|
public int optInt(String key, int defaultValue) {
|
|
final Number val = this.optNumber(key, null);
|
|
if (val == null) {
|
|
return defaultValue;
|
|
}
|
|
return val.intValue();
|
|
}
|
|
|
|
/**
|
|
* Get an optional Integer object 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 Integer optIntegerObject(String key) {
|
|
return this.optIntegerObject(key, 0);
|
|
}
|
|
|
|
/**
|
|
* Get an optional Integer object associated with a key, or the default 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.
|
|
* @param defaultValue The default.
|
|
* @return An object which is the value.
|
|
*/
|
|
|
|
public Integer optIntegerObject(String key, Integer defaultValue) {
|
|
final Number val = this.optNumber(key, null);
|
|
if (val == null) {
|
|
return defaultValue;
|
|
}
|
|
return val.intValue();
|
|
}
|
|
|
|
/**
|
|
* 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) {
|
|
return this.optJSONArray(key, null);
|
|
}
|
|
|
|
/**
|
|
* Get an optional JSONArray associated with a key, or the default if there is no such key, or if
|
|
* its value is not a JSONArray.
|
|
*
|
|
* @param key A key string.
|
|
* @param defaultValue The default.
|
|
* @return A JSONArray which is the value.
|
|
*/
|
|
|
|
public JSONArray optJSONArray(String key, JSONArray defaultValue) {
|
|
Object object = this.opt(key);
|
|
return object instanceof JSONArray ? (JSONArray) object : defaultValue;
|
|
}
|
|
|
|
/**
|
|
* Get an optional JSONObject associated with a key. It returns null if there is no such key, or if
|
|
* its value is not a JSONObject.
|
|
*
|
|
* @param key A key string.
|
|
* @return A JSONObject which is the value.
|
|
*/
|
|
|
|
public JSONObject optJSONObject(String key) { return this.optJSONObject(key, null); }
|
|
|
|
/**
|
|
* Get an optional JSONObject associated with a key, or the default if there is no such key or if
|
|
* the value is not a JSONObject.
|
|
*
|
|
* @param key A key string.
|
|
* @param defaultValue The default.
|
|
* @return An JSONObject which is the value.
|
|
*/
|
|
|
|
public JSONObject optJSONObject(String key, JSONObject defaultValue) {
|
|
Object object = this.opt(key);
|
|
return object instanceof JSONObject ? (JSONObject) object : defaultValue;
|
|
}
|
|
|
|
/**
|
|
* 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. If the value is a string, an attempt will be made to evaluate it as a
|
|
* number.
|
|
*
|
|
* @param key A key string.
|
|
* @param defaultValue The default.
|
|
* @return An object which is the value.
|
|
*/
|
|
|
|
public long optLong(String key, long defaultValue) {
|
|
final Number val = this.optNumber(key, null);
|
|
if (val == null) {
|
|
return defaultValue;
|
|
}
|
|
|
|
return val.longValue();
|
|
}
|
|
|
|
/**
|
|
* Get an optional Long object 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 optLongObject(String key) {
|
|
return this.optLongObject(key, 0L);
|
|
}
|
|
|
|
/**
|
|
* Get an optional Long object associated with a key, or the default 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.
|
|
* @param defaultValue The default.
|
|
* @return An object which is the value.
|
|
*/
|
|
|
|
public Long optLongObject(String key, Long defaultValue) {
|
|
final Number val = this.optNumber(key, null);
|
|
if (val == null) {
|
|
return defaultValue;
|
|
}
|
|
return val.longValue();
|
|
}
|
|
|
|
/**
|
|
* Get an optional {@link Number} value associated with a key, or <code>null</code> 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 ({@link BigDecimal}). This method would be used in cases where type
|
|
* coercion of the number value is unwanted.
|
|
*
|
|
* @param key A key string.
|
|
* @return An object which is the value.
|
|
*/
|
|
|
|
public Number optNumber(String key) {
|
|
return this.optNumber(key, null);
|
|
}
|
|
|
|
/**
|
|
* Get an optional {@link Number} value associated with a key, or the default 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. This method would be used in cases where type coercion of the number
|
|
* value is unwanted.
|
|
*
|
|
* @param key A key string.
|
|
* @param defaultValue The default.
|
|
* @return An object which is the value.
|
|
*/
|
|
|
|
public Number optNumber(String key, Number defaultValue) {
|
|
Object val = this.opt(key);
|
|
if (Null.TYPE.equals(val)) {
|
|
return defaultValue;
|
|
}
|
|
if (val instanceof Number){
|
|
return (Number) val;
|
|
}
|
|
|
|
try {
|
|
return JSONUtils.stringToNumber(val.toString());
|
|
}
|
|
catch (Exception e) {
|
|
return defaultValue;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 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.
|
|
*
|
|
* @param key A key string.
|
|
* @param defaultValue The default.
|
|
* @return A string which is the value.
|
|
*/
|
|
|
|
public String optString(String key, String defaultValue) {
|
|
Object object = this.opt(key);
|
|
return Null.TYPE.equals(object) ? defaultValue : object.toString();
|
|
}
|
|
|
|
/**
|
|
* Put a key/boolean pair in the JSONObject.
|
|
*
|
|
* @param key A key string.
|
|
* @param value A boolean which is the value.
|
|
* @return this.
|
|
* @throws JSONException If the value is non-finite number.
|
|
* @throws NullPointerException If the key is <code>null</code>.
|
|
*/
|
|
|
|
public JSONObject put(String key, boolean value) throws JSONException {
|
|
return this.put(key, value ? Boolean.TRUE : Boolean.FALSE);
|
|
}
|
|
|
|
/**
|
|
* Put a key/value pair in the JSONObject, where the value will be a JSONArray which is produced
|
|
* from a Collection.
|
|
*
|
|
* @param key A key string.
|
|
* @param value A Collection value.
|
|
* @return this.
|
|
* @throws JSONException If the value is non-finite number.
|
|
* @throws NullPointerException If the key is <code>null</code>.
|
|
*/
|
|
|
|
public JSONObject put(String key, Collection<?> value) throws JSONException {
|
|
return this.put(key, new JSONArray(value));
|
|
}
|
|
|
|
/**
|
|
* Put a key/double pair in the JSONObject.
|
|
*
|
|
* @param key A key string.
|
|
* @param value A double which is the value.
|
|
* @return this.
|
|
* @throws JSONException If the value is non-finite number.
|
|
* @throws NullPointerException If the key is <code>null</code>.
|
|
*/
|
|
|
|
public JSONObject put(String key, double value) throws JSONException {
|
|
return this.put(key, Double.valueOf(value));
|
|
}
|
|
|
|
/**
|
|
* Put a key/float pair in the JSONObject.
|
|
*
|
|
* @param key A key string.
|
|
* @param value A float which is the value.
|
|
* @return this.
|
|
* @throws JSONException If the value is non-finite number.
|
|
* @throws NullPointerException If the key is <code>null</code>.
|
|
*/
|
|
|
|
public JSONObject put(String key, float value) throws JSONException {
|
|
return this.put(key, Float.valueOf(value));
|
|
}
|
|
|
|
/**
|
|
* Put a key/int pair in the JSONObject.
|
|
*
|
|
* @param key A key string.
|
|
* @param value An int which is the value.
|
|
* @return this.
|
|
* @throws JSONException If the value is non-finite number.
|
|
* @throws NullPointerException If the key is <code>null</code>.
|
|
*/
|
|
|
|
public JSONObject put(String key, int value) throws JSONException {
|
|
return this.put(key, Integer.valueOf(value));
|
|
}
|
|
|
|
/**
|
|
* Put a key/long pair in the JSONObject.
|
|
*
|
|
* @param key A key string.
|
|
* @param value A long which is the value.
|
|
* @return this.
|
|
* @throws JSONException If the value is non-finite number.
|
|
* @throws NullPointerException If the key is <code>null</code>.
|
|
*/
|
|
|
|
public JSONObject put(String key, long value) throws JSONException {
|
|
return this.put(key, Long.valueOf(value));
|
|
}
|
|
|
|
/**
|
|
* Put a key/value pair in the JSONObject, where the value will be a JSONObject which is produced
|
|
* from a Map.
|
|
*
|
|
* @param key A key string.
|
|
* @param value A Map value.
|
|
* @return this.
|
|
* @throws JSONException If the value is non-finite number.
|
|
* @throws NullPointerException If the key is <code>null</code>.
|
|
*/
|
|
|
|
public JSONObject put(String key, Map<?, ?> value) throws JSONException {
|
|
return this.put(key, new JSONObject(value));
|
|
}
|
|
|
|
/**
|
|
* Put a key/value pair in the JSONObject. If the value is <code>null</code>, then the key will be
|
|
* removed from the JSONObject if it is present.
|
|
*
|
|
* @param key A key string.
|
|
* @param value types: Boolean, Double, Integer, JSONArray, JSONObject, Long, String, or the
|
|
* JSONObject.NULL object.
|
|
* @return this.
|
|
* @throws JSONException If the value is non-finite number.
|
|
* @throws NullPointerException If the key is <code>null</code>.
|
|
*/
|
|
|
|
public JSONObject put(String key, Object value) throws JSONException {
|
|
if (key == null) {
|
|
throw new NullPointerException("Null key.");
|
|
}
|
|
if (value != null) {
|
|
JSONUtils.testValidity(value);
|
|
this.map.put(key, value);
|
|
}
|
|
else {
|
|
this.remove(key);
|
|
}
|
|
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 with that name.
|
|
*
|
|
* @param key key to insert into
|
|
* @param value value to insert
|
|
* @return this.
|
|
* @throws JSONException if the key is a duplicate
|
|
*/
|
|
|
|
public JSONObject putOnce(String key, Object value) throws JSONException {
|
|
if (key != null && value != null) {
|
|
if (this.opt(key) != null) {
|
|
throw new JSONException("Duplicate key \"" + key + "\"");
|
|
}
|
|
return this.put(key, value);
|
|
}
|
|
return this;
|
|
}
|
|
|
|
/**
|
|
* Put a key/value pair in the JSONObject, but only if the key and the value are both non-null.
|
|
*
|
|
* @param key A key string.
|
|
* @param value An object which is the value. It should be of one of these types: Boolean, Double,
|
|
* Integer, JSONArray, JSONObject, Long, String, or the JSONObject.NULL object.
|
|
* @return this.
|
|
* @throws JSONException If the value is a non-finite number.
|
|
*/
|
|
|
|
public JSONObject putOpt(String key, Object value) throws JSONException {
|
|
if (key != null && value != null) {
|
|
return this.put(key, value);
|
|
}
|
|
return this;
|
|
}
|
|
|
|
/**
|
|
* Creates a JSONPointer using an initialization string and tries to match it to an item within this
|
|
* JSONObject. For example, given a JSONObject initialized with this document:
|
|
* <pre>
|
|
* {
|
|
* "a":{"b":"c"}
|
|
* }
|
|
* </pre>
|
|
* and this JSONPointer string:
|
|
* <pre>
|
|
* "/a/b"
|
|
* </pre>
|
|
* 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 query(new JSONPointer(jsonPointer));
|
|
}
|
|
|
|
/**
|
|
* Uses a user initialized JSONPointer and tries to match it to an item within this JSONObject. For
|
|
* example, given a JSONObject initialized with this document:
|
|
* <pre>
|
|
* {
|
|
* "a":{"b":"c"}
|
|
* }
|
|
* </pre>
|
|
* and this JSONPointer:
|
|
* <pre>
|
|
* "/a/b"
|
|
* </pre>
|
|
* 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(JSONPointer jsonPointer) {
|
|
return 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) {
|
|
return optQuery(new JSONPointer(jsonPointer));
|
|
}
|
|
|
|
/**
|
|
* 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 JSON pointer
|
|
* @return the queried value or {@code null}
|
|
* @throws IllegalArgumentException if {@code jsonPointer} has invalid syntax
|
|
*/
|
|
|
|
public Object optQuery(JSONPointer jsonPointer) {
|
|
try {
|
|
return jsonPointer.queryFrom(this);
|
|
}
|
|
catch (JSONPointerException e) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Remove a name and its value, if present.
|
|
*
|
|
* @param key The name to be removed.
|
|
* @return The value that was associated with the name, or null if there was no value.
|
|
*/
|
|
|
|
public Object remove(String key) {
|
|
return this.map.remove(key);
|
|
}
|
|
|
|
/**
|
|
* Determine if two JSONObjects are similar. They must contain the same set of names which must be
|
|
* associated with similar values.
|
|
*
|
|
* @param other The other JSONObject
|
|
* @return true if they are equal
|
|
*/
|
|
|
|
public boolean similar(Object other) {
|
|
try {
|
|
if (!(other instanceof JSONObject)) {
|
|
return false;
|
|
}
|
|
if (!this.keySet().equals(((JSONObject)other).keySet())) {
|
|
return false;
|
|
}
|
|
return checkSimilarEntries(other);
|
|
}
|
|
catch (Exception e) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
private boolean checkSimilarEntries(Object other) {
|
|
for (final Entry<String,?> entry : this.entrySet()) {
|
|
String name = entry.getKey();
|
|
Object valueThis = entry.getValue();
|
|
Object valueOther = ((JSONObject)other).get(name);
|
|
if(valueThis == valueOther) {
|
|
continue;
|
|
}
|
|
if(valueThis == null) {
|
|
return false;
|
|
}
|
|
|
|
if (!checkObjectType(valueThis, valueOther)) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Convenience function. Compares types of two objects.
|
|
*
|
|
* @param valueThis Object whose type is being checked
|
|
* @param valueOther Reference object
|
|
* @return true if match, else false
|
|
*/
|
|
|
|
private boolean checkObjectType(Object valueThis, Object valueOther) {
|
|
if (valueThis instanceof JSONObject) {
|
|
return ((JSONObject)valueThis).similar(valueOther);
|
|
}
|
|
else if (valueThis instanceof JSONArray) {
|
|
return ((JSONArray)valueThis).similar(valueOther);
|
|
}
|
|
else if (valueThis instanceof Number && valueOther instanceof Number) {
|
|
return JSONUtils.isNumberSimilar((Number)valueThis, (Number)valueOther);
|
|
}
|
|
else if (valueThis instanceof JSONString && valueOther instanceof JSONString) {
|
|
return ((JSONString) valueThis).toJSONString().equals(((JSONString) valueOther).toJSONString());
|
|
}
|
|
else if (!valueThis.equals(valueOther)) {
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
|
|
/**
|
|
* 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.isEmpty()) {
|
|
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.
|
|
* <p><b>
|
|
* Warning: This method assumes that the data structure is acyclical.
|
|
* </b>
|
|
*
|
|
* @return a printable, displayable, portable, transmittable representation of the object, beginning
|
|
* with <code>{</code> <small>(left brace)</small> and ending with <code>}</code> <small>
|
|
* (right brace)</small>.
|
|
*/
|
|
|
|
@Override
|
|
public String toString() {
|
|
try {
|
|
return this.toString(0);
|
|
}
|
|
catch (Exception e) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Make a pretty-printed JSON text of this JSONObject.
|
|
*
|
|
* <p>If <pre>{@code indentFactor > 0}</pre> and the {@link JSONObject} has only one key, then the
|
|
* object will be output on a single line:
|
|
* <pre>{@code {"key": 1}}</pre>
|
|
*
|
|
* <p>If an object has 2 or more keys, then it will be output across multiple lines: <pre>{@code {
|
|
* "key1": 1,
|
|
* "key2": "value 2",
|
|
* "key3": 3
|
|
* }}</pre>
|
|
* <p><b>
|
|
* Warning: This method assumes that the data structure is acyclical.
|
|
* </b>
|
|
*
|
|
* @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 <code>{</code> <small>(left brace)</small> and ending with <code>}</code> <small>
|
|
* (right brace)</small>.
|
|
* @throws JSONException If the object contains an invalid number.
|
|
*/
|
|
|
|
public String toString(int indentFactor) throws JSONException {
|
|
// 6 characters are the minimum to serialise a key value pair e.g.: "k":1,
|
|
// and we don't want to oversize the initial capacity
|
|
int initialSize = map.size() * 6;
|
|
Writer w = new StringBuilderWriter(Math.max(initialSize, 16));
|
|
return this.write(w, 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.
|
|
*
|
|
* <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> <small>(left
|
|
* brace)</small> and ending with <code>}</code> <small>(right
|
|
* brace)</small>.
|
|
* @throws JSONException
|
|
* If the value is or contains an invalid number.
|
|
*/
|
|
|
|
public static String valueToString(Object value) throws JSONException {
|
|
// moves the implementation to JSONWriter as:
|
|
// 1. It makes more sense to be part of the writer class
|
|
// 2. For Android support this method is not available. By implementing it in the Writer
|
|
// Android users can use the writer with the built in Android JSONObject implementation.
|
|
return JSONWriter.valueToString(value);
|
|
}
|
|
|
|
/**
|
|
* Wrap an object, if necessary. If the object is <code>null</code>, 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
|
|
*/
|
|
|
|
// TODO: Uncomment later
|
|
/*
|
|
public static Object wrap(Object object) {
|
|
return wrap(object, null);
|
|
}
|
|
*/
|
|
|
|
/**
|
|
* Wrap an object, if necessary. If the object is <code>null</code>, 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
|
|
* @param recursionDepth
|
|
* Variable for tracking the count of nested object creations.
|
|
* @param jsonParserConfiguration
|
|
* Variable to pass parser custom configuration for json parsing.
|
|
* @return The wrapped value
|
|
*/
|
|
|
|
// TODO: Uncomment later
|
|
/*
|
|
static Object wrap(Object object, int recursionDepth, JSONParserConfiguration jsonParserConfiguration) {
|
|
return wrap(object, null, recursionDepth, jsonParserConfiguration);
|
|
}
|
|
|
|
private static Object wrap(Object object, Set<Object> objectsRecord) {
|
|
return wrap(object, objectsRecord, 0, new JSONParserConfiguration());
|
|
}
|
|
*/
|
|
|
|
// TODO: Uncomment later
|
|
/*
|
|
private static Object wrap(Object object, Set<Object> objectsRecord, int recursionDepth, JSONParserConfiguration jsonParserConfiguration) {
|
|
try {
|
|
if (Null.TYPE.equals(object)) {
|
|
return Null.TYPE;
|
|
}
|
|
if (object instanceof JSONObject || object instanceof JSONArray
|
|
|| object instanceof JSONString || object instanceof String
|
|
|| object instanceof Byte || object instanceof Character
|
|
|| object instanceof Short || object instanceof Integer
|
|
|| object instanceof Long || object instanceof Boolean
|
|
|| object instanceof Float || object instanceof Double
|
|
|| object instanceof BigInteger || object instanceof BigDecimal
|
|
|| object instanceof Enum) {
|
|
return object;
|
|
}
|
|
|
|
if (object instanceof Collection) {
|
|
Collection<?> coll = (Collection<?>) object;
|
|
return new JSONArray(coll, recursionDepth, jsonParserConfiguration);
|
|
}
|
|
if (object.getClass().isArray()) {
|
|
return new JSONArray(object);
|
|
}
|
|
if (object instanceof Map) {
|
|
Map<?, ?> map = (Map<?, ?>) object;
|
|
return new JSONObject(map, recursionDepth, jsonParserConfiguration);
|
|
}
|
|
Package objectPackage = object.getClass().getPackage();
|
|
String objectPackageName = objectPackage != null ? objectPackage
|
|
.getName() : "";
|
|
if (objectPackageName.startsWith("java.")
|
|
|| objectPackageName.startsWith("javax.")
|
|
|| object.getClass().getClassLoader() == null) {
|
|
return object.toString();
|
|
}
|
|
if (objectsRecord != null) {
|
|
return new JSONObject(object, objectsRecord);
|
|
}
|
|
return new JSONObject(object);
|
|
}
|
|
catch (JSONException exception) {
|
|
throw exception;
|
|
} catch (Exception exception) {
|
|
return null;
|
|
}
|
|
}
|
|
*/
|
|
|
|
/**
|
|
* Write the contents of the JSONObject as JSON text to a writer. For
|
|
* compactness, no whitespace is added.
|
|
* <p><b>
|
|
* Warning: This method assumes that the data structure is acyclical.
|
|
* </b>
|
|
* @param writer the writer object
|
|
* @return The writer.
|
|
* @throws JSONException if a called function has an error
|
|
*/
|
|
public Writer write(Writer writer) throws JSONException {
|
|
return this.write(writer, 0, 0);
|
|
}
|
|
|
|
static Writer writeValue(Writer writer, Object value,
|
|
int indentFactor, int indent) throws JSONException, IOException {
|
|
if (value == null || value.equals(null)) {
|
|
writer.write("null");
|
|
} else if (value instanceof JSONString) {
|
|
// may throw an exception
|
|
processJsonStringToWriteValue(writer, value);
|
|
} else if (value instanceof String) {
|
|
// assuming most values are Strings, so testing it early
|
|
JSONUtils.quote(value.toString(), writer);
|
|
return writer;
|
|
} else if (value instanceof Number) {
|
|
// may throw an exception
|
|
processNumberToWriteValue(writer, (Number) value);
|
|
} else if (value instanceof Boolean) {
|
|
writer.write(value.toString());
|
|
} else if (value instanceof Enum<?>) {
|
|
writer.write(JSONUtils.quote(((Enum<?>)value).name()));
|
|
} else if (value instanceof JSONObject) {
|
|
((JSONObject) value).write(writer, indentFactor, indent);
|
|
} else if (value instanceof JSONArray) {
|
|
((JSONArray) value).write(writer, indentFactor, indent);
|
|
} else if (value instanceof Map) {
|
|
Map<?, ?> map = (Map<?, ?>) value;
|
|
new JSONObject(map).write(writer, indentFactor, indent);
|
|
} else if (value instanceof Collection) {
|
|
Collection<?> coll = (Collection<?>) value;
|
|
new JSONArray(coll).write(writer, indentFactor, indent);
|
|
} else if (value.getClass().isArray()) {
|
|
new JSONArray(value).write(writer, indentFactor, indent);
|
|
} else {
|
|
JSONUtils.quote(value.toString(), writer);
|
|
}
|
|
return writer;
|
|
}
|
|
|
|
/**
|
|
* Convenience function to reduce cog complexity of calling method; writes value if string is valid
|
|
* @param writer Object doing the writing
|
|
* @param value Value to be written
|
|
* @throws IOException if something goes wrong
|
|
*/
|
|
private static void processJsonStringToWriteValue(Writer writer, Object value) throws IOException {
|
|
// JSONString must be checked first, so it can overwrite behaviour of other types below
|
|
Object o;
|
|
try {
|
|
o = ((JSONString) value).toJSONString();
|
|
} catch (Exception e) {
|
|
throw new JSONException(e);
|
|
}
|
|
writer.write(o != null ? o.toString() : JSONUtils.quote(value.toString()));
|
|
}
|
|
|
|
/**
|
|
* Convenience function to reduce cog complexity of calling method; writes value if number is valid
|
|
* @param writer Object doing the writing
|
|
* @param value Value to be written
|
|
* @throws IOException if something goes wrong
|
|
*/
|
|
private static void processNumberToWriteValue(Writer writer, Number value) throws IOException {
|
|
// not all Numbers may match actual JSON Numbers. i.e. fractions or Imaginary
|
|
final String numberAsString = JSONUtils.numberToString(value);
|
|
if(JSONUtils.NUMBER_PATTERN.matcher(numberAsString).matches()) {
|
|
writer.write(numberAsString);
|
|
} else {
|
|
// The Number value is not a valid JSON number.
|
|
// Instead we will quote it as a string
|
|
JSONUtils.quote(numberAsString, writer);
|
|
}
|
|
}
|
|
|
|
static final void indent(Writer writer, int indent) throws IOException {
|
|
for (int i = 0; i < indent; i += 1) {
|
|
writer.write(' ');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Write the contents of the JSONObject as JSON text to a writer.
|
|
*
|
|
* <p>If <pre>{@code indentFactor > 0}</pre> and the {@link JSONObject}
|
|
* has only one key, then the object will be output on a single line:
|
|
* <pre>{@code {"key": 1}}</pre>
|
|
*
|
|
* <p>If an object has 2 or more keys, then it will be output across
|
|
* multiple lines: <pre>{@code {
|
|
* "key1": 1,
|
|
* "key2": "value 2",
|
|
* "key3": 3
|
|
* }}</pre>
|
|
* <p><b>
|
|
* Warning: This method assumes that the data structure is acyclical.
|
|
* </b>
|
|
*
|
|
* @param writer
|
|
* Writes the serialized JSON
|
|
* @param indentFactor
|
|
* The number of spaces to add to each level of indentation.
|
|
* @param indent
|
|
* The indentation of the top level.
|
|
* @return The writer.
|
|
* @throws JSONException if a called function has an error or a write error
|
|
* occurs
|
|
*/
|
|
|
|
public Writer write(Writer writer, int indentFactor, int indent)
|
|
throws JSONException {
|
|
try {
|
|
boolean needsComma = false;
|
|
final int length = this.length();
|
|
writer.write('{');
|
|
|
|
if (length == 1) {
|
|
final Entry<String,?> entry = this.entrySet().iterator().next();
|
|
final String key = entry.getKey();
|
|
writer.write(JSONUtils.quote(key));
|
|
writer.write(':');
|
|
if (indentFactor > 0) {
|
|
writer.write(' ');
|
|
}
|
|
// might throw an exception
|
|
attemptWriteValue(writer, indentFactor, indent, entry, key);
|
|
} else if (length != 0) {
|
|
writeContent(writer, indentFactor, indent, needsComma);
|
|
}
|
|
writer.write('}');
|
|
return writer;
|
|
} catch (IOException exception) {
|
|
throw new JSONException(exception);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Convenience function. Writer attempts to write formatted content
|
|
* @param writer
|
|
* Writes the serialized JSON
|
|
* @param indentFactor
|
|
* The number of spaces to add to each level of indentation.
|
|
* @param indent
|
|
* The indentation of the top level.
|
|
* @param needsComma
|
|
* Boolean flag indicating a comma is needed
|
|
* @throws IOException
|
|
* If something goes wrong
|
|
*/
|
|
private void writeContent(Writer writer, int indentFactor, int indent, boolean needsComma) throws IOException {
|
|
final int newIndent = indent + indentFactor;
|
|
for (final Entry<String,?> entry : this.entrySet()) {
|
|
if (needsComma) {
|
|
writer.write(',');
|
|
}
|
|
if (indentFactor > 0) {
|
|
writer.write('\n');
|
|
}
|
|
indent(writer, newIndent);
|
|
final String key = entry.getKey();
|
|
writer.write(JSONUtils.quote(key));
|
|
writer.write(':');
|
|
if (indentFactor > 0) {
|
|
writer.write(' ');
|
|
}
|
|
attemptWriteValue(writer, indentFactor, newIndent, entry, key);
|
|
needsComma = true;
|
|
}
|
|
if (indentFactor > 0) {
|
|
writer.write('\n');
|
|
}
|
|
indent(writer, indent);
|
|
}
|
|
|
|
/**
|
|
* Convenience function. Writer attempts to write a value.
|
|
* @param writer
|
|
* Writes the serialized JSON
|
|
* @param indentFactor
|
|
* The number of spaces to add to each level of indentation.
|
|
* @param indent
|
|
* The indentation of the top level.
|
|
* @param entry
|
|
* Contains the value being written
|
|
* @param key
|
|
* Identifies the value
|
|
* @throws JSONException if a called function has an error or a write error
|
|
* occurs
|
|
|
|
*/
|
|
private static void attemptWriteValue(Writer writer, int indentFactor, int indent, Entry<String, ?> entry, String key) {
|
|
try{
|
|
writeValue(writer, entry.getValue(), indentFactor, indent);
|
|
} catch (Exception e) {
|
|
throw new JSONException("Unable to write JSONObject value for key: " + key, e);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Returns a java.util.Map containing all of the entries in this object.
|
|
* If an entry in the object is a JSONArray or JSONObject it will also
|
|
* be converted.
|
|
* <p>
|
|
* Warning: This method assumes that the data structure is acyclical.
|
|
*
|
|
* @return a java.util.Map containing the entries of this object
|
|
*/
|
|
public Map<String, Object> toMap() {
|
|
Map<String, Object> results = new HashMap<String, Object>();
|
|
for (Entry<String, Object> entry : this.entrySet()) {
|
|
Object value;
|
|
if (entry.getValue() == null || Null.TYPE.equals(entry.getValue())) {
|
|
value = null;
|
|
} else if (entry.getValue() instanceof JSONObject) {
|
|
value = ((JSONObject) entry.getValue()).toMap();
|
|
} else if (entry.getValue() instanceof JSONArray) {
|
|
value = ((JSONArray) entry.getValue()).toList();
|
|
} else {
|
|
value = entry.getValue();
|
|
}
|
|
results.put(entry.getKey(), value);
|
|
}
|
|
return results;
|
|
}
|
|
|
|
/**
|
|
* Create a new JSONException in a common format for incorrect conversions.
|
|
* @param key name of the key
|
|
* @param valueType the type of value being coerced to
|
|
* @param cause optional cause of the coercion failure
|
|
* @return JSONException that can be thrown.
|
|
*/
|
|
private JSONException wrongValueFormatException(
|
|
String key,
|
|
String valueType,
|
|
Object value,
|
|
Throwable cause) {
|
|
if(value == null) {
|
|
|
|
return new JSONException(
|
|
"JSONObject[" + JSONUtils.quote(key) + "] is not a " + valueType + " (null)."
|
|
, cause);
|
|
}
|
|
// don't try to toString collections or known object types that could be large.
|
|
if(value instanceof Map || value instanceof Iterable || value instanceof JSONObject) {
|
|
return new JSONException(
|
|
"JSONObject[" + JSONUtils.quote(key) + "] is not a " + valueType + " (" + value.getClass() + ")."
|
|
, cause);
|
|
}
|
|
return new JSONException(
|
|
"JSONObject[" + JSONUtils.quote(key) + "] is not a " + valueType + " (" + value.getClass() + " : " + value + ")."
|
|
, cause);
|
|
}
|
|
|
|
|
|
/**
|
|
* Helper method to extract the raw Class from Type.
|
|
*/
|
|
private Class<?> getRawType(Type type) {
|
|
if (type instanceof Class) {
|
|
return (Class<?>) type;
|
|
} else if (type instanceof ParameterizedType) {
|
|
return (Class<?>) ((ParameterizedType) type).getRawType();
|
|
} else if (type instanceof GenericArrayType) {
|
|
return Object[].class; // Simplified handling for arrays
|
|
}
|
|
return Object.class; // Fallback
|
|
}
|
|
|
|
/**
|
|
* Extracts the element Type for a Collection Type.
|
|
*/
|
|
private Type getElementType(Type type) {
|
|
if (type instanceof ParameterizedType) {
|
|
Type[] args = ((ParameterizedType) type).getActualTypeArguments();
|
|
return args.length > 0 ? args[0] : Object.class;
|
|
}
|
|
return Object.class;
|
|
}
|
|
|
|
/**
|
|
* Extracts the key and value Types for a Map Type.
|
|
*/
|
|
private Type[] getMapTypes(Type type) {
|
|
if (type instanceof ParameterizedType) {
|
|
Type[] args = ((ParameterizedType) type).getActualTypeArguments();
|
|
if (args.length == 2) {
|
|
return args;
|
|
}
|
|
}
|
|
return new Type[]{Object.class, Object.class}; // Default: String keys, Object values
|
|
}
|
|
|
|
|
|
//TODO: Remove code
|
|
/**
|
|
* Deserializes a JSON string into an instance of the specified class.
|
|
*
|
|
* <p>This method attempts to map JSON key-value pairs to the corresponding fields
|
|
* of the given class. It supports basic data types including int, double, float,
|
|
* long, and boolean (as well as their boxed counterparts). The class must have a
|
|
* no-argument constructor, and the field names in the class must match the keys
|
|
* in the JSON string.
|
|
*
|
|
* @param <T> the type of the object to return
|
|
* @param jsonString json in string format
|
|
* @param clazz the class of the object to be returned
|
|
* @return an instance of Object T with fields populated from the JSON string
|
|
*/
|
|
//TODO: Remove code
|
|
/*
|
|
public static <T> T fromJson(String jsonString, Class<T> clazz) {
|
|
JSONObject jsonObject = new JSONObject(jsonString);
|
|
return jsonObject.fromJson(clazz);
|
|
}
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
* Deserializes a JSON string into an instance of the specified class.
|
|
*
|
|
* <p>This method attempts to map JSON key-value pairs to the corresponding fields
|
|
* of the given class. It supports basic data types including {@code int}, {@code double},
|
|
* {@code float}, {@code long}, and {@code boolean}, as well as their boxed counterparts.
|
|
* The target class must have a no-argument constructor, and its field names must match
|
|
* the keys in the JSON string. Static fields are ignored.
|
|
*
|
|
* <p><strong>Note:</strong> Only classes that are explicitly supported and registered within
|
|
* the {@code JSONObject} context can be deserialized. If the provided class is not among those,
|
|
* this method will not be able to deserialize it. This ensures that only a limited and
|
|
* controlled set of types can be instantiated from JSON for safety and predictability.
|
|
*
|
|
* @param clazz the class of the object to be returned
|
|
* @param <T> the type of the object
|
|
* @return an instance of type {@code T} with fields populated from the JSON string
|
|
* @throws IllegalArgumentException if the class is not supported for deserialization
|
|
*/
|
|
@SuppressWarnings("unchecked")
|
|
public <T> T fromJson(Class<T> clazz) {
|
|
try {
|
|
T obj = clazz.getDeclaredConstructor().newInstance();
|
|
for (Field field : clazz.getDeclaredFields()) {
|
|
if (Modifier.isStatic(field.getModifiers())) {
|
|
continue;
|
|
}
|
|
field.setAccessible(true);
|
|
String fieldName = field.getName();
|
|
if (has(fieldName)) {
|
|
Object value = get(fieldName);
|
|
Type fieldType = field.getGenericType();
|
|
Object convertedValue = convertValue(value, fieldType);
|
|
field.set(obj, convertedValue);
|
|
}
|
|
}
|
|
return obj;
|
|
} catch (NoSuchMethodException e) {
|
|
throw new JSONException("No no-arg constructor for class: " + clazz.getName(), e);
|
|
} catch (Exception e) {
|
|
throw new JSONException("Failed to instantiate or set field for class: " + clazz.getName(), e);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Recursively converts a value to the target Type, handling nested generics for Collections and Maps.
|
|
*/
|
|
private Object convertValue(Object value, Type targetType) throws JSONException {
|
|
if (value == null) {
|
|
return null;
|
|
}
|
|
|
|
Class<?> rawType = getRawType(targetType);
|
|
|
|
// Direct assignment
|
|
if (rawType.isAssignableFrom(value.getClass())) {
|
|
return value;
|
|
}
|
|
|
|
if (rawType == int.class || rawType == Integer.class) {
|
|
return ((Number) value).intValue();
|
|
} else if (rawType == double.class || rawType == Double.class) {
|
|
return ((Number) value).doubleValue();
|
|
} else if (rawType == float.class || rawType == Float.class) {
|
|
return ((Number) value).floatValue();
|
|
} else if (rawType == long.class || rawType == Long.class) {
|
|
return ((Number) value).longValue();
|
|
} else if (rawType == boolean.class || rawType == Boolean.class) {
|
|
return value;
|
|
} else if (rawType == String.class) {
|
|
return value;
|
|
} else if (rawType == BigDecimal.class) {
|
|
return new BigDecimal((String) value);
|
|
} else if (rawType == BigInteger.class) {
|
|
return new BigInteger((String) value);
|
|
}
|
|
|
|
// Enum conversion
|
|
if (rawType.isEnum() && value instanceof String) {
|
|
return stringToEnum(rawType, (String) value);
|
|
}
|
|
|
|
// Collection handling (e.g., List<List<Map<String, Integer>>>)
|
|
if (Collection.class.isAssignableFrom(rawType)) {
|
|
if (value instanceof JSONArray) {
|
|
Type elementType = getElementType(targetType);
|
|
return fromJsonArray((JSONArray) value, rawType, elementType);
|
|
}
|
|
}
|
|
// Map handling (e.g., Map<Integer, List<String>>)
|
|
else if (Map.class.isAssignableFrom(rawType) && value instanceof JSONObject) {
|
|
Type[] mapTypes = getMapTypes(targetType);
|
|
Type keyType = mapTypes[0];
|
|
Type valueType = mapTypes[1];
|
|
return convertToMap((JSONObject) value, keyType, valueType, rawType);
|
|
}
|
|
// POJO handling (including custom classes like Tuple<Integer, String, Integer>)
|
|
else if (!rawType.isPrimitive() && !rawType.isEnum() && value instanceof JSONObject) {
|
|
// Recurse with the raw class for POJO deserialization
|
|
return ((JSONObject) value).fromJson(rawType);
|
|
}
|
|
|
|
// Fallback
|
|
return value.toString();
|
|
}
|
|
|
|
/**
|
|
* Converts a JSONObject to a Map with the specified generic key and value Types.
|
|
* Supports nested types via recursive convertValue.
|
|
*/
|
|
private Map<?, ?> convertToMap(JSONObject jsonMap, Type keyType, Type valueType, Class<?> mapType) throws JSONException {
|
|
try {
|
|
@SuppressWarnings("unchecked")
|
|
Map<Object, Object> createdMap = new HashMap();
|
|
|
|
for (Object keyObj : jsonMap.keySet()) {
|
|
String keyStr = (String) keyObj;
|
|
Object mapValue = jsonMap.get(keyStr);
|
|
// Convert key (e.g., String to Integer for Map<Integer, ...>)
|
|
Object convertedKey = convertValue(keyStr, keyType);
|
|
// Convert value recursively (handles nesting)
|
|
Object convertedValue = convertValue(mapValue, valueType);
|
|
createdMap.put(convertedKey, convertedValue);
|
|
}
|
|
return createdMap;
|
|
} catch (Exception e) {
|
|
throw new JSONException("Failed to convert JSONObject to Map: " + mapType.getName(), e);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Converts a String to an Enum value.
|
|
* The unchecked warning is suppressed when casting valueOf() to E
|
|
* @param enumClass enum class
|
|
* @param value value of enum
|
|
* @param <E> type of enum
|
|
*/
|
|
@SuppressWarnings("unchecked")
|
|
private <E> E stringToEnum(Class<?> enumClass, String value) throws JSONException {
|
|
try {
|
|
@SuppressWarnings("unchecked")
|
|
Class<E> enumType = (Class<E>) enumClass;
|
|
Method valueOfMethod = enumType.getMethod("valueOf", String.class);
|
|
return (E) valueOfMethod.invoke(null, value);
|
|
} catch (Exception e) {
|
|
throw new JSONException("Failed to convert string to enum: " + value + " for " + enumClass.getName(), e);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Deserializes a JSONArray into a Collection, supporting nested generics.
|
|
* Uses recursive convertValue for elements.
|
|
*/
|
|
@SuppressWarnings("unchecked")
|
|
private <T> Collection<T> fromJsonArray(JSONArray jsonArray, Class<?> collectionType, Type elementType) throws JSONException {
|
|
try {
|
|
Collection<T> collection = getCollection(collectionType);
|
|
|
|
for (int i = 0; i < jsonArray.length(); i++) {
|
|
Object jsonElement = jsonArray.get(i);
|
|
// Recursively convert each element using the full element Type (handles nesting)
|
|
Object convertedValue = convertValue(jsonElement, elementType);
|
|
collection.add((T) convertedValue);
|
|
}
|
|
return collection;
|
|
} catch (Exception e) {
|
|
throw new JSONException("Failed to convert JSONArray to Collection: " + collectionType.getName(), e);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Creates and returns a new instance of a supported {@link Collection} implementation
|
|
* based on the specified collection type.
|
|
* <p>
|
|
* This method currently supports the following collection types:
|
|
* <ul>
|
|
* <li>{@code List.class}</li>
|
|
* <li>{@code ArrayList.class}</li>
|
|
* <li>{@code Set.class}</li>
|
|
* <li>{@code HashSet.class}</li>
|
|
* </ul>
|
|
* If the provided type does not match any of the supported types, a {@link JSONException}
|
|
* is thrown.
|
|
*
|
|
* @param collectionType the {@link Class} object representing the desired collection type
|
|
* @return a new empty instance of the specified collection type
|
|
* @throws JSONException if the specified type is not a supported collection type
|
|
*/
|
|
private Collection getCollection(Class<?> collectionType) throws JSONException {
|
|
if (collectionType == List.class || collectionType == ArrayList.class) {
|
|
return new ArrayList();
|
|
} else if (collectionType == Set.class || collectionType == HashSet.class) {
|
|
return new HashSet();
|
|
} else {
|
|
throw new JSONException("Unsupported Collection type: " + collectionType.getName());
|
|
}
|
|
}
|
|
}
|