|
|
|
@ -96,10 +96,13 @@ public class JSONObject {
|
|
|
|
|
* method returns "null".
|
|
|
|
|
*/
|
|
|
|
|
public static final Object NULL = new Object() {
|
|
|
|
|
@Override public boolean equals(Object o) {
|
|
|
|
|
@Override
|
|
|
|
|
public boolean equals(Object o) {
|
|
|
|
|
return o == this || o == null; // API specifies this broken equals implementation
|
|
|
|
|
}
|
|
|
|
|
@Override public String toString() {
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public String toString() {
|
|
|
|
|
return "null";
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
@ -110,7 +113,7 @@ public class JSONObject {
|
|
|
|
|
* Creates a {@code JSONObject} with no name/value mappings.
|
|
|
|
|
*/
|
|
|
|
|
public JSONObject() {
|
|
|
|
|
nameValuePairs = new HashMap<String, Object>();
|
|
|
|
|
this.nameValuePairs = new HashMap<String, Object>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -134,7 +137,7 @@ public class JSONObject {
|
|
|
|
|
if (key == null) {
|
|
|
|
|
throw new NullPointerException("key == null");
|
|
|
|
|
}
|
|
|
|
|
nameValuePairs.put(key, wrap(entry.getValue()));
|
|
|
|
|
this.nameValuePairs.put(key, wrap(entry.getValue()));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -155,7 +158,8 @@ public class JSONObject {
|
|
|
|
|
Object object = readFrom.nextValue();
|
|
|
|
|
if (object instanceof JSONObject) {
|
|
|
|
|
this.nameValuePairs = ((JSONObject) object).nameValuePairs;
|
|
|
|
|
} else {
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
throw JSON.typeMismatch(object, "JSONObject");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -176,32 +180,38 @@ public class JSONObject {
|
|
|
|
|
* Creates a new {@code JSONObject} by copying mappings for the listed names
|
|
|
|
|
* from the given object. Names that aren't present in {@code copyFrom} will
|
|
|
|
|
* be skipped.
|
|
|
|
|
* @param copyFrom the source
|
|
|
|
|
* @param names the property names
|
|
|
|
|
* @throws JSONException if an error occurs
|
|
|
|
|
*/
|
|
|
|
|
public JSONObject(JSONObject copyFrom, String[] names) throws JSONException {
|
|
|
|
|
this();
|
|
|
|
|
for (String name : names) {
|
|
|
|
|
Object value = copyFrom.opt(name);
|
|
|
|
|
if (value != null) {
|
|
|
|
|
nameValuePairs.put(name, value);
|
|
|
|
|
this.nameValuePairs.put(name, value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the number of name/value mappings in this object.
|
|
|
|
|
* @return the number of name/value mappings in this object
|
|
|
|
|
*/
|
|
|
|
|
public int length() {
|
|
|
|
|
return nameValuePairs.size();
|
|
|
|
|
return this.nameValuePairs.size();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Maps {@code name} to {@code value}, clobbering any existing name/value
|
|
|
|
|
* mapping with the same name.
|
|
|
|
|
*
|
|
|
|
|
* @param name the name of the property
|
|
|
|
|
* @param value the value of the property
|
|
|
|
|
* @return this object.
|
|
|
|
|
* @throws JSONException if an error occurs
|
|
|
|
|
*/
|
|
|
|
|
public JSONObject put(String name, boolean value) throws JSONException {
|
|
|
|
|
nameValuePairs.put(checkName(name), value);
|
|
|
|
|
this.nameValuePairs.put(checkName(name), value);
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -209,12 +219,14 @@ public class JSONObject {
|
|
|
|
|
* Maps {@code name} to {@code value}, clobbering any existing name/value
|
|
|
|
|
* mapping with the same name.
|
|
|
|
|
*
|
|
|
|
|
* @param name the name of the property
|
|
|
|
|
* @param value a finite value. May not be {@link Double#isNaN() NaNs} or
|
|
|
|
|
* {@link Double#isInfinite() infinities}.
|
|
|
|
|
* @return this object.
|
|
|
|
|
* @throws JSONException if an error occurs
|
|
|
|
|
*/
|
|
|
|
|
public JSONObject put(String name, double value) throws JSONException {
|
|
|
|
|
nameValuePairs.put(checkName(name), JSON.checkDouble(value));
|
|
|
|
|
this.nameValuePairs.put(checkName(name), JSON.checkDouble(value));
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -222,10 +234,13 @@ public class JSONObject {
|
|
|
|
|
* Maps {@code name} to {@code value}, clobbering any existing name/value
|
|
|
|
|
* mapping with the same name.
|
|
|
|
|
*
|
|
|
|
|
* @param name the name of the property
|
|
|
|
|
* @param value the value of the property
|
|
|
|
|
* @return this object.
|
|
|
|
|
* @throws JSONException if an error occurs
|
|
|
|
|
*/
|
|
|
|
|
public JSONObject put(String name, int value) throws JSONException {
|
|
|
|
|
nameValuePairs.put(checkName(name), value);
|
|
|
|
|
this.nameValuePairs.put(checkName(name), value);
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -233,10 +248,13 @@ public class JSONObject {
|
|
|
|
|
* Maps {@code name} to {@code value}, clobbering any existing name/value
|
|
|
|
|
* mapping with the same name.
|
|
|
|
|
*
|
|
|
|
|
* @param name the name of the property
|
|
|
|
|
* @param value the value of the property
|
|
|
|
|
* @return this object.
|
|
|
|
|
* @throws JSONException if an error occurs
|
|
|
|
|
*/
|
|
|
|
|
public JSONObject put(String name, long value) throws JSONException {
|
|
|
|
|
nameValuePairs.put(checkName(name), value);
|
|
|
|
|
this.nameValuePairs.put(checkName(name), value);
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -245,28 +263,34 @@ public class JSONObject {
|
|
|
|
|
* mapping with the same name. If the value is {@code null}, any existing
|
|
|
|
|
* mapping for {@code name} is removed.
|
|
|
|
|
*
|
|
|
|
|
* @param name the name of the property
|
|
|
|
|
* @param value a {@link JSONObject}, {@link JSONArray}, String, Boolean,
|
|
|
|
|
* Integer, Long, Double, {@link #NULL}, or {@code null}. May not be
|
|
|
|
|
* {@link Double#isNaN() NaNs} or {@link Double#isInfinite()
|
|
|
|
|
* infinities}.
|
|
|
|
|
* @return this object.
|
|
|
|
|
* @throws JSONException if an error occurs
|
|
|
|
|
*/
|
|
|
|
|
public JSONObject put(String name, Object value) throws JSONException {
|
|
|
|
|
if (value == null) {
|
|
|
|
|
nameValuePairs.remove(name);
|
|
|
|
|
this.nameValuePairs.remove(name);
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
if (value instanceof Number) {
|
|
|
|
|
// deviate from the original by checking all Numbers, not just floats & doubles
|
|
|
|
|
JSON.checkDouble(((Number) value).doubleValue());
|
|
|
|
|
}
|
|
|
|
|
nameValuePairs.put(checkName(name), value);
|
|
|
|
|
this.nameValuePairs.put(checkName(name), value);
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Equivalent to {@code put(name, value)} when both parameters are non-null;
|
|
|
|
|
* does nothing otherwise.
|
|
|
|
|
* @param name the name of the property
|
|
|
|
|
* @param value the value of the property
|
|
|
|
|
* @return this object.
|
|
|
|
|
* @throws JSONException if an error occurs
|
|
|
|
|
*/
|
|
|
|
|
public JSONObject putOpt(String name, Object value) throws JSONException {
|
|
|
|
|
if (name == null || value == null) {
|
|
|
|
@ -283,12 +307,15 @@ public class JSONObject {
|
|
|
|
|
* mapped to {@code name}. In aggregate, this allows values to be added to a
|
|
|
|
|
* mapping one at a time.
|
|
|
|
|
*
|
|
|
|
|
* @param name the name of the property
|
|
|
|
|
* @param value a {@link JSONObject}, {@link JSONArray}, String, Boolean,
|
|
|
|
|
* Integer, Long, Double, {@link #NULL} or null. May not be {@link
|
|
|
|
|
* Double#isNaN() NaNs} or {@link Double#isInfinite() infinities}.
|
|
|
|
|
* @return this object.
|
|
|
|
|
* @throws JSONException if an error occurs
|
|
|
|
|
*/
|
|
|
|
|
public JSONObject accumulate(String name, Object value) throws JSONException {
|
|
|
|
|
Object current = nameValuePairs.get(checkName(name));
|
|
|
|
|
Object current = this.nameValuePairs.get(checkName(name));
|
|
|
|
|
if (current == null) {
|
|
|
|
|
return put(name, value);
|
|
|
|
|
}
|
|
|
|
@ -301,11 +328,12 @@ public class JSONObject {
|
|
|
|
|
if (current instanceof JSONArray) {
|
|
|
|
|
JSONArray array = (JSONArray) current;
|
|
|
|
|
array.put(value);
|
|
|
|
|
} else {
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
JSONArray array = new JSONArray();
|
|
|
|
|
array.put(current);
|
|
|
|
|
array.put(value);
|
|
|
|
|
nameValuePairs.put(name, array);
|
|
|
|
|
this.nameValuePairs.put(name, array);
|
|
|
|
|
}
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
@ -320,37 +348,43 @@ public class JSONObject {
|
|
|
|
|
/**
|
|
|
|
|
* Removes the named mapping if it exists; does nothing otherwise.
|
|
|
|
|
*
|
|
|
|
|
* @param name the name of the property
|
|
|
|
|
* @return the value previously mapped by {@code name}, or null if there was
|
|
|
|
|
* no such mapping.
|
|
|
|
|
*/
|
|
|
|
|
public Object remove(String name) {
|
|
|
|
|
return nameValuePairs.remove(name);
|
|
|
|
|
return this.nameValuePairs.remove(name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns true if this object has no mapping for {@code name} or if it has
|
|
|
|
|
* a mapping whose value is {@link #NULL}.
|
|
|
|
|
* @param name the name of the property
|
|
|
|
|
* @return true if this object has no mapping for {@code name}
|
|
|
|
|
*/
|
|
|
|
|
public boolean isNull(String name) {
|
|
|
|
|
Object value = nameValuePairs.get(name);
|
|
|
|
|
Object value = this.nameValuePairs.get(name);
|
|
|
|
|
return value == null || value == NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns true if this object has a mapping for {@code name}. The mapping
|
|
|
|
|
* may be {@link #NULL}.
|
|
|
|
|
* @param name the name of the property
|
|
|
|
|
* @return true if this object has a mapping for {@code name}
|
|
|
|
|
*/
|
|
|
|
|
public boolean has(String name) {
|
|
|
|
|
return nameValuePairs.containsKey(name);
|
|
|
|
|
return this.nameValuePairs.containsKey(name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the value mapped by {@code name}.
|
|
|
|
|
*
|
|
|
|
|
* @param name the name of the property
|
|
|
|
|
* @return the value
|
|
|
|
|
* @throws JSONException if no such mapping exists.
|
|
|
|
|
*/
|
|
|
|
|
public Object get(String name) throws JSONException {
|
|
|
|
|
Object result = nameValuePairs.get(name);
|
|
|
|
|
Object result = this.nameValuePairs.get(name);
|
|
|
|
|
if (result == null) {
|
|
|
|
|
throw new JSONException("No value for " + name);
|
|
|
|
|
}
|
|
|
|
@ -360,15 +394,19 @@ public class JSONObject {
|
|
|
|
|
/**
|
|
|
|
|
* Returns the value mapped by {@code name}, or null if no such mapping
|
|
|
|
|
* exists.
|
|
|
|
|
* @param name the name of the property
|
|
|
|
|
* @return the value or {@code null}
|
|
|
|
|
*/
|
|
|
|
|
public Object opt(String name) {
|
|
|
|
|
return nameValuePairs.get(name);
|
|
|
|
|
return this.nameValuePairs.get(name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the value mapped by {@code name} if it exists and is a boolean or
|
|
|
|
|
* can be coerced to a boolean.
|
|
|
|
|
*
|
|
|
|
|
* @param name the name of the property
|
|
|
|
|
* @return the value
|
|
|
|
|
* @throws JSONException if the mapping doesn't exist or cannot be coerced
|
|
|
|
|
* to a boolean.
|
|
|
|
|
*/
|
|
|
|
@ -384,6 +422,8 @@ public class JSONObject {
|
|
|
|
|
/**
|
|
|
|
|
* Returns the value mapped by {@code name} if it exists and is a boolean or
|
|
|
|
|
* can be coerced to a boolean. Returns false otherwise.
|
|
|
|
|
* @param name the name of the property
|
|
|
|
|
* @return the value or {@code null}
|
|
|
|
|
*/
|
|
|
|
|
public boolean optBoolean(String name) {
|
|
|
|
|
return optBoolean(name, false);
|
|
|
|
@ -392,6 +432,9 @@ public class JSONObject {
|
|
|
|
|
/**
|
|
|
|
|
* Returns the value mapped by {@code name} if it exists and is a boolean or
|
|
|
|
|
* can be coerced to a boolean. Returns {@code fallback} otherwise.
|
|
|
|
|
* @param name the name of the property
|
|
|
|
|
* @param fallback a fallback value
|
|
|
|
|
* @return the value or {@code fallback}
|
|
|
|
|
*/
|
|
|
|
|
public boolean optBoolean(String name, boolean fallback) {
|
|
|
|
|
Object object = opt(name);
|
|
|
|
@ -403,6 +446,8 @@ public class JSONObject {
|
|
|
|
|
* Returns the value mapped by {@code name} if it exists and is a double or
|
|
|
|
|
* can be coerced to a double.
|
|
|
|
|
*
|
|
|
|
|
* @param name the name of the property
|
|
|
|
|
* @return the value
|
|
|
|
|
* @throws JSONException if the mapping doesn't exist or cannot be coerced
|
|
|
|
|
* to a double.
|
|
|
|
|
*/
|
|
|
|
@ -418,6 +463,8 @@ public class JSONObject {
|
|
|
|
|
/**
|
|
|
|
|
* Returns the value mapped by {@code name} if it exists and is a double or
|
|
|
|
|
* can be coerced to a double. Returns {@code NaN} otherwise.
|
|
|
|
|
* @param name the name of the property
|
|
|
|
|
* @return the value or {@code NaN}
|
|
|
|
|
*/
|
|
|
|
|
public double optDouble(String name) {
|
|
|
|
|
return optDouble(name, Double.NaN);
|
|
|
|
@ -426,6 +473,9 @@ public class JSONObject {
|
|
|
|
|
/**
|
|
|
|
|
* Returns the value mapped by {@code name} if it exists and is a double or
|
|
|
|
|
* can be coerced to a double. Returns {@code fallback} otherwise.
|
|
|
|
|
* @param name the name of the property
|
|
|
|
|
* @param fallback a fallback value
|
|
|
|
|
* @return the value or {@code fallback}
|
|
|
|
|
*/
|
|
|
|
|
public double optDouble(String name, double fallback) {
|
|
|
|
|
Object object = opt(name);
|
|
|
|
@ -436,7 +486,8 @@ public class JSONObject {
|
|
|
|
|
/**
|
|
|
|
|
* Returns the value mapped by {@code name} if it exists and is an int or
|
|
|
|
|
* can be coerced to an int.
|
|
|
|
|
*
|
|
|
|
|
* @param name the name of the property
|
|
|
|
|
* @return the value
|
|
|
|
|
* @throws JSONException if the mapping doesn't exist or cannot be coerced
|
|
|
|
|
* to an int.
|
|
|
|
|
*/
|
|
|
|
@ -452,6 +503,8 @@ public class JSONObject {
|
|
|
|
|
/**
|
|
|
|
|
* Returns the value mapped by {@code name} if it exists and is an int or
|
|
|
|
|
* can be coerced to an int. Returns 0 otherwise.
|
|
|
|
|
* @param name the name of the property
|
|
|
|
|
* @return the value of {@code 0}
|
|
|
|
|
*/
|
|
|
|
|
public int optInt(String name) {
|
|
|
|
|
return optInt(name, 0);
|
|
|
|
@ -460,6 +513,9 @@ public class JSONObject {
|
|
|
|
|
/**
|
|
|
|
|
* Returns the value mapped by {@code name} if it exists and is an int or
|
|
|
|
|
* can be coerced to an int. Returns {@code fallback} otherwise.
|
|
|
|
|
* @param name the name of the property
|
|
|
|
|
* @param fallback a fallback value
|
|
|
|
|
* @return the value or {@code fallback}
|
|
|
|
|
*/
|
|
|
|
|
public int optInt(String name, int fallback) {
|
|
|
|
|
Object object = opt(name);
|
|
|
|
@ -472,6 +528,8 @@ public class JSONObject {
|
|
|
|
|
* can be coerced to a long. Note that JSON represents numbers as doubles,
|
|
|
|
|
* so this is <a href="#lossy">lossy</a>; use strings to transfer numbers via JSON.
|
|
|
|
|
*
|
|
|
|
|
* @param name the name of the property
|
|
|
|
|
* @return the value
|
|
|
|
|
* @throws JSONException if the mapping doesn't exist or cannot be coerced
|
|
|
|
|
* to a long.
|
|
|
|
|
*/
|
|
|
|
@ -488,6 +546,8 @@ public class JSONObject {
|
|
|
|
|
* Returns the value mapped by {@code name} if it exists and is a long or
|
|
|
|
|
* can be coerced to a long. Returns 0 otherwise. Note that JSON represents numbers as doubles,
|
|
|
|
|
* so this is <a href="#lossy">lossy</a>; use strings to transfer numbers via JSON.
|
|
|
|
|
* @param name the name of the property
|
|
|
|
|
* @return the value or {@code 0L}
|
|
|
|
|
*/
|
|
|
|
|
public long optLong(String name) {
|
|
|
|
|
return optLong(name, 0L);
|
|
|
|
@ -498,6 +558,9 @@ public class JSONObject {
|
|
|
|
|
* can be coerced to a long. Returns {@code fallback} otherwise. Note that JSON represents
|
|
|
|
|
* numbers as doubles, so this is <a href="#lossy">lossy</a>; use strings to transfer
|
|
|
|
|
* numbers via JSON.
|
|
|
|
|
* @param name the name of the property
|
|
|
|
|
* @param fallback a fallback value
|
|
|
|
|
* @return the value or {@code fallback}
|
|
|
|
|
*/
|
|
|
|
|
public long optLong(String name, long fallback) {
|
|
|
|
|
Object object = opt(name);
|
|
|
|
@ -508,7 +571,8 @@ public class JSONObject {
|
|
|
|
|
/**
|
|
|
|
|
* Returns the value mapped by {@code name} if it exists, coercing it if
|
|
|
|
|
* necessary.
|
|
|
|
|
*
|
|
|
|
|
* @param name the name of the property
|
|
|
|
|
* @return the value
|
|
|
|
|
* @throws JSONException if no such mapping exists.
|
|
|
|
|
*/
|
|
|
|
|
public String getString(String name) throws JSONException {
|
|
|
|
@ -523,6 +587,8 @@ public class JSONObject {
|
|
|
|
|
/**
|
|
|
|
|
* Returns the value mapped by {@code name} if it exists, coercing it if
|
|
|
|
|
* necessary. Returns the empty string if no such mapping exists.
|
|
|
|
|
* @param name the name of the property
|
|
|
|
|
* @return the value or an empty string
|
|
|
|
|
*/
|
|
|
|
|
public String optString(String name) {
|
|
|
|
|
return optString(name, "");
|
|
|
|
@ -531,6 +597,9 @@ public class JSONObject {
|
|
|
|
|
/**
|
|
|
|
|
* Returns the value mapped by {@code name} if it exists, coercing it if
|
|
|
|
|
* necessary. Returns {@code fallback} if no such mapping exists.
|
|
|
|
|
* @param name the name of the property
|
|
|
|
|
* @param fallback a fallback value
|
|
|
|
|
* @return the value or {@code fallback}
|
|
|
|
|
*/
|
|
|
|
|
public String optString(String name, String fallback) {
|
|
|
|
|
Object object = opt(name);
|
|
|
|
@ -541,7 +610,8 @@ public class JSONObject {
|
|
|
|
|
/**
|
|
|
|
|
* Returns the value mapped by {@code name} if it exists and is a {@code
|
|
|
|
|
* JSONArray}.
|
|
|
|
|
*
|
|
|
|
|
* @param name the name of the property
|
|
|
|
|
* @return the value
|
|
|
|
|
* @throws JSONException if the mapping doesn't exist or is not a {@code
|
|
|
|
|
* JSONArray}.
|
|
|
|
|
*/
|
|
|
|
@ -549,7 +619,8 @@ public class JSONObject {
|
|
|
|
|
Object object = get(name);
|
|
|
|
|
if (object instanceof JSONArray) {
|
|
|
|
|
return (JSONArray) object;
|
|
|
|
|
} else {
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
throw JSON.typeMismatch(name, object, "JSONArray");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -557,6 +628,8 @@ public class JSONObject {
|
|
|
|
|
/**
|
|
|
|
|
* Returns the value mapped by {@code name} if it exists and is a {@code
|
|
|
|
|
* JSONArray}. Returns null otherwise.
|
|
|
|
|
* @param name the name of the property
|
|
|
|
|
* @return the value or {@code null}
|
|
|
|
|
*/
|
|
|
|
|
public JSONArray optJSONArray(String name) {
|
|
|
|
|
Object object = opt(name);
|
|
|
|
@ -566,7 +639,8 @@ public class JSONObject {
|
|
|
|
|
/**
|
|
|
|
|
* Returns the value mapped by {@code name} if it exists and is a {@code
|
|
|
|
|
* JSONObject}.
|
|
|
|
|
*
|
|
|
|
|
* @param name the name of the property
|
|
|
|
|
* @return the value
|
|
|
|
|
* @throws JSONException if the mapping doesn't exist or is not a {@code
|
|
|
|
|
* JSONObject}.
|
|
|
|
|
*/
|
|
|
|
@ -574,7 +648,8 @@ public class JSONObject {
|
|
|
|
|
Object object = get(name);
|
|
|
|
|
if (object instanceof JSONObject) {
|
|
|
|
|
return (JSONObject) object;
|
|
|
|
|
} else {
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
throw JSON.typeMismatch(name, object, "JSONObject");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -582,6 +657,8 @@ public class JSONObject {
|
|
|
|
|
/**
|
|
|
|
|
* Returns the value mapped by {@code name} if it exists and is a {@code
|
|
|
|
|
* JSONObject}. Returns null otherwise.
|
|
|
|
|
* @param name the name of the property
|
|
|
|
|
* @return the value or {@code null}
|
|
|
|
|
*/
|
|
|
|
|
public JSONObject optJSONObject(String name) {
|
|
|
|
|
Object object = opt(name);
|
|
|
|
@ -592,8 +669,10 @@ public class JSONObject {
|
|
|
|
|
* Returns an array with the values corresponding to {@code names}. The
|
|
|
|
|
* array contains null for names that aren't mapped. This method returns
|
|
|
|
|
* null if {@code names} is either null or empty.
|
|
|
|
|
* @param names the names of the properties
|
|
|
|
|
* @return the array
|
|
|
|
|
*/
|
|
|
|
|
public JSONArray toJSONArray(JSONArray names) throws JSONException {
|
|
|
|
|
public JSONArray toJSONArray(JSONArray names) {
|
|
|
|
|
JSONArray result = new JSONArray();
|
|
|
|
|
if (names == null) {
|
|
|
|
|
return null;
|
|
|
|
@ -615,32 +694,37 @@ public class JSONObject {
|
|
|
|
|
* remove the corresponding mapping from this object. If this object is
|
|
|
|
|
* modified after the iterator is returned, the iterator's behavior is
|
|
|
|
|
* undefined. The order of the keys is undefined.
|
|
|
|
|
* @return the keys
|
|
|
|
|
*/
|
|
|
|
|
/* Return a raw type for API compatibility */
|
|
|
|
|
public Iterator keys() {
|
|
|
|
|
return nameValuePairs.keySet().iterator();
|
|
|
|
|
return this.nameValuePairs.keySet().iterator();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns an array containing the string names in this object. This method
|
|
|
|
|
* returns null if this object contains no mappings.
|
|
|
|
|
* @return the array
|
|
|
|
|
*/
|
|
|
|
|
public JSONArray names() {
|
|
|
|
|
return nameValuePairs.isEmpty()
|
|
|
|
|
return this.nameValuePairs.isEmpty()
|
|
|
|
|
? null
|
|
|
|
|
: new JSONArray(new ArrayList<String>(nameValuePairs.keySet()));
|
|
|
|
|
: new JSONArray(new ArrayList<String>(this.nameValuePairs.keySet()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Encodes this object as a compact JSON string, such as:
|
|
|
|
|
* <pre>{"query":"Pizza","locations":[94043,90210]}</pre>
|
|
|
|
|
* @return a string representation of the object.
|
|
|
|
|
*/
|
|
|
|
|
@Override public String toString() {
|
|
|
|
|
@Override
|
|
|
|
|
public String toString() {
|
|
|
|
|
try {
|
|
|
|
|
JSONStringer stringer = new JSONStringer();
|
|
|
|
|
writeTo(stringer);
|
|
|
|
|
return stringer.toString();
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
}
|
|
|
|
|
catch (JSONException e) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -659,6 +743,8 @@ public class JSONObject {
|
|
|
|
|
*
|
|
|
|
|
* @param indentSpaces the number of spaces to indent for each level of
|
|
|
|
|
* nesting.
|
|
|
|
|
* @return a string representation of the object.
|
|
|
|
|
* @throws JSONException if an error occurs
|
|
|
|
|
*/
|
|
|
|
|
public String toString(int indentSpaces) throws JSONException {
|
|
|
|
|
JSONStringer stringer = new JSONStringer(indentSpaces);
|
|
|
|
@ -668,7 +754,7 @@ public class JSONObject {
|
|
|
|
|
|
|
|
|
|
void writeTo(JSONStringer stringer) throws JSONException {
|
|
|
|
|
stringer.object();
|
|
|
|
|
for (Map.Entry<String, Object> entry : nameValuePairs.entrySet()) {
|
|
|
|
|
for (Map.Entry<String, Object> entry : this.nameValuePairs.entrySet()) {
|
|
|
|
|
stringer.key(entry.getKey()).value(entry.getValue());
|
|
|
|
|
}
|
|
|
|
|
stringer.endObject();
|
|
|
|
@ -679,6 +765,8 @@ public class JSONObject {
|
|
|
|
|
*
|
|
|
|
|
* @param number a finite value. May not be {@link Double#isNaN() NaNs} or
|
|
|
|
|
* {@link Double#isInfinite() infinities}.
|
|
|
|
|
* @return the encoded value
|
|
|
|
|
* @throws JSONException if an error occurs
|
|
|
|
|
*/
|
|
|
|
|
public static String numberToString(Number number) throws JSONException {
|
|
|
|
|
if (number == null) {
|
|
|
|
@ -707,6 +795,7 @@ public class JSONObject {
|
|
|
|
|
*
|
|
|
|
|
* @param data the string to encode. Null will be interpreted as an empty
|
|
|
|
|
* string.
|
|
|
|
|
* @return the quoted value
|
|
|
|
|
*/
|
|
|
|
|
public static String quote(String data) {
|
|
|
|
|
if (data == null) {
|
|
|
|
@ -718,7 +807,8 @@ public class JSONObject {
|
|
|
|
|
stringer.value(data);
|
|
|
|
|
stringer.close(JSONStringer.Scope.NULL, JSONStringer.Scope.NULL, "");
|
|
|
|
|
return stringer.toString();
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
}
|
|
|
|
|
catch (JSONException e) {
|
|
|
|
|
throw new AssertionError();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -734,6 +824,8 @@ public class JSONObject {
|
|
|
|
|
* If the object is a primitive wrapper type or {@code String}, returns the object.
|
|
|
|
|
* Otherwise if the object is from a {@code java} package, returns the result of {@code toString}.
|
|
|
|
|
* If wrapping fails, returns null.
|
|
|
|
|
* @param o the object to wrap
|
|
|
|
|
* @return the wrapped object
|
|
|
|
|
*/
|
|
|
|
|
public static Object wrap(Object o) {
|
|
|
|
|
if (o == null) {
|
|
|
|
@ -748,7 +840,8 @@ public class JSONObject {
|
|
|
|
|
try {
|
|
|
|
|
if (o instanceof Collection) {
|
|
|
|
|
return new JSONArray((Collection) o);
|
|
|
|
|
} else if (o.getClass().isArray()) {
|
|
|
|
|
}
|
|
|
|
|
else if (o.getClass().isArray()) {
|
|
|
|
|
return new JSONArray(o);
|
|
|
|
|
}
|
|
|
|
|
if (o instanceof Map) {
|
|
|
|
@ -768,7 +861,8 @@ public class JSONObject {
|
|
|
|
|
if (o.getClass().getPackage().getName().startsWith("java.")) {
|
|
|
|
|
return o.toString();
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception ignored) {
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ignored) {
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|