(m.size());
- for (final Entry, ?> e : m.entrySet()) {
- if(e.getKey() == null) {
- throw new NullPointerException("Null key.");
- }
+ 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);
@@ -325,28 +320,24 @@ private JSONObject(Map, ?> m, int recursionDepth, JSONParserConfiguration json
}
/**
- * 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
+ * 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.
+ * 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.
+ * 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.
+ * 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
+ * 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 {@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:
*
* @JSONPropertyName("FullName")
* public String getName() { return this.name; }
@@ -355,33 +346,27 @@ private JSONObject(Map, ?> m, int recursionDepth, JSONParserConfiguration json
*
* 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:
+ * name used in the JSONObject as seen below even though the field would normally be ignored:
*
* @JSONPropertyName("FullName")
* public String fullName() { return this.name; }
*
* 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:
+ * 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:
*
* @JSONPropertyName("FullName")
* @JSONPropertyIgnore
* public String getName() { return this.name; }
*
*
- * @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.
+ * @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();
@@ -394,20 +379,14 @@ private JSONObject(Object bean, Set objectsRecord) {
}
/**
- * Construct a JSONObject from an Object, using reflection to find the
- * public members. The resulting JSONObject's keys will be the strings from
- * the names array, and the values will be the field values associated with
- * those keys in the object. If a key is not found or not visible, then it
- * will not be copied into the new JSONObject.
+ * Construct a JSONObject from an Object, using reflection to find the public members. The resulting JSONObject's
+ * keys will be the strings from the names array, and the values will be the field values associated with those keys
+ * in the object. If a key is not found or not visible, then it will not be copied into the new JSONObject.
*
- * @param object
- * An object that has fields that should be used to make a
- * JSONObject.
- * @param names
- * An array of strings, the names of the fields to be obtained
- * from the object.
+ * @param object An object that has fields that should be used to make a JSONObject.
+ * @param names An array of strings, the names of the fields to be obtained from the object.
*/
- public JSONObject(Object object, String ... names) {
+ public JSONObject(Object object, String... names) {
this(names.length);
Class> c = object.getClass();
for (int i = 0; i < names.length; i += 1) {
@@ -420,34 +399,24 @@ public JSONObject(Object object, String ... names) {
}
/**
- * Construct a JSONObject from a source JSON text string. This is the most
- * commonly used JSONObject constructor.
+ * Construct a JSONObject from a source JSON text string. This is the most commonly used JSONObject constructor.
*
- * @param source
- * A string beginning with {
(left
- * brace) and ending with }
- * (right brace) .
- * @exception JSONException
- * If there is a syntax error in the source string or a
- * duplicated key.
+ * @param source A string beginning with {
(left brace) and ending with
+ * }
(right brace) .
+ * @throws 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.
+ * 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 {
(left
- * brace) and ending with }
- * (right brace) .
- * @param jsonParserConfiguration
- * 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.
+ * @param source A string beginning with {
(left brace) and ending
+ * with }
(right brace) .
+ * @param jsonParserConfiguration 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(String source, JSONParserConfiguration jsonParserConfiguration) throws JSONException {
this(new JSONTokener(source), jsonParserConfiguration);
@@ -456,17 +425,14 @@ public JSONObject(String source, JSONParserConfiguration jsonParserConfiguration
/**
* Construct a JSONObject from a ResourceBundle.
*
- * @param baseName
- * The ResourceBundle base name.
- * @param locale
- * The Locale to load the ResourceBundle for.
- * @throws JSONException
- * If any JSONExceptions are detected.
+ * @param baseName The ResourceBundle base name.
+ * @param locale The Locale to load the ResourceBundle for.
+ * @throws JSONException If any JSONExceptions are detected.
*/
public JSONObject(String baseName, Locale locale) throws JSONException {
this();
ResourceBundle bundle = ResourceBundle.getBundle(baseName, locale,
- Thread.currentThread().getContextClassLoader());
+ Thread.currentThread().getContextClassLoader());
// Iterate through the keys in the bundle.
@@ -497,44 +463,36 @@ public JSONObject(String baseName, Locale locale) throws JSONException {
}
/**
- * 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.
+ * 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){
+ protected JSONObject(int initialCapacity) {
this.map = new HashMap(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.
- *
- * 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.
+ * 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.
+ *
+ * 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.
+ * @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 null
.
+ * @throws JSONException If the value is non-finite number.
+ * @throws NullPointerException If the key is null
.
*/
public JSONObject accumulate(String key, Object value) throws JSONException {
testValidity(value);
Object object = this.opt(key);
if (object == null) {
this.put(key,
- value instanceof JSONArray ? new JSONArray().put(value)
- : value);
+ value instanceof JSONArray ? new JSONArray().put(value)
+ : value);
} else if (object instanceof JSONArray) {
((JSONArray) object).put(value);
} else {
@@ -544,21 +502,16 @@ public JSONObject accumulate(String key, Object value) throws JSONException {
}
/**
- * 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.
+ * 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.
+ * @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 null
.
+ * @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 null
.
*/
public JSONObject append(String key, Object value) throws JSONException {
testValidity(value);
@@ -574,11 +527,9 @@ public JSONObject append(String key, Object value) throws JSONException {
}
/**
- * Produce a string from a double. The string "null" will be returned if the
- * number is not finite.
+ * Produce a string from a double. The string "null" will be returned if the number is not finite.
*
- * @param d
- * A double.
+ * @param d A double.
* @return A String.
*/
public static String doubleToString(double d) {
@@ -590,7 +541,7 @@ public static String doubleToString(double d) {
String string = Double.toString(d);
if (string.indexOf('.') > 0 && string.indexOf('e') < 0
- && string.indexOf('E') < 0) {
+ && string.indexOf('E') < 0) {
while (string.endsWith("0")) {
string = string.substring(0, string.length() - 1);
}
@@ -604,11 +555,9 @@ public static String doubleToString(double d) {
/**
* Get the value object associated with a key.
*
- * @param key
- * A key string.
+ * @param key A key string.
* @return The object associated with the key.
- * @throws JSONException
- * if the key is not found.
+ * @throws JSONException if the key is not found.
*/
public Object get(String key) throws JSONException {
if (key == null) {
@@ -624,20 +573,15 @@ public Object get(String key) throws JSONException {
/**
* Get the enum value associated with a key.
*
- * @param
- * Enum Type
- * @param clazz
- * The type of enum to retrieve.
- * @param key
- * A key string.
+ * @param 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.
+ * @throws JSONException if the key is not found or if the value cannot be converted to an enum.
*/
public > E getEnum(Class clazz, String key) throws JSONException {
E val = optEnum(clazz, key);
- if(val==null) {
+ 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
@@ -649,22 +593,19 @@ public > E getEnum(Class clazz, String key) throws JSONExce
/**
* Get the boolean value associated with a key.
*
- * @param key
- * A key string.
+ * @param key A key string.
* @return The truth.
- * @throws JSONException
- * if the value is not a Boolean or the String "true" or
- * "false".
+ * @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 (object.equals(Boolean.FALSE)
- || (object instanceof String && ((String) object)
- .equalsIgnoreCase("false"))) {
+ || (object instanceof String && ((String) object)
+ .equalsIgnoreCase("false"))) {
return false;
} else if (object.equals(Boolean.TRUE)
- || (object instanceof String && ((String) object)
- .equalsIgnoreCase("true"))) {
+ || (object instanceof String && ((String) object)
+ .equalsIgnoreCase("true"))) {
return true;
}
throw wrongValueFormatException(key, "Boolean", object, null);
@@ -673,12 +614,9 @@ public boolean getBoolean(String key) throws JSONException {
/**
* Get the BigInteger value associated with a key.
*
- * @param key
- * A key string.
+ * @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.
+ * @throws JSONException if the key is not found or if the value cannot be converted to BigInteger.
*/
public BigInteger getBigInteger(String key) throws JSONException {
Object object = this.get(key);
@@ -690,17 +628,13 @@ public BigInteger getBigInteger(String key) throws JSONException {
}
/**
- * 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.
+ * 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.
+ * @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.
+ * @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);
@@ -714,17 +648,15 @@ public BigDecimal getBigDecimal(String key) throws JSONException {
/**
* Get the double value associated with a key.
*
- * @param key
- * A key string.
+ * @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.
+ * @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();
+ if (object instanceof Number) {
+ return ((Number) object).doubleValue();
}
try {
return Double.parseDouble(object.toString());
@@ -736,17 +668,15 @@ public double getDouble(String key) throws JSONException {
/**
* Get the float value associated with a key.
*
- * @param key
- * A key string.
+ * @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.
+ * @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();
+ if (object instanceof Number) {
+ return ((Number) object).floatValue();
}
try {
return Float.parseFloat(object.toString());
@@ -758,18 +688,16 @@ public float getFloat(String key) throws JSONException {
/**
* Get the Number value associated with a key.
*
- * @param key
- * A key string.
+ * @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.
+ * @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 (Number) object;
}
return stringToNumber(object.toString());
} catch (Exception e) {
@@ -780,17 +708,14 @@ public Number getNumber(String key) throws JSONException {
/**
* Get the int value associated with a key.
*
- * @param key
- * A key string.
+ * @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.
+ * @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();
+ if (object instanceof Number) {
+ return ((Number) object).intValue();
}
try {
return Integer.parseInt(object.toString());
@@ -802,11 +727,9 @@ public int getInt(String key) throws JSONException {
/**
* Get the JSONArray value associated with a key.
*
- * @param key
- * A key string.
+ * @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.
+ * @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);
@@ -819,11 +742,9 @@ public JSONArray getJSONArray(String key) throws JSONException {
/**
* Get the JSONObject value associated with a key.
*
- * @param key
- * A key string.
+ * @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.
+ * @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);
@@ -836,17 +757,14 @@ public JSONObject getJSONObject(String key) throws JSONException {
/**
* Get the long value associated with a key.
*
- * @param key
- * A key string.
+ * @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.
+ * @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();
+ if (object instanceof Number) {
+ return ((Number) object).longValue();
}
try {
return Long.parseLong(object.toString());
@@ -858,8 +776,7 @@ public long getLong(String key) throws JSONException {
/**
* Get an array of field names from a JSONObject.
*
- * @param jo
- * JSON object
+ * @param jo JSON object
* @return An array of field names, or null if there are no names.
*/
public static String[] getNames(JSONObject jo) {
@@ -872,8 +789,7 @@ public static String[] getNames(JSONObject jo) {
/**
* Get an array of public field names from an Object.
*
- * @param object
- * object to read
+ * @param object object to read
* @return An array of field names, or null if there are no names.
*/
public static String[] getNames(Object object) {
@@ -896,11 +812,9 @@ public static String[] getNames(Object object) {
/**
* Get the string associated with a key.
*
- * @param key
- * A key string.
+ * @param key A key string.
* @return A string which is the value.
- * @throws JSONException
- * if there is no string value for the key.
+ * @throws JSONException if there is no string value for the key.
*/
public String getString(String key) throws JSONException {
Object object = this.get(key);
@@ -913,8 +827,7 @@ public String getString(String key) throws JSONException {
/**
* Determine if the JSONObject contains a specific key.
*
- * @param key
- * A key string.
+ * @param key A key string.
* @return true if the key exists in the JSONObject.
*/
public boolean has(String key) {
@@ -922,19 +835,15 @@ public boolean has(String 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.
+ * 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.
+ * @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.
+ * @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);
@@ -945,13 +854,13 @@ public JSONObject increment(String key) throws JSONException {
} else if (value instanceof Long) {
this.put(key, ((Long) value).longValue() + 1L);
} else if (value instanceof BigInteger) {
- this.put(key, ((BigInteger)value).add(BigInteger.ONE));
+ this.put(key, ((BigInteger) value).add(BigInteger.ONE));
} else if (value instanceof Float) {
this.put(key, ((Float) value).floatValue() + 1.0f);
} else if (value instanceof Double) {
this.put(key, ((Double) value).doubleValue() + 1.0d);
} else if (value instanceof BigDecimal) {
- this.put(key, ((BigDecimal)value).add(BigDecimal.ONE));
+ this.put(key, ((BigDecimal) value).add(BigDecimal.ONE));
} else {
throw new JSONException("Unable to increment [" + quote(key) + "].");
}
@@ -959,53 +868,45 @@ public JSONObject increment(String key) throws JSONException {
}
/**
- * Determine if the value associated with the key is null
or if there is no
- * value.
+ * Determine if the value associated with the key is null
or if there is no value.
*
- * @param key
- * A key string.
- * @return true if there is no value associated with the key or if the value
- * is the JSONObject.NULL object.
+ * @param key A key string.
+ * @return true if there is no value associated with the key or if the value is the JSONObject.NULL object.
*/
public boolean isNull(String key) {
return JSONObject.NULL.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()
+ * Get an enumeration of the keys of the JSONObject. Modifying this key Set will also modify the JSONObject. Use
+ * with caution.
*
* @return An iterator of the keys.
+ * @see Set#iterator()
*/
public Iterator 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()
+ * Get a set of keys of the JSONObject. Modifying this key Set will also modify the JSONObject. Use with caution.
*
* @return A keySet.
+ * @see Map#keySet()
*/
public Set 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
+ * 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
+ * @see Map#entrySet()
*/
protected Set> entrySet() {
return this.map.entrySet();
@@ -1021,8 +922,7 @@ public int length() {
}
/**
- * Removes all of the elements from this JSONObject.
- * The JSONObject will be empty after this call returns.
+ * Removes all of the elements from this JSONObject. The JSONObject will be empty after this call returns.
*/
public void clear() {
this.map.clear();
@@ -1038,27 +938,23 @@ public boolean isEmpty() {
}
/**
- * Produce a JSONArray containing the names of the elements of this
- * JSONObject.
+ * 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.
+ * @return A JSONArray containing the key strings, or null if the JSONObject is empty.
*/
public JSONArray names() {
- if(this.map.isEmpty()) {
- return null;
- }
+ if (this.map.isEmpty()) {
+ return null;
+ }
return new JSONArray(this.map.keySet());
}
/**
* Produce a string from a Number.
*
- * @param number
- * A Number
+ * @param number A Number
* @return A String.
- * @throws JSONException
- * If n is a non-finite number.
+ * @throws JSONException If n is a non-finite number.
*/
public static String numberToString(Number number) throws JSONException {
if (number == null) {
@@ -1070,7 +966,7 @@ public static String numberToString(Number number) throws JSONException {
String string = number.toString();
if (string.indexOf('.') > 0 && string.indexOf('e') < 0
- && string.indexOf('E') < 0) {
+ && string.indexOf('E') < 0) {
while (string.endsWith("0")) {
string = string.substring(0, string.length() - 1);
}
@@ -1084,8 +980,7 @@ public static String numberToString(Number number) throws JSONException {
/**
* Get an optional value associated with a key.
*
- * @param key
- * A key string.
+ * @param key A key string.
* @return An object which is the value, or null if there is no value.
*/
public Object opt(String key) {
@@ -1095,12 +990,9 @@ public Object opt(String key) {
/**
* Get the enum value associated with a key.
*
- * @param
- * Enum Type
- * @param clazz
- * The type of enum to retrieve.
- * @param key
- * A key string.
+ * @param 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 optEnum(Class clazz, String key) {
@@ -1110,16 +1002,12 @@ public > E optEnum(Class clazz, String key) {
/**
* Get the enum value associated with a key.
*
- * @param
- * 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 clazz
+ * @param 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
+ * clazz
*/
public > E optEnum(Class clazz, String key, E defaultValue) {
try {
@@ -1142,11 +1030,10 @@ public > E optEnum(Class clazz, String key, E 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".
+ * 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.
+ * @param key A key string.
* @return The truth.
*/
public boolean optBoolean(String key) {
@@ -1154,14 +1041,11 @@ public boolean optBoolean(String key) {
}
/**
- * 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).
+ * 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.
+ * @param key A key string.
+ * @param defaultValue The default.
* @return The truth.
*/
public boolean optBoolean(String key, boolean defaultValue) {
@@ -1169,7 +1053,7 @@ public boolean optBoolean(String key, boolean defaultValue) {
if (NULL.equals(val)) {
return defaultValue;
}
- if (val instanceof Boolean){
+ if (val instanceof Boolean) {
return ((Boolean) val).booleanValue();
}
try {
@@ -1181,11 +1065,10 @@ public boolean optBoolean(String key, boolean 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".
+ * 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.
+ * @param key A key string.
* @return The truth.
*/
public Boolean optBooleanObject(String key) {
@@ -1193,14 +1076,11 @@ public Boolean optBooleanObject(String key) {
}
/**
- * 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).
+ * 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.
+ * @param key A key string.
+ * @param defaultValue The default.
* @return The truth.
*/
public Boolean optBooleanObject(String key, Boolean defaultValue) {
@@ -1208,7 +1088,7 @@ public Boolean optBooleanObject(String key, Boolean defaultValue) {
if (NULL.equals(val)) {
return defaultValue;
}
- if (val instanceof Boolean){
+ if (val instanceof Boolean) {
return ((Boolean) val).booleanValue();
}
try {
@@ -1220,17 +1100,13 @@ public Boolean optBooleanObject(String key, Boolean 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.
+ * 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.
+ * @param key A key string.
+ * @param defaultValue The default.
* @return An object which is the value.
*/
public BigDecimal optBigDecimal(String key, BigDecimal defaultValue) {
@@ -1239,39 +1115,38 @@ public BigDecimal optBigDecimal(String key, BigDecimal defaultValue) {
}
/**
- * @param val value to convert
+ * @param val value to convert
* @param defaultValue default value to return is the conversion doesn't work or is null.
- * @return BigDecimal conversion of the original value, or the defaultValue if unable
- * to convert.
+ * @return BigDecimal conversion of the original value, or the defaultValue if unable to convert.
*/
static BigDecimal objectToBigDecimal(Object val, BigDecimal defaultValue) {
return objectToBigDecimal(val, defaultValue, true);
}
/**
- * @param val value to convert
+ * @param val value to convert
* @param defaultValue default value to return is the conversion doesn't work or is null.
- * @param exact When true
, then {@link Double} and {@link Float} values will be converted exactly.
- * When false
, they will be converted to {@link String} values before converting to {@link BigDecimal}.
- * @return BigDecimal conversion of the original value, or the defaultValue if unable
- * to convert.
+ * @param exact When true
, then {@link Double} and {@link Float} values will be converted
+ * exactly. When false
, they will be converted to {@link String} values before
+ * converting to {@link BigDecimal}.
+ * @return BigDecimal conversion of the original value, or the defaultValue if unable to convert.
*/
static BigDecimal objectToBigDecimal(Object val, BigDecimal defaultValue, boolean exact) {
if (NULL.equals(val)) {
return defaultValue;
}
- if (val instanceof BigDecimal){
+ if (val instanceof BigDecimal) {
return (BigDecimal) val;
}
- if (val instanceof BigInteger){
+ if (val instanceof BigInteger) {
return new BigDecimal((BigInteger) val);
}
- if (val instanceof Double || val instanceof Float){
- if (!numberIsFinite((Number)val)) {
+ if (val instanceof Double || val instanceof Float) {
+ if (!numberIsFinite((Number) val)) {
return defaultValue;
}
if (exact) {
- return new BigDecimal(((Number)val).doubleValue());
+ return new BigDecimal(((Number) val).doubleValue());
}
// use the string constructor so that we maintain "nice" values for doubles and floats
// the double constructor will translate doubles to "exact" values instead of the likely
@@ -1279,7 +1154,7 @@ static BigDecimal objectToBigDecimal(Object val, BigDecimal defaultValue, boolea
return new BigDecimal(val.toString());
}
if (val instanceof Long || val instanceof Integer
- || val instanceof Short || val instanceof Byte){
+ || val instanceof Short || val instanceof Byte) {
return new BigDecimal(((Number) val).longValue());
}
// don't check if it's a string in case of unchecked Number subclasses
@@ -1291,14 +1166,11 @@ static BigDecimal objectToBigDecimal(Object val, BigDecimal defaultValue, boolea
}
/**
- * 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.
+ * 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 key A key string.
+ * @param defaultValue The default.
* @return An object which is the value.
*/
public BigInteger optBigInteger(String key, BigInteger defaultValue) {
@@ -1307,29 +1179,28 @@ public BigInteger optBigInteger(String key, BigInteger defaultValue) {
}
/**
- * @param val value to convert
+ * @param val value to convert
* @param defaultValue default value to return is the conversion doesn't work or is null.
- * @return BigInteger conversion of the original value, or the defaultValue if unable
- * to convert.
+ * @return BigInteger conversion of the original value, or the defaultValue if unable to convert.
*/
static BigInteger objectToBigInteger(Object val, BigInteger defaultValue) {
if (NULL.equals(val)) {
return defaultValue;
}
- if (val instanceof BigInteger){
+ if (val instanceof BigInteger) {
return (BigInteger) val;
}
- if (val instanceof BigDecimal){
+ if (val instanceof BigDecimal) {
return ((BigDecimal) val).toBigInteger();
}
- if (val instanceof Double || val instanceof Float){
- if (!numberIsFinite((Number)val)) {
+ if (val instanceof Double || val instanceof Float) {
+ if (!numberIsFinite((Number) val)) {
return defaultValue;
}
return new BigDecimal(((Number) val).doubleValue()).toBigInteger();
}
if (val instanceof Long || val instanceof Integer
- || val instanceof Short || val instanceof Byte){
+ || val instanceof Short || val instanceof Byte) {
return BigInteger.valueOf(((Number) val).longValue());
}
// don't check if it's a string in case of unchecked Number subclasses
@@ -1340,7 +1211,7 @@ static BigInteger objectToBigInteger(Object val, BigInteger defaultValue) {
// this conversion to BigDecimal then to BigInteger is to maintain
// that type cast support that may truncate the decimal.
final String valStr = val.toString();
- if(isDecimalNotation(valStr)) {
+ if (isDecimalNotation(valStr)) {
return new BigDecimal(valStr).toBigInteger();
}
return new BigInteger(valStr);
@@ -1350,12 +1221,10 @@ static BigInteger objectToBigInteger(Object val, BigInteger defaultValue) {
}
/**
- * 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.
+ * 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.
+ * @param key A string which is the key.
* @return An object which is the value.
*/
public double optDouble(String key) {
@@ -1363,14 +1232,11 @@ public double optDouble(String key) {
}
/**
- * 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.
+ * 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.
+ * @param key A key string.
+ * @param defaultValue The default.
* @return An object which is the value.
*/
public double optDouble(String key, double defaultValue) {
@@ -1382,12 +1248,10 @@ public double optDouble(String key, double defaultValue) {
}
/**
- * 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.
+ * 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.
+ * @param key A string which is the key.
* @return An object which is the value.
*/
public Double optDoubleObject(String key) {
@@ -1395,14 +1259,11 @@ public Double optDoubleObject(String key) {
}
/**
- * 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.
+ * 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.
+ * @param key A key string.
+ * @param defaultValue The default.
* @return An object which is the value.
*/
public Double optDoubleObject(String key, Double defaultValue) {
@@ -1414,12 +1275,10 @@ public Double optDoubleObject(String key, Double defaultValue) {
}
/**
- * 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.
+ * 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.
+ * @param key A key string.
* @return The value.
*/
public float optFloat(String key) {
@@ -1427,14 +1286,11 @@ public float optFloat(String key) {
}
/**
- * 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.
+ * 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.
+ * @param key A key string.
+ * @param defaultValue The default value.
* @return The value.
*/
public float optFloat(String key, float defaultValue) {
@@ -1450,12 +1306,10 @@ public float optFloat(String key, float defaultValue) {
}
/**
- * 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.
+ * 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.
+ * @param key A key string.
* @return The object.
*/
public Float optFloatObject(String key) {
@@ -1463,14 +1317,11 @@ public Float optFloatObject(String key) {
}
/**
- * 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.
+ * 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.
+ * @param key A key string.
+ * @param defaultValue The default object.
* @return The object.
*/
public Float optFloatObject(String key, Float defaultValue) {
@@ -1486,12 +1337,10 @@ public Float optFloatObject(String key, Float defaultValue) {
}
/**
- * 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.
+ * 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.
+ * @param key A key string.
* @return An object which is the value.
*/
public int optInt(String key) {
@@ -1499,14 +1348,11 @@ public int optInt(String key) {
}
/**
- * 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.
+ * 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.
+ * @param key A key string.
+ * @param defaultValue The default.
* @return An object which is the value.
*/
public int optInt(String key, int defaultValue) {
@@ -1518,12 +1364,10 @@ public int optInt(String key, int defaultValue) {
}
/**
- * 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.
+ * 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.
+ * @param key A key string.
* @return An object which is the value.
*/
public Integer optIntegerObject(String key) {
@@ -1531,14 +1375,11 @@ public Integer optIntegerObject(String key) {
}
/**
- * 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.
+ * 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.
+ * @param key A key string.
+ * @param defaultValue The default.
* @return An object which is the value.
*/
public Integer optIntegerObject(String key, Integer defaultValue) {
@@ -1550,11 +1391,10 @@ public Integer optIntegerObject(String key, Integer defaultValue) {
}
/**
- * 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.
+ * 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.
+ * @param key A key string.
* @return A JSONArray which is the value.
*/
public JSONArray optJSONArray(String key) {
@@ -1562,13 +1402,11 @@ public JSONArray optJSONArray(String key) {
}
/**
- * 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.
+ * 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.
+ * @param key A key string.
+ * @param defaultValue The default.
* @return A JSONArray which is the value.
*/
public JSONArray optJSONArray(String key, JSONArray defaultValue) {
@@ -1577,23 +1415,22 @@ public JSONArray optJSONArray(String key, JSONArray 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.
+ * 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.
+ * @param key A key string.
* @return A JSONObject which is the value.
*/
- public JSONObject optJSONObject(String key) { return this.optJSONObject(key, null); }
+ 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.
+ * 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.
+ * @param key A key string.
+ * @param defaultValue The default.
* @return An JSONObject which is the value.
*/
public JSONObject optJSONObject(String key, JSONObject defaultValue) {
@@ -1602,12 +1439,10 @@ public JSONObject optJSONObject(String key, JSONObject 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.
+ * 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.
+ * @param key A key string.
* @return An object which is the value.
*/
public long optLong(String key) {
@@ -1615,14 +1450,11 @@ public long optLong(String key) {
}
/**
- * 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.
+ * 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.
+ * @param key A key string.
+ * @param defaultValue The default.
* @return An object which is the value.
*/
public long optLong(String key, long defaultValue) {
@@ -1635,12 +1467,10 @@ public long optLong(String key, long defaultValue) {
}
/**
- * 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.
+ * 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.
+ * @param key A key string.
* @return An object which is the value.
*/
public Long optLongObject(String key) {
@@ -1648,14 +1478,11 @@ public Long optLongObject(String key) {
}
/**
- * 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.
+ * 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.
+ * @param key A key string.
+ * @param defaultValue The default.
* @return An object which is the value.
*/
public Long optLongObject(String key, Long defaultValue) {
@@ -1668,13 +1495,11 @@ public Long optLongObject(String key, Long defaultValue) {
}
/**
- * Get an optional {@link Number} value associated with a key, or null
- * 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.
+ * Get an optional {@link Number} value associated with a key, or null
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.
+ * @param key A key string.
* @return An object which is the value.
*/
public Number optNumber(String key) {
@@ -1682,15 +1507,12 @@ public Number optNumber(String key) {
}
/**
- * 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
+ * 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.
+ * @param key A key string.
+ * @param defaultValue The default.
* @return An object which is the value.
*/
public Number optNumber(String key, Number defaultValue) {
@@ -1698,7 +1520,7 @@ public Number optNumber(String key, Number defaultValue) {
if (NULL.equals(val)) {
return defaultValue;
}
- if (val instanceof Number){
+ if (val instanceof Number) {
return (Number) val;
}
@@ -1710,12 +1532,10 @@ public Number optNumber(String key, Number 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.
+ * 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.
+ * @param key A key string.
* @return A string which is the value.
*/
public String optString(String key) {
@@ -1723,13 +1543,10 @@ public String optString(String key) {
}
/**
- * Get an optional string associated with a key. It returns the defaultValue
- * if there is no such 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.
+ * @param key A key string.
+ * @param defaultValue The default.
* @return A string which is the value.
*/
public String optString(String key, String defaultValue) {
@@ -1738,15 +1555,11 @@ public String optString(String key, String defaultValue) {
}
/**
- * Populates the internal map of the JSONObject with the bean properties. The
- * bean can not be recursive.
+ * Populates the internal map of the JSONObject with the bean properties. The bean can not be recursive.
*
+ * @param bean the bean
+ * @throws JSONException If a getter returned a non-finite number.
* @see JSONObject#JSONObject(Object)
- *
- * @param bean
- * the bean
- * @throws JSONException
- * If a getter returned a non-finite number.
*/
private void populateMap(Object bean) {
populateMap(bean, Collections.newSetFromMap(new IdentityHashMap()));
@@ -1763,11 +1576,11 @@ private void populateMap(Object bean, Set objectsRecord) {
for (final Method method : methods) {
final int modifiers = method.getModifiers();
if (Modifier.isPublic(modifiers)
- && !Modifier.isStatic(modifiers)
- && method.getParameterTypes().length == 0
- && !method.isBridge()
- && method.getReturnType() != Void.TYPE
- && isValidMethodName(method.getName())) {
+ && !Modifier.isStatic(modifiers)
+ && method.getParameterTypes().length == 0
+ && !method.isBridge()
+ && method.getReturnType() != Void.TYPE
+ && isValidMethodName(method.getName())) {
final String key = getKeyNameFromMethod(method);
if (key != null && !key.isEmpty()) {
try {
@@ -1848,18 +1661,14 @@ private static String getKeyNameFromMethod(Method method) {
}
/**
- * Searches the class hierarchy to see if the method or it's super
- * implementations and interfaces has the annotation.
- *
- * @param
- * type of the annotation
+ * Searches the class hierarchy to see if the method or it's super implementations and interfaces has the
+ * annotation.
*
- * @param m
- * method to check
- * @param annotationClass
- * annotation to look for
- * @return the {@link Annotation} if the annotation exists on the current method
- * or one of its super class definitions
+ * @param type of the annotation
+ * @param m method to check
+ * @param annotationClass annotation to look for
+ * @return the {@link Annotation} if the annotation exists on the current method or one of its super class
+ * definitions
*/
private static A getAnnotation(final Method m, final Class annotationClass) {
// if we have invalid data the result is null
@@ -1890,13 +1699,14 @@ private static A getAnnotation(final Method m, final Clas
}
//If the superclass is Object, no annotations will be found any more
- if (c.getSuperclass().equals(Object.class))
+ if (c.getSuperclass().equals(Object.class)) {
return null;
+ }
try {
return getAnnotation(
- c.getSuperclass().getMethod(m.getName(), m.getParameterTypes()),
- annotationClass);
+ c.getSuperclass().getMethod(m.getName(), m.getParameterTypes()),
+ annotationClass);
} catch (final SecurityException ex) {
return null;
} catch (final NoSuchMethodException ex) {
@@ -1905,14 +1715,11 @@ private static A getAnnotation(final Method m, final Clas
}
/**
- * Searches the class hierarchy to see if the method or it's super
- * implementations and interfaces has the annotation. Returns the depth of the
- * annotation in the hierarchy.
+ * Searches the class hierarchy to see if the method or it's super implementations and interfaces has the
+ * annotation. Returns the depth of the annotation in the hierarchy.
*
- * @param m
- * method to check
- * @param annotationClass
- * annotation to look for
+ * @param m method to check
+ * @param annotationClass annotation to look for
* @return Depth of the annotation or -1 if the annotation is not on the method.
*/
private static int getAnnotationDepth(final Method m, final Class extends Annotation> annotationClass) {
@@ -1948,13 +1755,14 @@ private static int getAnnotationDepth(final Method m, final Class extends Anno
}
//If the superclass is Object, no annotations will be found any more
- if (c.getSuperclass().equals(Object.class))
+ if (c.getSuperclass().equals(Object.class)) {
return -1;
+ }
try {
int d = getAnnotationDepth(
- c.getSuperclass().getMethod(m.getName(), m.getParameterTypes()),
- annotationClass);
+ c.getSuperclass().getMethod(m.getName(), m.getParameterTypes()),
+ annotationClass);
if (d > 0) {
// since the annotation was on the superclass, add 1
return d + 1;
@@ -1970,33 +1778,24 @@ private static int getAnnotationDepth(final Method m, final Class extends Anno
/**
* Put a key/boolean pair in the JSONObject.
*
- * @param key
- * A key string.
- * @param value
- * A boolean which is the value.
+ * @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 null
.
+ * @throws JSONException If the value is non-finite number.
+ * @throws NullPointerException If the key is null
.
*/
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.
+ * 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.
+ * @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 null
.
+ * @throws JSONException If the value is non-finite number.
+ * @throws NullPointerException If the key is null
.
*/
public JSONObject put(String key, Collection> value) throws JSONException {
return this.put(key, new JSONArray(value));
@@ -2005,15 +1804,11 @@ public JSONObject put(String key, Collection> value) throws JSONException {
/**
* Put a key/double pair in the JSONObject.
*
- * @param key
- * A key string.
- * @param value
- * A double which is the value.
+ * @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 null
.
+ * @throws JSONException If the value is non-finite number.
+ * @throws NullPointerException If the key is null
.
*/
public JSONObject put(String key, double value) throws JSONException {
return this.put(key, Double.valueOf(value));
@@ -2022,15 +1817,11 @@ public JSONObject put(String key, double value) throws JSONException {
/**
* Put a key/float pair in the JSONObject.
*
- * @param key
- * A key string.
- * @param value
- * A float which is the value.
+ * @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 null
.
+ * @throws JSONException If the value is non-finite number.
+ * @throws NullPointerException If the key is null
.
*/
public JSONObject put(String key, float value) throws JSONException {
return this.put(key, Float.valueOf(value));
@@ -2039,15 +1830,11 @@ public JSONObject put(String key, float value) throws JSONException {
/**
* Put a key/int pair in the JSONObject.
*
- * @param key
- * A key string.
- * @param value
- * An int which is the value.
+ * @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 null
.
+ * @throws JSONException If the value is non-finite number.
+ * @throws NullPointerException If the key is null
.
*/
public JSONObject put(String key, int value) throws JSONException {
return this.put(key, Integer.valueOf(value));
@@ -2056,53 +1843,39 @@ public JSONObject put(String key, int value) throws JSONException {
/**
* Put a key/long pair in the JSONObject.
*
- * @param key
- * A key string.
- * @param value
- * A long which is the value.
+ * @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 null
.
+ * @throws JSONException If the value is non-finite number.
+ * @throws NullPointerException If the key is null
.
*/
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.
+ * 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.
+ * @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 null
.
+ * @throws JSONException If the value is non-finite number.
+ * @throws NullPointerException If the key is null
.
*/
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 null
, then the
- * key will be removed from the JSONObject if it is present.
+ * Put a key/value pair in the JSONObject. If the value is null
, then the key will be removed from the
+ * JSONObject if it is present.
*
- * @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.
+ * @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 non-finite number.
- * @throws NullPointerException
- * If the key is null
.
+ * @throws JSONException If the value is non-finite number.
+ * @throws NullPointerException If the key is null
.
*/
public JSONObject put(String key, Object value) throws JSONException {
if (key == null) {
@@ -2118,17 +1891,13 @@ public JSONObject put(String key, Object value) throws JSONException {
}
/**
- * 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.
+ * 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
+ * @param key key to insert into
+ * @param value value to insert
* @return this.
- * @throws JSONException
- * if the key is a duplicate
+ * @throws JSONException if the key is a duplicate
*/
public JSONObject putOnce(String key, Object value) throws JSONException {
if (key != null && value != null) {
@@ -2141,18 +1910,13 @@ public JSONObject putOnce(String key, Object value) throws JSONException {
}
/**
- * Put a key/value pair in the JSONObject, but only if the key and the value
- * are both non-null.
+ * 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.
+ * @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.
+ * @throws JSONException If the value is a non-finite number.
*/
public JSONObject putOpt(String key, Object value) throws JSONException {
if (key != null && value != null) {
@@ -2162,9 +1926,8 @@ public JSONObject putOpt(String key, Object value) throws JSONException {
}
/**
- * 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:
+ * 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:
*
* {
* "a":{"b":"c"}
@@ -2174,8 +1937,8 @@ public JSONObject putOpt(String key, Object value) throws JSONException {
*
* "/a/b"
*
- * Then this method will return the String "c".
- * A JSONPointerException may be thrown from code called by this method.
+ * 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
@@ -2183,10 +1946,10 @@ public JSONObject putOpt(String key, Object value) throws JSONException {
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:
+ * 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:
*
* {
* "a":{"b":"c"}
@@ -2196,8 +1959,8 @@ public Object query(String jsonPointer) {
*
* "/a/b"
*
- * Then this method will return the String "c".
- * A JSONPointerException may be thrown from code called by this method.
+ * 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
@@ -2207,20 +1970,20 @@ public Object query(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.
+ * 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));
+ 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.
+ * 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}
@@ -2235,14 +1998,11 @@ public Object optQuery(JSONPointer jsonPointer) {
}
/**
- * Produce a string in double quotes with backslash sequences in all the
- * right places. A backslash will be inserted within </, producing
- * <\/, allowing JSON text to be delivered in HTML. In JSON text, a
- * string cannot contain a control character or an unescaped quote or
- * backslash.
+ * Produce a string in double quotes with backslash sequences in all the right places. A backslash will be inserted
+ * within </, producing <\/, allowing JSON text to be delivered in HTML. In JSON text, a string cannot contain
+ * a control character or an unescaped quote or backslash.
*
- * @param string
- * A String
+ * @param string A String
* @return A String correctly formatted for insertion in a JSON text.
*/
@SuppressWarnings("resource")
@@ -2284,42 +2044,42 @@ public static Writer quote(String string, Writer w) throws IOException {
b = c;
c = string.charAt(i);
switch (c) {
- case '\\':
- case '"':
- w.write('\\');
- w.write(c);
- break;
- case '/':
- if (b == '<') {
+ case '\\':
+ case '"':
w.write('\\');
- }
- w.write(c);
- break;
- case '\b':
- w.write("\\b");
- break;
- case '\t':
- w.write("\\t");
- break;
- case '\n':
- w.write("\\n");
- break;
- case '\f':
- w.write("\\f");
- break;
- case '\r':
- w.write("\\r");
- break;
- default:
- if (c < ' ' || (c >= '\u0080' && c < '\u00a0')
- || (c >= '\u2000' && c < '\u2100')) {
- w.write("\\u");
- hhhh = Integer.toHexString(c);
- w.write("0000", 0, 4 - hhhh.length());
- w.write(hhhh);
- } else {
w.write(c);
- }
+ break;
+ case '/':
+ if (b == '<') {
+ w.write('\\');
+ }
+ w.write(c);
+ break;
+ case '\b':
+ w.write("\\b");
+ break;
+ case '\t':
+ w.write("\\t");
+ break;
+ case '\n':
+ w.write("\\n");
+ break;
+ case '\f':
+ w.write("\\f");
+ break;
+ case '\r':
+ w.write("\\r");
+ break;
+ default:
+ if (c < ' ' || (c >= '\u0080' && c < '\u00a0')
+ || (c >= '\u2000' && c < '\u2100')) {
+ w.write("\\u");
+ hhhh = Integer.toHexString(c);
+ w.write("0000", 0, 4 - hhhh.length());
+ w.write(hhhh);
+ } else {
+ w.write(c);
+ }
}
}
w.write('"');
@@ -2329,18 +2089,15 @@ public static Writer quote(String string, Writer w) throws IOException {
/**
* 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.
+ * @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
+ * 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
@@ -2351,34 +2108,34 @@ public boolean similar(Object other) {
if (!(other instanceof JSONObject)) {
return false;
}
- if (!this.keySet().equals(((JSONObject)other).keySet())) {
+ if (!this.keySet().equals(((JSONObject) other).keySet())) {
return false;
}
- for (final Entry entry : this.entrySet()) {
+ for (final Entry entry : this.entrySet()) {
String name = entry.getKey();
Object valueThis = entry.getValue();
- Object valueOther = ((JSONObject)other).get(name);
- if(valueThis == valueOther) {
- continue;
+ Object valueOther = ((JSONObject) other).get(name);
+ if (valueThis == valueOther) {
+ continue;
}
- if(valueThis == null) {
- return false;
+ if (valueThis == null) {
+ return false;
}
if (valueThis instanceof JSONObject) {
- if (!((JSONObject)valueThis).similar(valueOther)) {
+ if (!((JSONObject) valueThis).similar(valueOther)) {
return false;
}
} else if (valueThis instanceof JSONArray) {
- if (!((JSONArray)valueThis).similar(valueOther)) {
+ if (!((JSONArray) valueThis).similar(valueOther)) {
return false;
}
} else if (valueThis instanceof Number && valueOther instanceof Number) {
- if (!isNumberSimilar((Number)valueThis, (Number)valueOther)) {
- return false;
+ if (!isNumberSimilar((Number) valueThis, (Number) valueOther)) {
+ return false;
}
} else if (valueThis instanceof JSONString && valueOther instanceof JSONString) {
if (!((JSONString) valueThis).toJSONString().equals(((JSONString) valueOther).toJSONString())) {
- return false;
+ return false;
}
} else if (!valueThis.equals(valueOther)) {
return false;
@@ -2392,14 +2149,13 @@ public boolean similar(Object other) {
/**
* Compares two numbers to see if they are similar.
- *
- * If either of the numbers are Double or Float instances, then they are checked to have
- * a finite value. If either value is not finite (NaN or ±infinity), then this
- * function will always return false. If both numbers are finite, they are first checked
- * to be the same type and implement {@link Comparable}. If they do, then the actual
- * {@link Comparable#compareTo(Object)} is called. If they are not the same type, or don't
- * implement Comparable, then they are converted to {@link BigDecimal}s. Finally the
- * BigDecimal values are compared using {@link BigDecimal#compareTo(BigDecimal)}.
+ *
+ * If either of the numbers are Double or Float instances, then they are checked to have a finite value. If either
+ * value is not finite (NaN or ±infinity), then this function will always return false. If both numbers are
+ * finite, they are first checked to be the same type and implement {@link Comparable}. If they do, then the actual
+ * {@link Comparable#compareTo(Object)} is called. If they are not the same type, or don't implement Comparable,
+ * then they are converted to {@link BigDecimal}s. Finally the BigDecimal values are compared using
+ * {@link BigDecimal#compareTo(BigDecimal)}.
*
* @param l the Left value to compare. Can not be null
.
* @param r the right value to compare. Can not be null
.
@@ -2413,10 +2169,10 @@ static boolean isNumberSimilar(Number l, Number r) {
// if the classes are the same and implement Comparable
// then use the built in compare first.
- if(l.getClass().equals(r.getClass()) && l instanceof Comparable) {
- @SuppressWarnings({ "rawtypes", "unchecked" })
- int compareTo = ((Comparable)l).compareTo(r);
- return compareTo==0;
+ if (l.getClass().equals(r.getClass()) && l instanceof Comparable) {
+ @SuppressWarnings({"rawtypes", "unchecked"})
+ int compareTo = ((Comparable) l).compareTo(r);
+ return compareTo == 0;
}
// BigDecimal should be able to handle all of our number types that we support through
@@ -2447,18 +2203,15 @@ private static boolean numberIsFinite(Number n) {
*/
protected static boolean isDecimalNotation(final String val) {
return val.indexOf('.') > -1 || val.indexOf('e') > -1
- || val.indexOf('E') > -1 || "-0".equals(val);
+ || val.indexOf('E') > -1 || "-0".equals(val);
}
/**
- * Try to convert a string into a number, boolean, or null. If the string
- * can't be converted, return the string.
+ * Try to convert a string into a number, boolean, or null. If the string can't be converted, return the string.
*
- * @param string
- * A String. can not be null.
+ * @param string A String. can not be null.
* @return A simple JSON value.
- * @throws NullPointerException
- * Thrown if the string is null.
+ * @throws NullPointerException Thrown if the string is null.
*/
// Changes to this method must be copied to the corresponding method in
// the XML class to keep full support for Android
@@ -2494,14 +2247,14 @@ public static Object stringToValue(String string) {
}
/**
- * Converts a string to a number using the narrowest possible type. Possible
- * returns for this function are BigDecimal, Double, BigInteger, Long, and Integer.
- * When a Double is returned, it should always be a valid Double and not NaN or +-infinity.
+ * Converts a string to a number using the narrowest possible type. Possible returns for this function are
+ * BigDecimal, Double, BigInteger, Long, and Integer. When a Double is returned, it should always be a valid Double
+ * and not NaN or +-infinity.
*
* @param val value to convert
* @return Number representation of the value.
- * @throws NumberFormatException thrown if the value is not a valid number. A public
- * caller should catch this and wrap it in a {@link JSONException} if applicable.
+ * @throws NumberFormatException thrown if the value is not a valid number. A public caller should catch this and
+ * wrap it in a {@link JSONException} if applicable.
*/
protected static Number stringToNumber(final String val) throws NumberFormatException {
char initial = val.charAt(0);
@@ -2513,7 +2266,7 @@ protected static Number stringToNumber(final String val) throws NumberFormatExce
// keep that by forcing a decimal.
try {
BigDecimal bd = new BigDecimal(val);
- if(initial == '-' && BigDecimal.ZERO.compareTo(bd)==0) {
+ if (initial == '-' && BigDecimal.ZERO.compareTo(bd) == 0) {
return Double.valueOf(-0.0);
}
return bd;
@@ -2521,26 +2274,26 @@ protected static Number stringToNumber(final String val) throws NumberFormatExce
// this is to support "Hex Floats" like this: 0x1.0P-1074
try {
Double d = Double.valueOf(val);
- if(d.isNaN() || d.isInfinite()) {
- throw new NumberFormatException("val ["+val+"] is not a valid number.");
+ if (d.isNaN() || d.isInfinite()) {
+ throw new NumberFormatException("val [" + val + "] is not a valid number.");
}
return d;
} catch (NumberFormatException ignore) {
- throw new NumberFormatException("val ["+val+"] is not a valid number.");
+ throw new NumberFormatException("val [" + val + "] is not a valid number.");
}
}
}
// block items like 00 01 etc. Java number parsers treat these as Octal.
- if(initial == '0' && val.length() > 1) {
+ if (initial == '0' && val.length() > 1) {
char at1 = val.charAt(1);
- if(at1 >= '0' && at1 <= '9') {
- throw new NumberFormatException("val ["+val+"] is not a valid number.");
+ if (at1 >= '0' && at1 <= '9') {
+ throw new NumberFormatException("val [" + val + "] is not a valid number.");
}
} else if (initial == '-' && val.length() > 2) {
char at1 = val.charAt(1);
char at2 = val.charAt(2);
- if(at1 == '0' && at2 >= '0' && at2 <= '9') {
- throw new NumberFormatException("val ["+val+"] is not a valid number.");
+ if (at1 == '0' && at2 >= '0' && at2 <= '9') {
+ throw new NumberFormatException("val [" + val + "] is not a valid number.");
}
}
// integer representation.
@@ -2552,24 +2305,22 @@ protected static Number stringToNumber(final String val) throws NumberFormatExce
// only what they need. i.e. Less runtime overhead if the value is
// long lived.
BigInteger bi = new BigInteger(val);
- if(bi.bitLength() <= 31){
+ if (bi.bitLength() <= 31) {
return Integer.valueOf(bi.intValue());
}
- if(bi.bitLength() <= 63){
+ if (bi.bitLength() <= 63) {
return Long.valueOf(bi.longValue());
}
return bi;
}
- throw new NumberFormatException("val ["+val+"] is not a valid number.");
+ throw new NumberFormatException("val [" + val + "] is not a valid number.");
}
/**
* Throw an exception if the object is a NaN or infinite number.
*
- * @param o
- * The object to test.
- * @throws JSONException
- * If o is a non-finite number.
+ * @param o The object to test.
+ * @throws JSONException If o is a non-finite number.
*/
public static void testValidity(Object o) throws JSONException {
if (o instanceof Number && !numberIsFinite((Number) o)) {
@@ -2578,15 +2329,12 @@ public static void testValidity(Object o) throws JSONException {
}
/**
- * Produce a JSONArray containing the values of the members of this
- * JSONObject.
+ * 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.
+ * @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.
+ * @throws JSONException If any of the values are non-finite numbers.
*/
public JSONArray toJSONArray(JSONArray names) throws JSONException {
if (names == null || names.isEmpty()) {
@@ -2600,17 +2348,15 @@ public JSONArray toJSONArray(JSONArray names) throws JSONException {
}
/**
- * 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.
+ * Make a JSON text of this JSONObject. For compactness, no whitespace is added. If this would not result in a
+ * syntactically correct JSON text, then null will be returned instead.
*
* Warning: This method assumes that the data structure is acyclical.
*
*
- * @return a printable, displayable, portable, transmittable representation
- * of the object, beginning with {
(left
- * brace) and ending with }
(right
- * brace) .
+ * @return a printable, displayable, portable, transmittable representation of the object, beginning with
+ * {
(left brace) and ending with }
(right
+ * brace) .
*/
@Override
public String toString() {
@@ -2638,14 +2384,11 @@ public String toString() {
* Warning: This method assumes that the data structure is acyclical.
*
*
- * @param indentFactor
- * The number of spaces to add to each level of indentation.
- * @return a printable, displayable, portable, transmittable representation
- * of the object, beginning with {
(left
- * brace) and ending with }
(right
- * brace) .
- * @throws JSONException
- * If the object contains an invalid number.
+ * @param indentFactor The number of spaces to add to each level of indentation.
+ * @return a printable, displayable, portable, transmittable representation of the object, beginning with
+ * {
(left brace) and ending with }
(right
+ * brace) .
+ * @throws JSONException If the object contains an invalid number.
*/
@SuppressWarnings("resource")
public String toString(int indentFactor) throws JSONException {
@@ -2657,47 +2400,37 @@ public String toString(int indentFactor) throws JSONException {
}
/**
- * 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.
+ * Make a JSON text of an Object value. If the object has an value.toJSONString() method, then that method will be
+ * used to produce the JSON text. The method is required to produce a strictly conforming text. If the object does
+ * not contain a toJSONString method (which is the most common case), then a text will be produced by other means.
+ * If the value is an array or Collection, then a JSONArray will be made from it and its toJSONString method will be
+ * called. If the value is a MAP, then a JSONObject will be made from it and its toJSONString method will be called.
+ * Otherwise, the value's toString method will be called, and the result will be quoted.
*
*
* Warning: This method assumes that the data structure is acyclical.
*
- * @param value
- * The value to be serialized.
- * @return a printable, displayable, transmittable representation of the
- * object, beginning with {
(left
- * brace) and ending with }
(right
- * brace) .
- * @throws JSONException
- * If the value is or contains an invalid number.
+ * @param value The value to be serialized.
+ * @return a printable, displayable, transmittable representation of the object, beginning with
+ * {
(left brace) and ending with }
(right
+ * brace) .
+ * @throws JSONException If the value is or contains an invalid number.
*/
public static String valueToString(Object value) throws JSONException {
- // 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.
+ // 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 null
, return the NULL
- * object. If it is an array or collection, wrap it in a JSONArray. If it is
- * a map, wrap it in a JSONObject. If it is a standard property (Double,
- * String, et al) then it is already wrapped. Otherwise, if it comes from
- * one of the java packages, turn it into a string. And if it doesn't, try
- * to wrap it in a JSONObject. If the wrapping fails, then null is returned.
+ * Wrap an object, if necessary. If the object is null
, return the NULL object. If it is an array or
+ * collection, wrap it in a JSONArray. If it is a map, wrap it in a JSONObject. If it is a standard property
+ * (Double, String, et al) then it is already wrapped. Otherwise, if it comes from one of the java packages, turn it
+ * into a string. And if it doesn't, try to wrap it in a JSONObject. If the wrapping fails, then null is returned.
*
- * @param object
- * The object to wrap
+ * @param object The object to wrap
* @return The wrapped value
*/
public static Object wrap(Object object) {
@@ -2705,42 +2438,38 @@ public static Object wrap(Object object) {
}
/**
- * Wrap an object, if necessary. If the object is null
, return the NULL
- * object. If it is an array or collection, wrap it in a JSONArray. If it is
- * a map, wrap it in a JSONObject. If it is a standard property (Double,
- * String, et al) then it is already wrapped. Otherwise, if it comes from
- * one of the java packages, turn it into a string. And if it doesn't, try
- * to wrap it in a JSONObject. If the wrapping fails, then null is returned.
+ * Wrap an object, if necessary. If the object is null
, return the NULL object. If it is an array or
+ * collection, wrap it in a JSONArray. If it is a map, wrap it in a JSONObject. If it is a standard property
+ * (Double, String, et al) then it is already wrapped. Otherwise, if it comes from one of the java packages, turn it
+ * into a string. And if it doesn't, try to wrap it in a JSONObject. If the wrapping fails, then null is returned.
*
- * @param object
- * The object to wrap
- * @param recursionDepth
- * Variable for tracking the count of nested object creations.
- * @param jsonParserConfiguration
- * Variable to pass parser custom configuration for json parsing.
+ * @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
*/
static Object wrap(Object object, int recursionDepth, JSONParserConfiguration jsonParserConfiguration) {
- return wrap(object, null, recursionDepth, jsonParserConfiguration);
+ return wrap(object, null, recursionDepth, jsonParserConfiguration);
}
private static Object wrap(Object object, Set objectsRecord) {
- return wrap(object, objectsRecord, 0, new JSONParserConfiguration());
+ return wrap(object, objectsRecord, 0, new JSONParserConfiguration());
}
- private static Object wrap(Object object, Set objectsRecord, int recursionDepth, JSONParserConfiguration jsonParserConfiguration) {
+ private static Object wrap(Object object, Set objectsRecord, int recursionDepth,
+ JSONParserConfiguration jsonParserConfiguration) {
try {
if (NULL.equals(object)) {
return NULL;
}
if (object instanceof JSONObject || object instanceof JSONArray
- || NULL.equals(object) || object instanceof JSONString
- || 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 String || object instanceof BigInteger
- || object instanceof BigDecimal || object instanceof Enum) {
+ || NULL.equals(object) || object instanceof JSONString
+ || 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 String || object instanceof BigInteger
+ || object instanceof BigDecimal || object instanceof Enum) {
return object;
}
@@ -2757,18 +2486,17 @@ private static Object wrap(Object object, Set objectsRecord, int recursi
}
Package objectPackage = object.getClass().getPackage();
String objectPackageName = objectPackage != null ? objectPackage
- .getName() : "";
+ .getName() : "";
if (objectPackageName.startsWith("java.")
- || objectPackageName.startsWith("javax.")
- || object.getClass().getClassLoader() == null) {
+ || 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) {
+ } catch (JSONException exception) {
throw exception;
} catch (Exception exception) {
return null;
@@ -2776,11 +2504,11 @@ private static Object wrap(Object object, Set objectsRecord, int recursi
}
/**
- * Write the contents of the JSONObject as JSON text to a writer. For
- * compactness, no whitespace is added.
+ * Write the contents of the JSONObject as JSON text to a writer. For compactness, no whitespace is added.
*
* Warning: This method assumes that the data structure is acyclical.
*
+ *
* @param writer the writer object
* @return The writer.
* @throws JSONException if a called function has an error
@@ -2791,7 +2519,7 @@ public Writer write(Writer writer) throws JSONException {
@SuppressWarnings("resource")
static final Writer writeValue(Writer writer, Object value,
- int indentFactor, int indent) throws JSONException, IOException {
+ int indentFactor, int indent) throws JSONException, IOException {
if (value == null || value.equals(null)) {
writer.write("null");
} else if (value instanceof JSONString) {
@@ -2810,7 +2538,7 @@ static final Writer writeValue(Writer writer, Object value,
} else if (value instanceof Number) {
// not all Numbers may match actual JSON Numbers. i.e. fractions or Imaginary
final String numberAsString = numberToString((Number) value);
- if(NUMBER_PATTERN.matcher(numberAsString).matches()) {
+ if (NUMBER_PATTERN.matcher(numberAsString).matches()) {
writer.write(numberAsString);
} else {
// The Number value is not a valid JSON number.
@@ -2820,7 +2548,7 @@ static final Writer writeValue(Writer writer, Object value,
} else if (value instanceof Boolean) {
writer.write(value.toString());
} else if (value instanceof Enum>) {
- writer.write(quote(((Enum>)value).name()));
+ writer.write(quote(((Enum>) value).name()));
} else if (value instanceof JSONObject) {
((JSONObject) value).write(writer, indentFactor, indent);
} else if (value instanceof JSONArray) {
@@ -2862,40 +2590,36 @@ static final void indent(Writer writer, int indent) throws IOException {
* Warning: This method assumes that the data structure is acyclical.
*
*
- * @param writer
- * Writes the serialized JSON
- * @param indentFactor
- * The number of spaces to add to each level of indentation.
- * @param indent
- * The indentation of the top level.
+ * @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
+ * @throws JSONException if a called function has an error or a write error occurs
*/
@SuppressWarnings("resource")
public Writer write(Writer writer, int indentFactor, int indent)
- throws JSONException {
+ throws JSONException {
try {
boolean needsComma = false;
final int length = this.length();
writer.write('{');
if (length == 1) {
- final Entry entry = this.entrySet().iterator().next();
+ final Entry entry = this.entrySet().iterator().next();
final String key = entry.getKey();
writer.write(quote(key));
writer.write(':');
if (indentFactor > 0) {
writer.write(' ');
}
- try{
+ try {
writeValue(writer, entry.getValue(), indentFactor, indent);
} catch (Exception e) {
throw new JSONException("Unable to write JSONObject value for key: " + key, e);
}
} else if (length != 0) {
final int newIndent = indent + indentFactor;
- for (final Entry entry : this.entrySet()) {
+ for (final Entry entry : this.entrySet()) {
if (needsComma) {
writer.write(',');
}
@@ -2929,9 +2653,8 @@ public Writer write(Writer writer, int indentFactor, int indent)
}
/**
- * 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.
+ * 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.
*
* Warning: This method assumes that the data structure is acyclical.
*
@@ -2957,35 +2680,37 @@ public Map toMap() {
/**
* Create a new JSONException in a common format for incorrect conversions.
- * @param key name of the key
+ *
+ * @param key name of the key
* @param valueType the type of value being coerced to
- * @param cause optional cause of the coercion failure
+ * @param cause optional cause of the coercion failure
* @return JSONException that can be thrown.
*/
private static JSONException wrongValueFormatException(
- String key,
- String valueType,
- Object value,
- Throwable cause) {
- if(value == null) {
+ String key,
+ String valueType,
+ Object value,
+ Throwable cause) {
+ if (value == null) {
return new JSONException(
- "JSONObject[" + quote(key) + "] is not a " + valueType + " (null)."
- , cause);
+ "JSONObject[" + 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) {
+ if (value instanceof Map || value instanceof Iterable || value instanceof JSONObject) {
return new JSONException(
- "JSONObject[" + quote(key) + "] is not a " + valueType + " (" + value.getClass() + ")."
- , cause);
+ "JSONObject[" + quote(key) + "] is not a " + valueType + " (" + value.getClass() + ")."
+ , cause);
}
return new JSONException(
- "JSONObject[" + quote(key) + "] is not a " + valueType + " (" + value.getClass() + " : " + value + ")."
- , cause);
+ "JSONObject[" + quote(key) + "] is not a " + valueType + " (" + value.getClass() + " : " + value + ")."
+ , cause);
}
/**
* Create a new JSONException in a common format for recursive object definition.
+ *
* @param key name of the key
* @return JSONException that can be thrown.
*/
@@ -2997,21 +2722,28 @@ private static JSONException recursivelyDefinedObjectException(String key) {
/**
* For a prospective number, remove the leading zeros
+ *
* @param value prospective number
* @return number without leading zeros
*/
- private static String removeLeadingZerosOfNumber(String value){
- if (value.equals("-")){return value;}
+ private static String removeLeadingZerosOfNumber(String value) {
+ if (value.equals("-")) {
+ return value;
+ }
boolean negativeFirstChar = (value.charAt(0) == '-');
- int counter = negativeFirstChar ? 1:0;
- while (counter < value.length()){
- if (value.charAt(counter) != '0'){
- if (negativeFirstChar) {return "-".concat(value.substring(counter));}
+ int counter = negativeFirstChar ? 1 : 0;
+ while (counter < value.length()) {
+ if (value.charAt(counter) != '0') {
+ if (negativeFirstChar) {
+ return "-".concat(value.substring(counter));
+ }
return value.substring(counter);
}
++counter;
}
- if (negativeFirstChar) {return "-0";}
+ if (negativeFirstChar) {
+ return "-0";
+ }
return "0";
}
}
diff --git a/src/test/java/org/json/junit/JSONParserConfigurationTest.java b/src/test/java/org/json/junit/JSONParserConfigurationTest.java
index 427aad4df..f3f4b0e23 100644
--- a/src/test/java/org/json/junit/JSONParserConfigurationTest.java
+++ b/src/test/java/org/json/junit/JSONParserConfigurationTest.java
@@ -46,6 +46,44 @@ public void givenInvalidInputArrays_testStrictModeTrue_shouldThrowJsonException(
() -> new JSONArray(testCase, jsonParserConfiguration)));
}
+ @Test
+ public void givenInvalidJsonObjects_testStrictModeTrue_shouldThrowJsonException() throws IOException {
+ /*assertThrows(JSONException.class,
+ () -> new JSONObject("{}abc", new JSONParserConfiguration().withStrictMode(true)));*/
+
+ try (final Stream lines = Files.lines(Paths.get("src/test/resources/Issue884-validJsonObj.json"))) {
+ final String resultJsonAsString = lines.collect(Collectors.joining());
+
+ final JSONObject jsonObject = new JSONObject(resultJsonAsString, new JSONParserConfiguration().withStrictMode(true));
+
+ System.out.println(jsonObject.toString(2));
+ } catch (Exception e){
+ e.printStackTrace();
+ }
+
+ //JSONObject jsonObject = new JSONObject("{\"test\": {\"key\": \"val\", \"key2\"}}", new JSONParserConfiguration().withStrictMode(true));
+ //JSONObject jsonObject2 = new JSONObject("{}}", new JSONParserConfiguration().withStrictMode(true));
+ }
+
+ @Test
+ public void givenValidJsonObject_testStrictModeTrue_shouldThrowJsonException() throws IOException {
+ /*assertThrows(JSONException.class,
+ () -> new JSONObject("{}abc", new JSONParserConfiguration().withStrictMode(true)));*/
+
+ try (final Stream lines = Files.lines(Paths.get("src/test/resources/Issue884-validJsonObj.json"))) {
+ final String resultJsonAsString = lines.collect(Collectors.joining());
+
+ final JSONObject jsonObject = new JSONObject(resultJsonAsString, new JSONParserConfiguration().withStrictMode(true));
+
+ System.out.println(jsonObject.toString(2));
+ } catch (Exception e){
+ e.printStackTrace();
+ }
+
+ //JSONObject jsonObject = new JSONObject("{\"test\": {\"key\": \"val\", \"key2\"}}", new JSONParserConfiguration().withStrictMode(true));
+ //JSONObject jsonObject2 = new JSONObject("{}}", new JSONParserConfiguration().withStrictMode(true));
+ }
+
@Test
public void givenEmptyArray_testStrictModeTrue_shouldNotThrowJsonException() {
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
@@ -76,7 +114,7 @@ public void givenValidDoubleArray_testStrictModeTrue_shouldNotThrowJsonException
}
@Test
- public void givenValidEmptyArrayInsideArray_testStrictModeTrue_shouldNotThrowJsonException(){
+ public void givenValidEmptyArrayInsideArray_testStrictModeTrue_shouldNotThrowJsonException() {
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
.withStrictMode(true);
@@ -88,7 +126,7 @@ public void givenValidEmptyArrayInsideArray_testStrictModeTrue_shouldNotThrowJso
}
@Test
- public void givenValidEmptyArrayInsideArray_testStrictModeFalse_shouldNotThrowJsonException(){
+ public void givenValidEmptyArrayInsideArray_testStrictModeFalse_shouldNotThrowJsonException() {
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
.withStrictMode(false);
@@ -242,8 +280,12 @@ public void givenUnbalancedQuotes_testStrictModeFalse_shouldThrowJsonException()
JSONException jeTwo = assertThrows(JSONException.class,
() -> new JSONArray(testCaseTwo, jsonParserConfiguration));
- assertEquals("Unterminated string. Character with int code 0 is not allowed within a quoted string. at 15 [character 16 line 1]", jeOne.getMessage());
- assertEquals("Unterminated string. Character with int code 0 is not allowed within a quoted string. at 15 [character 16 line 1]", jeTwo.getMessage());
+ assertEquals(
+ "Unterminated string. Character with int code 0 is not allowed within a quoted string. at 15 [character 16 line 1]",
+ jeOne.getMessage());
+ assertEquals(
+ "Unterminated string. Character with int code 0 is not allowed within a quoted string. at 15 [character 16 line 1]",
+ jeTwo.getMessage());
}
diff --git a/src/test/resources/Issue884-validJsonObj.json b/src/test/resources/Issue884-validJsonObj.json
new file mode 100644
index 000000000..3c08c3e5b
--- /dev/null
+++ b/src/test/resources/Issue884-validJsonObj.json
@@ -0,0 +1,14 @@
+{
+ "test": {
+ "key": "value",
+ "key2": "value",
+ "key3": {
+ "anotherNested": "obj",
+ "anArray": [
+ "val1",
+ 1,
+ 3
+ ]
+ }
+ }
+}
\ No newline at end of file