mirror of
https://github.com/ethauvin/JSON-java.git
synced 2025-06-17 07:50:52 -07:00
reduces the use of unnecessary exceptions
This commit is contained in:
parent
04181fb6e2
commit
7627d40d10
2 changed files with 336 additions and 216 deletions
188
JSONArray.java
188
JSONArray.java
|
@ -72,13 +72,13 @@ import java.util.Map;
|
|||
* <li>Strings do not need to be quoted at all if they do not begin with a quote
|
||||
* or single quote, and if they do not contain leading or trailing spaces, and
|
||||
* if they do not contain any of these characters:
|
||||
* <code>{ } [ ] / \ : , #</code> and if they do not look like numbers and
|
||||
* if they are not the reserved words <code>true</code>, <code>false</code>, or
|
||||
* <code>{ } [ ] / \ : , #</code> and if they do not look like numbers and if
|
||||
* they are not the reserved words <code>true</code>, <code>false</code>, or
|
||||
* <code>null</code>.</li>
|
||||
* </ul>
|
||||
*
|
||||
* @author JSON.org
|
||||
* @version 2016-05-20
|
||||
* @version 2016-07-08
|
||||
*/
|
||||
public class JSONArray implements Iterable<Object> {
|
||||
|
||||
|
@ -241,11 +241,14 @@ public class JSONArray implements Iterable<Object> {
|
|||
public double getDouble(int index) throws JSONException {
|
||||
Object object = this.get(index);
|
||||
try {
|
||||
return object instanceof Number ? ((Number) object).doubleValue()
|
||||
: Double.parseDouble((String) object);
|
||||
} catch (Exception e) {
|
||||
throw new JSONException("JSONArray[" + index + "] is not a number.");
|
||||
if (object instanceof Number) {
|
||||
return ((Number) object).doubleValue();
|
||||
} else if (object instanceof String) {
|
||||
return Double.parseDouble((String) object);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
}
|
||||
throw new JSONException("JSONArray[" + index + "] is not a number.");
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -260,15 +263,17 @@ public class JSONArray implements Iterable<Object> {
|
|||
* if the key is not found or if the value cannot be converted
|
||||
* to an enum.
|
||||
*/
|
||||
public <E extends Enum<E>> E getEnum(Class<E> clazz, int index) throws JSONException {
|
||||
public <E extends Enum<E>> E getEnum(Class<E> clazz, int index)
|
||||
throws JSONException {
|
||||
E val = optEnum(clazz, index);
|
||||
if (val == null) {
|
||||
// JSONException should really take a throwable argument.
|
||||
// If it did, I would re-implement this with the Enum.valueOf
|
||||
// method and place any thrown exception in the JSONException
|
||||
throw new JSONException("JSONObject[" + JSONObject.quote(Integer.toString(index))
|
||||
+ "] is not an enum of type " + JSONObject.quote(clazz.getSimpleName())
|
||||
+ ".");
|
||||
throw new JSONException("JSONObject["
|
||||
+ JSONObject.quote(Integer.toString(index))
|
||||
+ "] is not an enum of type "
|
||||
+ JSONObject.quote(clazz.getSimpleName()) + ".");
|
||||
}
|
||||
return val;
|
||||
}
|
||||
|
@ -288,8 +293,8 @@ public class JSONArray implements Iterable<Object> {
|
|||
try {
|
||||
return new BigDecimal(object.toString());
|
||||
} catch (Exception e) {
|
||||
throw new JSONException("JSONArray[" + index +
|
||||
"] could not convert to BigDecimal.");
|
||||
throw new JSONException("JSONArray[" + index
|
||||
+ "] could not convert to BigDecimal.");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -308,8 +313,8 @@ public class JSONArray implements Iterable<Object> {
|
|||
try {
|
||||
return new BigInteger(object.toString());
|
||||
} catch (Exception e) {
|
||||
throw new JSONException("JSONArray[" + index +
|
||||
"] could not convert to BigInteger.");
|
||||
throw new JSONException("JSONArray[" + index
|
||||
+ "] could not convert to BigInteger.");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -325,11 +330,14 @@ public class JSONArray implements Iterable<Object> {
|
|||
public int getInt(int index) throws JSONException {
|
||||
Object object = this.get(index);
|
||||
try {
|
||||
return object instanceof Number ? ((Number) object).intValue()
|
||||
: Integer.parseInt((String) object);
|
||||
} catch (Exception e) {
|
||||
throw new JSONException("JSONArray[" + index + "] is not a number.");
|
||||
if (object instanceof Number) {
|
||||
return ((Number) object).intValue();
|
||||
} else if (object instanceof String) {
|
||||
return Integer.parseInt((String) object);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
}
|
||||
throw new JSONException("JSONArray[" + index + "] is not a number.");
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -381,11 +389,14 @@ public class JSONArray implements Iterable<Object> {
|
|||
public long getLong(int index) throws JSONException {
|
||||
Object object = this.get(index);
|
||||
try {
|
||||
return object instanceof Number ? ((Number) object).longValue()
|
||||
: Long.parseLong((String) object);
|
||||
} catch (Exception e) {
|
||||
throw new JSONException("JSONArray[" + index + "] is not a number.");
|
||||
if (object instanceof Number) {
|
||||
return ((Number) object).longValue();
|
||||
} else if (object instanceof String) {
|
||||
return Long.parseLong((String) object);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
}
|
||||
throw new JSONException("JSONArray[" + index + "] is not a number.");
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -486,11 +497,20 @@ public class JSONArray implements Iterable<Object> {
|
|||
* @return The truth.
|
||||
*/
|
||||
public boolean optBoolean(int index, boolean defaultValue) {
|
||||
try {
|
||||
return this.getBoolean(index);
|
||||
} catch (Exception e) {
|
||||
Object object = this.opt(index);
|
||||
if (JSONObject.NULL.equals(object)) {
|
||||
return defaultValue;
|
||||
}
|
||||
if (object.equals(Boolean.FALSE)
|
||||
|| (object instanceof String && ((String) object)
|
||||
.equalsIgnoreCase("false"))) {
|
||||
return false;
|
||||
} else if (object.equals(Boolean.TRUE)
|
||||
|| (object instanceof String && ((String) object)
|
||||
.equalsIgnoreCase("true"))) {
|
||||
return true;
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -518,11 +538,19 @@ public class JSONArray implements Iterable<Object> {
|
|||
* @return The value.
|
||||
*/
|
||||
public double optDouble(int index, double defaultValue) {
|
||||
try {
|
||||
return this.getDouble(index);
|
||||
} catch (Exception e) {
|
||||
Object object = this.opt(index);
|
||||
if (JSONObject.NULL.equals(object)) {
|
||||
return defaultValue;
|
||||
}
|
||||
try {
|
||||
if (object instanceof Number) {
|
||||
return ((Number) object).doubleValue();
|
||||
} else if (object instanceof String) {
|
||||
return Double.parseDouble((String) object);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -550,11 +578,19 @@ public class JSONArray implements Iterable<Object> {
|
|||
* @return The value.
|
||||
*/
|
||||
public int optInt(int index, int defaultValue) {
|
||||
try {
|
||||
return this.getInt(index);
|
||||
} catch (Exception e) {
|
||||
Object object = this.opt(index);
|
||||
if (JSONObject.NULL.equals(object)) {
|
||||
return defaultValue;
|
||||
}
|
||||
try {
|
||||
if (object instanceof Number) {
|
||||
return ((Number) object).intValue();
|
||||
} else if (object instanceof String) {
|
||||
return Integer.parseInt((String) object);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -579,10 +615,11 @@ public class JSONArray implements Iterable<Object> {
|
|||
* The index must be between 0 and length() - 1.
|
||||
* @param defaultValue
|
||||
* The default in case the value is not found
|
||||
* @return The enum value at the index location or defaultValue if
|
||||
* the value is not found or cannot be assigned to clazz
|
||||
* @return The enum value at the index location or defaultValue if the value
|
||||
* is not found or cannot be assigned to clazz
|
||||
*/
|
||||
public <E extends Enum<E>> E optEnum(Class<E> clazz, int index, E defaultValue) {
|
||||
public <E extends Enum<E>> E optEnum(Class<E> clazz, int index,
|
||||
E defaultValue) {
|
||||
try {
|
||||
Object val = this.opt(index);
|
||||
if (JSONObject.NULL.equals(val)) {
|
||||
|
@ -602,7 +639,6 @@ public class JSONArray implements Iterable<Object> {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the optional BigInteger value associated with an index. The
|
||||
* defaultValue is returned if there is no value for the index, or if the
|
||||
|
@ -615,8 +651,12 @@ public class JSONArray implements Iterable<Object> {
|
|||
* @return The value.
|
||||
*/
|
||||
public BigInteger optBigInteger(int index, BigInteger defaultValue) {
|
||||
Object object = this.opt(index);
|
||||
if (JSONObject.NULL.equals(object)) {
|
||||
return defaultValue;
|
||||
}
|
||||
try {
|
||||
return this.getBigInteger(index);
|
||||
return new BigInteger(object.toString());
|
||||
} catch (Exception e) {
|
||||
return defaultValue;
|
||||
}
|
||||
|
@ -634,8 +674,12 @@ public class JSONArray implements Iterable<Object> {
|
|||
* @return The value.
|
||||
*/
|
||||
public BigDecimal optBigDecimal(int index, BigDecimal defaultValue) {
|
||||
Object object = this.opt(index);
|
||||
if (JSONObject.NULL.equals(object)) {
|
||||
return defaultValue;
|
||||
}
|
||||
try {
|
||||
return this.getBigDecimal(index);
|
||||
return new BigDecimal(object.toString());
|
||||
} catch (Exception e) {
|
||||
return defaultValue;
|
||||
}
|
||||
|
@ -693,11 +737,19 @@ public class JSONArray implements Iterable<Object> {
|
|||
* @return The value.
|
||||
*/
|
||||
public long optLong(int index, long defaultValue) {
|
||||
try {
|
||||
return this.getLong(index);
|
||||
} catch (Exception e) {
|
||||
Object object = this.opt(index);
|
||||
if (JSONObject.NULL.equals(object)) {
|
||||
return defaultValue;
|
||||
}
|
||||
try {
|
||||
if (object instanceof Number) {
|
||||
return ((Number) object).longValue();
|
||||
} else if (object instanceof String) {
|
||||
return Long.parseLong((String) object);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -961,22 +1013,27 @@ public class JSONArray implements Iterable<Object> {
|
|||
}
|
||||
|
||||
/**
|
||||
* Creates a JSONPointer using an intialization string and tries to
|
||||
* match it to an item within this JSONArray. For example, given a
|
||||
* JSONArray initialized with this document:
|
||||
* Creates a JSONPointer using an initialization string and tries to match
|
||||
* it to an item within this JSONArray. For example, given a JSONArray
|
||||
* initialized with this document:
|
||||
*
|
||||
* <pre>
|
||||
* [
|
||||
* {"b":"c"}
|
||||
* ]
|
||||
* </pre>
|
||||
* and this JSONPointer string:
|
||||
* <pre>
|
||||
* "/0/b"
|
||||
* </pre>
|
||||
* Then this method will return the String "c"
|
||||
* A JSONPointerException may be thrown from code called by this method.
|
||||
*
|
||||
* @param jsonPointer string that can be used to create a JSONPointer
|
||||
* and this JSONPointer string:
|
||||
*
|
||||
* <pre>
|
||||
* "/0/b"
|
||||
* </pre>
|
||||
*
|
||||
* Then this method will return the String "c" A JSONPointerException may be
|
||||
* thrown from code called by this method.
|
||||
*
|
||||
* @param jsonPointer
|
||||
* string that can be used to create a JSONPointer
|
||||
* @return the item matched by the JSONPointer, otherwise null
|
||||
*/
|
||||
public Object query(String jsonPointer) {
|
||||
|
@ -984,12 +1041,14 @@ public class JSONArray implements Iterable<Object> {
|
|||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @param jsonPointer
|
||||
* the string representation of the JSON pointer
|
||||
* @return the queried value or {@code null}
|
||||
* @throws IllegalArgumentException if {@code jsonPointer} has invalid syntax
|
||||
* @throws IllegalArgumentException
|
||||
* if {@code jsonPointer} has invalid syntax
|
||||
*/
|
||||
public Object optQuery(String jsonPointer) {
|
||||
JSONPointer pointer = new JSONPointer(jsonPointer);
|
||||
|
@ -1009,16 +1068,16 @@ public class JSONArray implements Iterable<Object> {
|
|||
* was no value.
|
||||
*/
|
||||
public Object remove(int index) {
|
||||
return index >= 0 && index < this.length()
|
||||
? this.myArrayList.remove(index)
|
||||
: null;
|
||||
return index >= 0 && index < this.length() ? this.myArrayList
|
||||
.remove(index) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if two JSONArrays are similar.
|
||||
* They must contain similar sequences.
|
||||
* Determine if two JSONArrays are similar. They must contain similar
|
||||
* sequences.
|
||||
*
|
||||
* @param other The other JSONArray
|
||||
* @param other
|
||||
* The other JSONArray
|
||||
* @return true if they are equal
|
||||
*/
|
||||
public boolean similar(Object other) {
|
||||
|
@ -1081,6 +1140,7 @@ public class JSONArray implements Iterable<Object> {
|
|||
* @return a printable, displayable, transmittable representation of the
|
||||
* array.
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
try {
|
||||
return this.toString(0);
|
||||
|
@ -1174,9 +1234,9 @@ public class JSONArray implements Iterable<Object> {
|
|||
}
|
||||
|
||||
/**
|
||||
* Returns a java.util.List containing all of the elements in this array.
|
||||
* If an element in the array is a JSONArray or JSONObject it will also
|
||||
* be converted.
|
||||
* Returns a java.util.List containing all of the elements in this array. If
|
||||
* an element in the array is a JSONArray or JSONObject it will also be
|
||||
* converted.
|
||||
* <p>
|
||||
* Warning: This method assumes that the data structure is acyclical.
|
||||
*
|
||||
|
|
252
JSONObject.java
252
JSONObject.java
|
@ -46,32 +46,29 @@ import java.util.Set;
|
|||
* A JSONObject is an unordered collection of name/value pairs. Its external
|
||||
* form is a string wrapped in curly braces with colons between the names and
|
||||
* values, and commas between the values and names. The internal form is an
|
||||
* object having <code>get</code> and <code>opt</code> methods for accessing
|
||||
* the values by name, and <code>put</code> methods for adding or replacing
|
||||
* values by name. The values can be any of these types: <code>Boolean</code>,
|
||||
* object having <code>get</code> and <code>opt</code> methods for accessing the
|
||||
* values by name, and <code>put</code> methods for adding or replacing values
|
||||
* by name. The values can be any of these types: <code>Boolean</code>,
|
||||
* <code>JSONArray</code>, <code>JSONObject</code>, <code>Number</code>,
|
||||
* <code>String</code>, or the <code>JSONObject.NULL</code> object. A
|
||||
* JSONObject constructor can be used to convert an external form JSON text
|
||||
* into an internal form whose values can be retrieved with the
|
||||
* <code>get</code> and <code>opt</code> methods, or to convert values into a
|
||||
* JSON text using the <code>put</code> and <code>toString</code> methods. A
|
||||
* <code>get</code> method returns a value if one can be found, and throws an
|
||||
* exception if one cannot be found. An <code>opt</code> method returns a
|
||||
* default value instead of throwing an exception, and so is useful for
|
||||
* obtaining optional values.
|
||||
* <code>String</code>, or the <code>JSONObject.NULL</code> object. A JSONObject
|
||||
* constructor can be used to convert an external form JSON text into an
|
||||
* internal form whose values can be retrieved with the <code>get</code> and
|
||||
* <code>opt</code> methods, or to convert values into a JSON text using the
|
||||
* <code>put</code> and <code>toString</code> methods. A <code>get</code> method
|
||||
* returns a value if one can be found, and throws an exception if one cannot be
|
||||
* found. An <code>opt</code> method returns a default value instead of throwing
|
||||
* an exception, and so is useful for obtaining optional values.
|
||||
* <p>
|
||||
* The generic <code>get()</code> and <code>opt()</code> methods return an
|
||||
* object, which you can cast or query for type. There are also typed
|
||||
* <code>get</code> and <code>opt</code> methods that do type checking and type
|
||||
* coercion for you. The opt methods differ from the get methods in that they
|
||||
* do not throw. Instead, they return a specified value, such as null.
|
||||
* coercion for you. The opt methods differ from the get methods in that they do
|
||||
* not throw. Instead, they return a specified value, such as null.
|
||||
* <p>
|
||||
* The <code>put</code> methods add or replace values in an object. For
|
||||
* example,
|
||||
* The <code>put</code> methods add or replace values in an object. For example,
|
||||
*
|
||||
* <pre>
|
||||
* myString = new JSONObject()
|
||||
* .put("JSON", "Hello, World!").toString();
|
||||
* myString = new JSONObject().put("JSON", "Hello, World!").toString();
|
||||
* </pre>
|
||||
*
|
||||
* produces the string <code>{"JSON": "Hello, World"}</code>.
|
||||
|
@ -84,16 +81,16 @@ import java.util.Set;
|
|||
* before the closing brace.</li>
|
||||
* <li>Strings may be quoted with <code>'</code> <small>(single
|
||||
* quote)</small>.</li>
|
||||
* <li>Strings do not need to be quoted at all if they do not begin with a
|
||||
* quote or single quote, and if they do not contain leading or trailing
|
||||
* spaces, and if they do not contain any of these characters:
|
||||
* <code>{ } [ ] / \ : , #</code> and if they do not look like numbers and
|
||||
* if they are not the reserved words <code>true</code>, <code>false</code>,
|
||||
* or <code>null</code>.</li>
|
||||
* <li>Strings do not need to be quoted at all if they do not begin with a quote
|
||||
* or single quote, and if they do not contain leading or trailing spaces, and
|
||||
* if they do not contain any of these characters:
|
||||
* <code>{ } [ ] / \ : , #</code> and if they do not look like numbers and if
|
||||
* they are not the reserved words <code>true</code>, <code>false</code>, or
|
||||
* <code>null</code>.</li>
|
||||
* </ul>
|
||||
*
|
||||
* @author JSON.org
|
||||
* @version 2016-05-20
|
||||
* @version 2016-07-08
|
||||
*/
|
||||
public class JSONObject {
|
||||
/**
|
||||
|
@ -104,8 +101,8 @@ public class JSONObject {
|
|||
private static final class Null {
|
||||
|
||||
/**
|
||||
* There is only intended to be a single instance of the NULL object,
|
||||
* so the clone method returns itself.
|
||||
* There is only intended to be a single instance of the NULL object, so
|
||||
* the clone method returns itself.
|
||||
*
|
||||
* @return NULL.
|
||||
*/
|
||||
|
@ -132,6 +129,7 @@ public class JSONObject {
|
|||
*
|
||||
* @return The string "null".
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return "null";
|
||||
}
|
||||
|
@ -341,8 +339,10 @@ public class JSONObject {
|
|||
Object key = keys.nextElement();
|
||||
if (key != null) {
|
||||
|
||||
// Go through the path, ensuring that there is a nested JSONObject for each
|
||||
// segment except the last. Add the value using the last segment's name into
|
||||
// Go through the path, ensuring that there is a nested
|
||||
// JSONObject for each
|
||||
// segment except the last. Add the value using the last
|
||||
// segment's name into
|
||||
// the deepest nested JSONObject.
|
||||
|
||||
String[] path = ((String) key).split("\\.");
|
||||
|
@ -485,15 +485,16 @@ public class JSONObject {
|
|||
* if the key is not found or if the value cannot be converted
|
||||
* to an enum.
|
||||
*/
|
||||
public <E extends Enum<E>> E getEnum(Class<E> clazz, String key) throws JSONException {
|
||||
public <E extends Enum<E>> E getEnum(Class<E> clazz, String key)
|
||||
throws JSONException {
|
||||
E val = optEnum(clazz, key);
|
||||
if (val == null) {
|
||||
// JSONException should really take a throwable argument.
|
||||
// If it did, I would re-implement this with the Enum.valueOf
|
||||
// method and place any thrown exception in the JSONException
|
||||
throw new JSONException("JSONObject[" + quote(key)
|
||||
+ "] is not an enum of type " + quote(clazz.getSimpleName())
|
||||
+ ".");
|
||||
+ "] is not an enum of type "
|
||||
+ quote(clazz.getSimpleName()) + ".");
|
||||
}
|
||||
return val;
|
||||
}
|
||||
|
@ -530,8 +531,8 @@ public class JSONObject {
|
|||
* A key string.
|
||||
* @return The numeric value.
|
||||
* @throws JSONException
|
||||
* if the key is not found or if the value cannot
|
||||
* be converted to BigInteger.
|
||||
* 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);
|
||||
|
@ -539,7 +540,7 @@ public class JSONObject {
|
|||
return new BigInteger(object.toString());
|
||||
} catch (Exception e) {
|
||||
throw new JSONException("JSONObject[" + quote(key)
|
||||
+ "] could not be converted to BigInteger.");
|
||||
+ "] could not be converted to BigInteger.", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -550,8 +551,8 @@ public class JSONObject {
|
|||
* A key string.
|
||||
* @return The numeric value.
|
||||
* @throws JSONException
|
||||
* if the key is not found or if the value
|
||||
* cannot be converted to BigDecimal.
|
||||
* 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);
|
||||
|
@ -559,7 +560,7 @@ public class JSONObject {
|
|||
return new BigDecimal(object.toString());
|
||||
} catch (Exception e) {
|
||||
throw new JSONException("JSONObject[" + quote(key)
|
||||
+ "] could not be converted to BigDecimal.");
|
||||
+ "] could not be converted to BigDecimal.", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -576,13 +577,16 @@ public class JSONObject {
|
|||
public double getDouble(String key) throws JSONException {
|
||||
Object object = this.get(key);
|
||||
try {
|
||||
return object instanceof Number ? ((Number) object).doubleValue()
|
||||
: Double.parseDouble((String) object);
|
||||
if (object instanceof Number) {
|
||||
return ((Number) object).doubleValue();
|
||||
} else if (object instanceof String) {
|
||||
return Double.parseDouble((String) object);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
}
|
||||
throw new JSONException("JSONObject[" + quote(key)
|
||||
+ "] is not a number.");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the int value associated with a key.
|
||||
|
@ -597,12 +601,14 @@ public class JSONObject {
|
|||
public int getInt(String key) throws JSONException {
|
||||
Object object = this.get(key);
|
||||
try {
|
||||
return object instanceof Number ? ((Number) object).intValue()
|
||||
: Integer.parseInt((String) object);
|
||||
} catch (Exception e) {
|
||||
throw new JSONException("JSONObject[" + quote(key)
|
||||
+ "] is not an int.");
|
||||
if (object instanceof Number) {
|
||||
return ((Number) object).intValue();
|
||||
} else if (object instanceof String) {
|
||||
return Integer.parseInt((String) object);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
}
|
||||
throw new JSONException("JSONObject[" + quote(key) + "] is not an int.");
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -654,12 +660,14 @@ public class JSONObject {
|
|||
public long getLong(String key) throws JSONException {
|
||||
Object object = this.get(key);
|
||||
try {
|
||||
return object instanceof Number ? ((Number) object).longValue()
|
||||
: Long.parseLong((String) object);
|
||||
} catch (Exception e) {
|
||||
throw new JSONException("JSONObject[" + quote(key)
|
||||
+ "] is not a long.");
|
||||
if (object instanceof Number) {
|
||||
return ((Number) object).longValue();
|
||||
} else if (object instanceof String) {
|
||||
return Long.parseLong((String) object);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
}
|
||||
throw new JSONException("JSONObject[" + quote(key) + "] is not a long.");
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -776,7 +784,7 @@ public class JSONObject {
|
|||
* is the JSONObject.NULL object.
|
||||
*/
|
||||
public boolean isNull(String key) {
|
||||
return JSONObject.NULL.equals(this.opt(key));
|
||||
return NULL.equals(this.opt(key));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -885,10 +893,11 @@ public class JSONObject {
|
|||
* 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
|
||||
* @return The enum value associated with the key or defaultValue if the
|
||||
* value is not found or cannot be assigned to clazz
|
||||
*/
|
||||
public <E extends Enum<E>> E optEnum(Class<E> clazz, String key, E defaultValue) {
|
||||
public <E extends Enum<E>> E optEnum(Class<E> clazz, String key,
|
||||
E defaultValue) {
|
||||
try {
|
||||
Object val = this.opt(key);
|
||||
if (NULL.equals(val)) {
|
||||
|
@ -932,11 +941,20 @@ public class JSONObject {
|
|||
* @return The truth.
|
||||
*/
|
||||
public boolean optBoolean(String key, boolean defaultValue) {
|
||||
try {
|
||||
return this.getBoolean(key);
|
||||
} catch (Exception e) {
|
||||
Object object = this.get(key);
|
||||
if (NULL.equals(object)) {
|
||||
return defaultValue;
|
||||
}
|
||||
if (object.equals(Boolean.FALSE)
|
||||
|| (object instanceof String && ((String) object)
|
||||
.equalsIgnoreCase("false"))) {
|
||||
return false;
|
||||
} else if (object.equals(Boolean.TRUE)
|
||||
|| (object instanceof String && ((String) object)
|
||||
.equalsIgnoreCase("true"))) {
|
||||
return true;
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -964,8 +982,12 @@ public class JSONObject {
|
|||
* @return An object which is the value.
|
||||
*/
|
||||
public BigInteger optBigInteger(String key, BigInteger defaultValue) {
|
||||
Object object = this.get(key);
|
||||
if (NULL.equals(object)) {
|
||||
return defaultValue;
|
||||
}
|
||||
try {
|
||||
return this.getBigInteger(key);
|
||||
return new BigInteger(object.toString());
|
||||
} catch (Exception e) {
|
||||
return defaultValue;
|
||||
}
|
||||
|
@ -983,8 +1005,12 @@ public class JSONObject {
|
|||
* @return An object which is the value.
|
||||
*/
|
||||
public BigDecimal optBigDecimal(String key, BigDecimal defaultValue) {
|
||||
Object object = this.opt(key);
|
||||
if (NULL.equals(object)) {
|
||||
return defaultValue;
|
||||
}
|
||||
try {
|
||||
return this.getBigDecimal(key);
|
||||
return new BigDecimal(object.toString());
|
||||
} catch (Exception e) {
|
||||
return defaultValue;
|
||||
}
|
||||
|
@ -1002,11 +1028,19 @@ public class JSONObject {
|
|||
* @return An object which is the value.
|
||||
*/
|
||||
public double optDouble(String key, double defaultValue) {
|
||||
try {
|
||||
return this.getDouble(key);
|
||||
} catch (Exception e) {
|
||||
Object object = this.get(key);
|
||||
if (NULL.equals(object)) {
|
||||
return defaultValue;
|
||||
}
|
||||
try {
|
||||
if (object instanceof Number) {
|
||||
return ((Number) object).doubleValue();
|
||||
} else if (object instanceof String) {
|
||||
return Double.parseDouble((String) object);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1034,11 +1068,19 @@ public class JSONObject {
|
|||
* @return An object which is the value.
|
||||
*/
|
||||
public int optInt(String key, int defaultValue) {
|
||||
try {
|
||||
return this.getInt(key);
|
||||
} catch (Exception e) {
|
||||
Object object = this.get(key);
|
||||
if (NULL.equals(object)) {
|
||||
return defaultValue;
|
||||
}
|
||||
try {
|
||||
if (object instanceof Number) {
|
||||
return ((Number) object).intValue();
|
||||
} else if (object instanceof String) {
|
||||
return Integer.parseInt((String) object);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1092,11 +1134,19 @@ public class JSONObject {
|
|||
* @return An object which is the value.
|
||||
*/
|
||||
public long optLong(String key, long defaultValue) {
|
||||
try {
|
||||
return this.getLong(key);
|
||||
} catch (Exception e) {
|
||||
Object object = this.get(key);
|
||||
if (NULL.equals(object)) {
|
||||
return defaultValue;
|
||||
}
|
||||
try {
|
||||
if (object instanceof Number) {
|
||||
return ((Number) object).longValue();
|
||||
} else if (object instanceof String) {
|
||||
return Long.parseLong((String) object);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1301,8 +1351,10 @@ public class JSONObject {
|
|||
* are both non-null, and only if there is not already a member with that
|
||||
* name.
|
||||
*
|
||||
* @param key string
|
||||
* @param value object
|
||||
* @param key
|
||||
* string
|
||||
* @param value
|
||||
* object
|
||||
* @return this.
|
||||
* @throws JSONException
|
||||
* if the key is a duplicate
|
||||
|
@ -1339,22 +1391,27 @@ public class JSONObject {
|
|||
}
|
||||
|
||||
/**
|
||||
* Creates a JSONPointer using an intialization 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:
|
||||
*
|
||||
* <pre>
|
||||
* {
|
||||
* "a":{"b":"c"}
|
||||
* }
|
||||
* </pre>
|
||||
* and this JSONPointer string:
|
||||
* <pre>
|
||||
* "/a/b"
|
||||
* </pre>
|
||||
* Then this method will return the String "c".
|
||||
* A JSONPointerException may be thrown from code called by this method.
|
||||
*
|
||||
* @param jsonPointer string that can be used to create a JSONPointer
|
||||
* and this JSONPointer string:
|
||||
*
|
||||
* <pre>
|
||||
* "/a/b"
|
||||
* </pre>
|
||||
*
|
||||
* Then this method will return the String "c". A JSONPointerException may
|
||||
* be thrown from code called by this method.
|
||||
*
|
||||
* @param jsonPointer
|
||||
* string that can be used to create a JSONPointer
|
||||
* @return the item matched by the JSONPointer, otherwise null
|
||||
*/
|
||||
public Object query(String jsonPointer) {
|
||||
|
@ -1362,12 +1419,14 @@ public class JSONObject {
|
|||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @param jsonPointer
|
||||
* the string representation of the JSON pointer
|
||||
* @return the queried value or {@code null}
|
||||
* @throws IllegalArgumentException if {@code jsonPointer} has invalid syntax
|
||||
* @throws IllegalArgumentException
|
||||
* if {@code jsonPointer} has invalid syntax
|
||||
*/
|
||||
public Object optQuery(String jsonPointer) {
|
||||
JSONPointer pointer = new JSONPointer(jsonPointer);
|
||||
|
@ -1472,11 +1531,11 @@ public class JSONObject {
|
|||
}
|
||||
|
||||
/**
|
||||
* Determine if two JSONObjects are similar.
|
||||
* They must contain the same set of names which must be associated with
|
||||
* similar values.
|
||||
* 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
|
||||
* @param other
|
||||
* The other JSONObject
|
||||
* @return true if they are equal
|
||||
*/
|
||||
public boolean similar(Object other) {
|
||||
|
@ -1542,8 +1601,7 @@ public class JSONObject {
|
|||
if ((initial >= '0' && initial <= '9') || initial == '-') {
|
||||
try {
|
||||
if (string.indexOf('.') > -1 || string.indexOf('e') > -1
|
||||
|| string.indexOf('E') > -1
|
||||
|| "-0".equals(string)) {
|
||||
|| string.indexOf('E') > -1 || "-0".equals(string)) {
|
||||
Double d = Double.valueOf(string);
|
||||
if (!d.isInfinite() && !d.isNaN()) {
|
||||
return d;
|
||||
|
@ -1621,6 +1679,7 @@ public class JSONObject {
|
|||
* brace)</small> and ending with <code>}</code> <small>(right
|
||||
* brace)</small>.
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
try {
|
||||
return this.toString(0);
|
||||
|
@ -1864,7 +1923,8 @@ public class JSONObject {
|
|||
if (indentFactor > 0) {
|
||||
writer.write(' ');
|
||||
}
|
||||
writeValue(writer, this.map.get(key), indentFactor, newindent);
|
||||
writeValue(writer, this.map.get(key), indentFactor,
|
||||
newindent);
|
||||
commanate = true;
|
||||
}
|
||||
if (indentFactor > 0) {
|
||||
|
@ -1880,13 +1940,13 @@ public class JSONObject {
|
|||
}
|
||||
|
||||
/**
|
||||
* Returns a java.util.Map containing all of the entrys 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.
|
||||
* <p>
|
||||
* Warning: This method assumes that the data structure is acyclical.
|
||||
*
|
||||
* @return a java.util.Map containing the entrys of this object
|
||||
* @return a java.util.Map containing the entries of this object
|
||||
*/
|
||||
public Map<String, Object> toMap() {
|
||||
Map<String, Object> results = new HashMap<String, Object>();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue