|
|
|
@ -1,5 +1,5 @@
|
|
|
|
|
/*
|
|
|
|
|
* Copyright 2012-2018 the original author or authors.
|
|
|
|
|
* Copyright (C) 2010 The Android Open Source Project
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
@ -25,56 +25,52 @@ import java.util.Map;
|
|
|
|
|
// Note: this class was written without inspecting the non-free org.json sourcecode.
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A modifiable set of name/value mappings. Names are unique, non-null strings.
|
|
|
|
|
* Values may be any mix of {@link JSONObject JSONObjects}, {@link JSONArray
|
|
|
|
|
* JSONArrays}, Strings, Booleans, Integers, Longs, Doubles or {@link #NULL}.
|
|
|
|
|
* Values may not be {@code null}, {@link Double#isNaN() NaNs}, {@link
|
|
|
|
|
* Double#isInfinite() infinities}, or of any type not listed here.
|
|
|
|
|
*
|
|
|
|
|
* <p>This class can coerce values to another type when requested.
|
|
|
|
|
* A modifiable set of name/value mappings. Names are unique, non-null strings. Values may
|
|
|
|
|
* be any mix of {@link JSONObject JSONObjects}, {@link JSONArray JSONArrays}, Strings,
|
|
|
|
|
* Booleans, Integers, Longs, Doubles or {@link #NULL}. Values may not be {@code null},
|
|
|
|
|
* {@link Double#isNaN() NaNs}, {@link Double#isInfinite() infinities}, or of any type not
|
|
|
|
|
* listed here.
|
|
|
|
|
* <p>
|
|
|
|
|
* This class can coerce values to another type when requested.
|
|
|
|
|
* <ul>
|
|
|
|
|
* <li>When the requested type is a boolean, strings will be coerced using a
|
|
|
|
|
* case-insensitive comparison to "true" and "false".
|
|
|
|
|
* <li>When the requested type is a double, other {@link Number} types will
|
|
|
|
|
* be coerced using {@link Number#doubleValue() doubleValue}. Strings
|
|
|
|
|
* that can be coerced using {@link Double#valueOf(String)} will be.
|
|
|
|
|
* <li>When the requested type is an int, other {@link Number} types will
|
|
|
|
|
* be coerced using {@link Number#intValue() intValue}. Strings
|
|
|
|
|
* that can be coerced using {@link Double#valueOf(String)} will be,
|
|
|
|
|
* and then cast to int.
|
|
|
|
|
* <li>When the requested type is a double, other {@link Number} types will be coerced
|
|
|
|
|
* using {@link Number#doubleValue() doubleValue}. Strings that can be coerced using
|
|
|
|
|
* {@link Double#valueOf(String)} will be.
|
|
|
|
|
* <li>When the requested type is an int, other {@link Number} types will be coerced using
|
|
|
|
|
* {@link Number#intValue() intValue}. Strings that can be coerced using
|
|
|
|
|
* {@link Double#valueOf(String)} will be, and then cast to int.
|
|
|
|
|
* <li><a name="lossy">When the requested type is a long, other {@link Number} types will
|
|
|
|
|
* be coerced using {@link Number#longValue() longValue}. Strings
|
|
|
|
|
* that can be coerced using {@link Double#valueOf(String)} will be,
|
|
|
|
|
* and then cast to long. This two-step conversion is lossy for very
|
|
|
|
|
* large values. For example, the string "9223372036854775806" yields the
|
|
|
|
|
* long 9223372036854775807.</a>
|
|
|
|
|
* <li>When the requested type is a String, other non-null values will be
|
|
|
|
|
* coerced using {@link String#valueOf(Object)}. Although null cannot be
|
|
|
|
|
* coerced, the sentinel value {@link JSONObject#NULL} is coerced to the
|
|
|
|
|
* string "null".
|
|
|
|
|
* be coerced using {@link Number#longValue() longValue}. Strings that can be coerced
|
|
|
|
|
* using {@link Double#valueOf(String)} will be, and then cast to long. This two-step
|
|
|
|
|
* conversion is lossy for very large values. For example, the string
|
|
|
|
|
* "9223372036854775806" yields the long 9223372036854775807.</a>
|
|
|
|
|
* <li>When the requested type is a String, other non-null values will be coerced using
|
|
|
|
|
* {@link String#valueOf(Object)}. Although null cannot be coerced, the sentinel value
|
|
|
|
|
* {@link JSONObject#NULL} is coerced to the string "null".
|
|
|
|
|
* </ul>
|
|
|
|
|
*
|
|
|
|
|
* <p>This class can look up both mandatory and optional values:
|
|
|
|
|
* <p>
|
|
|
|
|
* This class can look up both mandatory and optional values:
|
|
|
|
|
* <ul>
|
|
|
|
|
* <li>Use <code>get<i>Type</i>()</code> to retrieve a mandatory value. This
|
|
|
|
|
* fails with a {@code JSONException} if the requested name has no value
|
|
|
|
|
* or if the value cannot be coerced to the requested type.
|
|
|
|
|
* <li>Use <code>opt<i>Type</i>()</code> to retrieve an optional value. This
|
|
|
|
|
* returns a system- or user-supplied default if the requested name has no
|
|
|
|
|
* value or if the value cannot be coerced to the requested type.
|
|
|
|
|
* <li>Use <code>get<i>Type</i>()</code> to retrieve a mandatory value. This fails with a
|
|
|
|
|
* {@code JSONException} if the requested name has no value or if the value cannot be
|
|
|
|
|
* coerced to the requested type.
|
|
|
|
|
* <li>Use <code>opt<i>Type</i>()</code> to retrieve an optional value. This returns a
|
|
|
|
|
* system- or user-supplied default if the requested name has no value or if the value
|
|
|
|
|
* cannot be coerced to the requested type.
|
|
|
|
|
* </ul>
|
|
|
|
|
*
|
|
|
|
|
* <p><strong>Warning:</strong> this class represents null in two incompatible
|
|
|
|
|
* ways: the standard Java {@code null} reference, and the sentinel value {@link
|
|
|
|
|
* JSONObject#NULL}. In particular, calling {@code put(name, null)} removes the
|
|
|
|
|
* named entry from the object but {@code put(name, JSONObject.NULL)} stores an
|
|
|
|
|
* entry whose value is {@code JSONObject.NULL}.
|
|
|
|
|
*
|
|
|
|
|
* <p>Instances of this class are not thread safe. Although this class is
|
|
|
|
|
* nonfinal, it was not designed for inheritance and should not be subclassed.
|
|
|
|
|
* In particular, self-use by overrideable methods is not specified. See
|
|
|
|
|
* <i>Effective Java</i> Item 17, "Design and Document or inheritance or else
|
|
|
|
|
* prohibit it" for further information.
|
|
|
|
|
* <p>
|
|
|
|
|
* <strong>Warning:</strong> this class represents null in two incompatible ways: the
|
|
|
|
|
* standard Java {@code null} reference, and the sentinel value {@link JSONObject#NULL}.
|
|
|
|
|
* In particular, calling {@code put(name, null)} removes the named entry from the object
|
|
|
|
|
* but {@code put(name, JSONObject.NULL)} stores an entry whose value is
|
|
|
|
|
* {@code JSONObject.NULL}.
|
|
|
|
|
* <p>
|
|
|
|
|
* Instances of this class are not thread safe. Although this class is nonfinal, it was
|
|
|
|
|
* not designed for inheritance and should not be subclassed. In particular, self-use by
|
|
|
|
|
* overrideable methods is not specified. See <i>Effective Java</i> Item 17, "Design and
|
|
|
|
|
* Document or inheritance or else prohibit it" for further information.
|
|
|
|
|
*/
|
|
|
|
|
public class JSONObject {
|
|
|
|
|
|
|
|
|
@ -90,21 +86,23 @@ public class JSONObject {
|
|
|
|
|
* <li>do not throw on {@link #get(String)}
|
|
|
|
|
* <li>are included in the encoded JSON string.
|
|
|
|
|
* </ul>
|
|
|
|
|
*
|
|
|
|
|
* <p>This value violates the general contract of {@link Object#equals} by
|
|
|
|
|
* returning true when compared to {@code null}. Its {@link #toString}
|
|
|
|
|
* method returns "null".
|
|
|
|
|
* <p>
|
|
|
|
|
* This value violates the general contract of {@link Object#equals} by returning true
|
|
|
|
|
* when compared to {@code null}. Its {@link #toString} method returns "null".
|
|
|
|
|
*/
|
|
|
|
|
public static final Object NULL = new Object() {
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean equals(Object o) {
|
|
|
|
|
return o == this || o == null; // API specifies this broken equals implementation
|
|
|
|
|
return o == this || o == null; // API specifies this broken equals
|
|
|
|
|
// implementation
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public String toString() {
|
|
|
|
|
return "null";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private final Map<String, Object> nameValuePairs;
|
|
|
|
@ -117,21 +115,22 @@ public class JSONObject {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates a new {@code JSONObject} by copying all name/value mappings from
|
|
|
|
|
* the given map.
|
|
|
|
|
* Creates a new {@code JSONObject} by copying all name/value mappings from the given
|
|
|
|
|
* map.
|
|
|
|
|
*
|
|
|
|
|
* @param copyFrom a map whose keys are of type {@link String} and whose
|
|
|
|
|
* values are of supported types.
|
|
|
|
|
* @param copyFrom a map whose keys are of type {@link String} and whose values are of
|
|
|
|
|
* supported types.
|
|
|
|
|
* @throws NullPointerException if any of the map's keys are null.
|
|
|
|
|
*/
|
|
|
|
|
/* (accept a raw type for API compatibility) */
|
|
|
|
|
@SuppressWarnings("rawtypes")
|
|
|
|
|
public JSONObject(Map copyFrom) {
|
|
|
|
|
this();
|
|
|
|
|
Map<?, ?> contentsTyped = (Map<?, ?>) copyFrom;
|
|
|
|
|
Map<?, ?> contentsTyped = copyFrom;
|
|
|
|
|
for (Map.Entry<?, ?> entry : contentsTyped.entrySet()) {
|
|
|
|
|
/*
|
|
|
|
|
* Deviate from the original by checking that keys are non-null and
|
|
|
|
|
* of the proper type. (We still defer validating the values).
|
|
|
|
|
* Deviate from the original by checking that keys are non-null and of the
|
|
|
|
|
* proper type. (We still defer validating the values).
|
|
|
|
|
*/
|
|
|
|
|
String key = (String) entry.getKey();
|
|
|
|
|
if (key == null) {
|
|
|
|
@ -142,18 +141,15 @@ public class JSONObject {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates a new {@code JSONObject} with name/value mappings from the next
|
|
|
|
|
* object in the tokener.
|
|
|
|
|
*
|
|
|
|
|
* @param readFrom a tokener whose nextValue() method will yield a
|
|
|
|
|
* {@code JSONObject}.
|
|
|
|
|
* @throws JSONException if the parse fails or doesn't yield a
|
|
|
|
|
* {@code JSONObject}.
|
|
|
|
|
* Creates a new {@code JSONObject} with name/value mappings from the next object in
|
|
|
|
|
* the tokener.
|
|
|
|
|
* @param readFrom a tokener whose nextValue() method will yield a {@code JSONObject}.
|
|
|
|
|
* @throws JSONException if the parse fails or doesn't yield a {@code JSONObject}.
|
|
|
|
|
*/
|
|
|
|
|
public JSONObject(JSONTokener readFrom) throws JSONException {
|
|
|
|
|
/*
|
|
|
|
|
* Getting the parser to populate this could get tricky. Instead, just
|
|
|
|
|
* parse to temporary JSONObject and then steal the data from that.
|
|
|
|
|
* Getting the parser to populate this could get tricky. Instead, just parse to
|
|
|
|
|
* temporary JSONObject and then steal the data from that.
|
|
|
|
|
*/
|
|
|
|
|
Object object = readFrom.nextValue();
|
|
|
|
|
if (object instanceof JSONObject) {
|
|
|
|
@ -165,9 +161,7 @@ public class JSONObject {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates a new {@code JSONObject} with name/value mappings from the JSON
|
|
|
|
|
* string.
|
|
|
|
|
*
|
|
|
|
|
* Creates a new {@code JSONObject} with name/value mappings from the JSON string.
|
|
|
|
|
* @param json a JSON-encoded string containing an object.
|
|
|
|
|
* @throws JSONException if the parse fails or doesn't yield a {@code
|
|
|
|
|
* JSONObject}.
|
|
|
|
@ -177,9 +171,8 @@ 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.
|
|
|
|
|
* 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
|
|
|
|
@ -203,8 +196,8 @@ public class JSONObject {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Maps {@code name} to {@code value}, clobbering any existing name/value
|
|
|
|
|
* mapping with the same name.
|
|
|
|
|
* 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.
|
|
|
|
@ -216,9 +209,8 @@ public class JSONObject {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Maps {@code name} to {@code value}, clobbering any existing name/value
|
|
|
|
|
* mapping with the same name.
|
|
|
|
|
*
|
|
|
|
|
* 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}.
|
|
|
|
@ -231,9 +223,8 @@ public class JSONObject {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Maps {@code name} to {@code value}, clobbering any existing name/value
|
|
|
|
|
* mapping with the same name.
|
|
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
@ -245,9 +236,8 @@ public class JSONObject {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Maps {@code name} to {@code value}, clobbering any existing name/value
|
|
|
|
|
* mapping with the same name.
|
|
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
@ -259,15 +249,13 @@ public class JSONObject {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Maps {@code name} to {@code value}, clobbering any existing name/value
|
|
|
|
|
* mapping with the same name. If the value is {@code null}, any existing
|
|
|
|
|
* mapping for {@code name} is removed.
|
|
|
|
|
*
|
|
|
|
|
* Maps {@code name} to {@code value}, clobbering any existing name/value 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}.
|
|
|
|
|
* @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
|
|
|
|
|
*/
|
|
|
|
@ -277,7 +265,8 @@ public class JSONObject {
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
if (value instanceof Number) {
|
|
|
|
|
// deviate from the original by checking all Numbers, not just floats & doubles
|
|
|
|
|
// deviate from the original by checking all Numbers, not just floats &
|
|
|
|
|
// doubles
|
|
|
|
|
JSON.checkDouble(((Number) value).doubleValue());
|
|
|
|
|
}
|
|
|
|
|
this.nameValuePairs.put(checkName(name), value);
|
|
|
|
@ -285,8 +274,8 @@ public class JSONObject {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Equivalent to {@code put(name, value)} when both parameters are non-null;
|
|
|
|
|
* does nothing otherwise.
|
|
|
|
|
* 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.
|
|
|
|
@ -300,17 +289,15 @@ public class JSONObject {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Appends {@code value} to the array already mapped to {@code name}. If
|
|
|
|
|
* this object has no mapping for {@code name}, this inserts a new mapping.
|
|
|
|
|
* If the mapping exists but its value is not an array, the existing
|
|
|
|
|
* and new values are inserted in order into a new array which is itself
|
|
|
|
|
* mapped to {@code name}. In aggregate, this allows values to be added to a
|
|
|
|
|
* mapping one at a time.
|
|
|
|
|
*
|
|
|
|
|
* Appends {@code value} to the array already mapped to {@code name}. If this object
|
|
|
|
|
* has no mapping for {@code name}, this inserts a new mapping. If the mapping exists
|
|
|
|
|
* but its value is not an array, the existing and new values are inserted in order
|
|
|
|
|
* into a new array which is itself 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}.
|
|
|
|
|
* @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
|
|
|
|
|
*/
|
|
|
|
@ -349,16 +336,16 @@ 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.
|
|
|
|
|
* @return the value previously mapped by {@code name}, or null if there was no such
|
|
|
|
|
* mapping.
|
|
|
|
|
*/
|
|
|
|
|
public Object remove(String 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}.
|
|
|
|
|
* 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}
|
|
|
|
|
*/
|
|
|
|
@ -368,8 +355,8 @@ public class JSONObject {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns true if this object has a mapping for {@code name}. The mapping
|
|
|
|
|
* may be {@link #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}
|
|
|
|
|
*/
|
|
|
|
@ -392,8 +379,7 @@ public class JSONObject {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the value mapped by {@code name}, or null if no such mapping
|
|
|
|
|
* exists.
|
|
|
|
|
* 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}
|
|
|
|
|
*/
|
|
|
|
@ -402,13 +388,12 @@ 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 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.
|
|
|
|
|
* @throws JSONException if the mapping doesn't exist or cannot be coerced to a
|
|
|
|
|
* boolean.
|
|
|
|
|
*/
|
|
|
|
|
public boolean getBoolean(String name) throws JSONException {
|
|
|
|
|
Object object = get(name);
|
|
|
|
@ -420,8 +405,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.
|
|
|
|
|
* 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}
|
|
|
|
|
*/
|
|
|
|
@ -430,8 +415,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 {@code fallback} otherwise.
|
|
|
|
|
* 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}
|
|
|
|
@ -443,13 +428,13 @@ 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 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.
|
|
|
|
|
* @throws JSONException if the mapping doesn't exist or cannot be coerced to a
|
|
|
|
|
* double.
|
|
|
|
|
*/
|
|
|
|
|
public double getDouble(String name) throws JSONException {
|
|
|
|
|
Object object = get(name);
|
|
|
|
@ -461,8 +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. Returns {@code NaN} otherwise.
|
|
|
|
|
* 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}
|
|
|
|
|
*/
|
|
|
|
@ -471,8 +456,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 fallback} otherwise.
|
|
|
|
|
* 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}
|
|
|
|
@ -484,12 +469,11 @@ 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 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.
|
|
|
|
|
* @throws JSONException if the mapping doesn't exist or cannot be coerced to an int.
|
|
|
|
|
*/
|
|
|
|
|
public int getInt(String name) throws JSONException {
|
|
|
|
|
Object object = get(name);
|
|
|
|
@ -501,8 +485,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.
|
|
|
|
|
* 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}
|
|
|
|
|
*/
|
|
|
|
@ -511,8 +495,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 {@code fallback} otherwise.
|
|
|
|
|
* 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}
|
|
|
|
@ -524,14 +508,12 @@ public class JSONObject {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the value mapped by {@code name} if it exists and is a long or
|
|
|
|
|
* 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.
|
|
|
|
|
*
|
|
|
|
|
* Returns the value mapped by {@code name} if it exists and is a long or 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.
|
|
|
|
|
* @throws JSONException if the mapping doesn't exist or cannot be coerced to a long.
|
|
|
|
|
*/
|
|
|
|
|
public long getLong(String name) throws JSONException {
|
|
|
|
|
Object object = get(name);
|
|
|
|
@ -543,9 +525,10 @@ 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.
|
|
|
|
|
* 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}
|
|
|
|
|
*/
|
|
|
|
@ -554,8 +537,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 {@code fallback} otherwise. Note that JSON represents
|
|
|
|
|
* Returns the value mapped by {@code name} if it exists and is a long or 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
|
|
|
|
@ -569,8 +552,7 @@ public class JSONObject {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the value mapped by {@code name} if it exists, coercing it if
|
|
|
|
|
* necessary.
|
|
|
|
|
* 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.
|
|
|
|
@ -585,8 +567,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.
|
|
|
|
|
* 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
|
|
|
|
|
*/
|
|
|
|
@ -595,8 +577,8 @@ 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.
|
|
|
|
|
* 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}
|
|
|
|
@ -666,9 +648,9 @@ 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.
|
|
|
|
|
* 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
|
|
|
|
|
*/
|
|
|
|
@ -689,26 +671,26 @@ public class JSONObject {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns an iterator of the {@code String} names in this object. The
|
|
|
|
|
* returned iterator supports {@link Iterator#remove() remove}, which will
|
|
|
|
|
* 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.
|
|
|
|
|
* Returns an iterator of the {@code String} names in this object. The returned
|
|
|
|
|
* iterator supports {@link Iterator#remove() remove}, which will 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 */
|
|
|
|
|
@SuppressWarnings("rawtypes")
|
|
|
|
|
public Iterator keys() {
|
|
|
|
|
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.
|
|
|
|
|
* 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 this.nameValuePairs.isEmpty()
|
|
|
|
|
? null
|
|
|
|
|
return this.nameValuePairs.isEmpty() ? null
|
|
|
|
|
: new JSONArray(new ArrayList<String>(this.nameValuePairs.keySet()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -730,9 +712,7 @@ public class JSONObject {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Encodes this object as a human readable JSON string for debugging, such
|
|
|
|
|
* as:
|
|
|
|
|
* <pre>
|
|
|
|
|
* Encodes this object as a human readable JSON string for debugging, such as: <pre>
|
|
|
|
|
* {
|
|
|
|
|
* "query": "Pizza",
|
|
|
|
|
* "locations": [
|
|
|
|
@ -740,9 +720,7 @@ public class JSONObject {
|
|
|
|
|
* 90210
|
|
|
|
|
* ]
|
|
|
|
|
* }</pre>
|
|
|
|
|
*
|
|
|
|
|
* @param indentSpaces the number of spaces to indent for each level of
|
|
|
|
|
* nesting.
|
|
|
|
|
* @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
|
|
|
|
|
*/
|
|
|
|
@ -762,7 +740,6 @@ public class JSONObject {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Encodes the number as a JSON string.
|
|
|
|
|
*
|
|
|
|
|
* @param number a finite value. May not be {@link Double#isNaN() NaNs} or
|
|
|
|
|
* {@link Double#isInfinite() infinities}.
|
|
|
|
|
* @return the encoded value
|
|
|
|
@ -782,7 +759,7 @@ public class JSONObject {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
long longValue = number.longValue();
|
|
|
|
|
if (doubleValue == (double) longValue) {
|
|
|
|
|
if (doubleValue == longValue) {
|
|
|
|
|
return Long.toString(longValue);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -790,11 +767,9 @@ public class JSONObject {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Encodes {@code data} as a JSON string. This applies quotes and any
|
|
|
|
|
* necessary character escaping.
|
|
|
|
|
*
|
|
|
|
|
* @param data the string to encode. Null will be interpreted as an empty
|
|
|
|
|
* string.
|
|
|
|
|
* Encodes {@code data} as a JSON string. This applies quotes and any necessary
|
|
|
|
|
* character escaping.
|
|
|
|
|
* @param data the string to encode. Null will be interpreted as an empty string.
|
|
|
|
|
* @return the quoted value
|
|
|
|
|
*/
|
|
|
|
|
public static String quote(String data) {
|
|
|
|
@ -815,18 +790,19 @@ public class JSONObject {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Wraps the given object if necessary.
|
|
|
|
|
*
|
|
|
|
|
* <p>If the object is null or , returns {@link #NULL}.
|
|
|
|
|
* If the object is a {@code JSONArray} or {@code JSONObject}, no wrapping is necessary.
|
|
|
|
|
* If the object is {@code NULL}, no wrapping is necessary.
|
|
|
|
|
* If the object is an array or {@code Collection}, returns an equivalent {@code JSONArray}.
|
|
|
|
|
* If the object is a {@code Map}, returns an equivalent {@code 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.
|
|
|
|
|
* <p>
|
|
|
|
|
* If the object is null or , returns {@link #NULL}. If the object is a
|
|
|
|
|
* {@code JSONArray} or {@code JSONObject}, no wrapping is necessary. If the object is
|
|
|
|
|
* {@code NULL}, no wrapping is necessary. If the object is an array or
|
|
|
|
|
* {@code Collection}, returns an equivalent {@code JSONArray}. If the object is a
|
|
|
|
|
* {@code Map}, returns an equivalent {@code 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
|
|
|
|
|
*/
|
|
|
|
|
@SuppressWarnings("rawtypes")
|
|
|
|
|
public static Object wrap(Object o) {
|
|
|
|
|
if (o == null) {
|
|
|
|
|
return NULL;
|
|
|
|
@ -847,15 +823,9 @@ public class JSONObject {
|
|
|
|
|
if (o instanceof Map) {
|
|
|
|
|
return new JSONObject((Map) o);
|
|
|
|
|
}
|
|
|
|
|
if (o instanceof Boolean ||
|
|
|
|
|
o instanceof Byte ||
|
|
|
|
|
o instanceof Character ||
|
|
|
|
|
o instanceof Double ||
|
|
|
|
|
o instanceof Float ||
|
|
|
|
|
o instanceof Integer ||
|
|
|
|
|
o instanceof Long ||
|
|
|
|
|
o instanceof Short ||
|
|
|
|
|
o instanceof String) {
|
|
|
|
|
if (o instanceof Boolean || o instanceof Byte || o instanceof Character
|
|
|
|
|
|| o instanceof Double || o instanceof Float || o instanceof Integer
|
|
|
|
|
|| o instanceof Long || o instanceof Short || o instanceof String) {
|
|
|
|
|
return o;
|
|
|
|
|
}
|
|
|
|
|
if (o.getClass().getPackage().getName().startsWith("java.")) {
|
|
|
|
@ -866,4 +836,5 @@ public class JSONObject {
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|