Merge branch '1.5.x'
This commit is contained in:
commit
f2d3f51f3f
|
@ -30,11 +30,13 @@ class JSON {
|
||||||
static Boolean toBoolean(Object value) {
|
static Boolean toBoolean(Object value) {
|
||||||
if (value instanceof Boolean) {
|
if (value instanceof Boolean) {
|
||||||
return (Boolean) value;
|
return (Boolean) value;
|
||||||
} else if (value instanceof String) {
|
}
|
||||||
|
else if (value instanceof String) {
|
||||||
String stringValue = (String) value;
|
String stringValue = (String) value;
|
||||||
if ("true".equalsIgnoreCase(stringValue)) {
|
if ("true".equalsIgnoreCase(stringValue)) {
|
||||||
return true;
|
return true;
|
||||||
} else if ("false".equalsIgnoreCase(stringValue)) {
|
}
|
||||||
|
else if ("false".equalsIgnoreCase(stringValue)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -44,12 +46,15 @@ class JSON {
|
||||||
static Double toDouble(Object value) {
|
static Double toDouble(Object value) {
|
||||||
if (value instanceof Double) {
|
if (value instanceof Double) {
|
||||||
return (Double) value;
|
return (Double) value;
|
||||||
} else if (value instanceof Number) {
|
}
|
||||||
|
else if (value instanceof Number) {
|
||||||
return ((Number) value).doubleValue();
|
return ((Number) value).doubleValue();
|
||||||
} else if (value instanceof String) {
|
}
|
||||||
|
else if (value instanceof String) {
|
||||||
try {
|
try {
|
||||||
return Double.valueOf((String) value);
|
return Double.valueOf((String) value);
|
||||||
} catch (NumberFormatException ignored) {
|
}
|
||||||
|
catch (NumberFormatException ignored) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
|
@ -58,12 +63,15 @@ class JSON {
|
||||||
static Integer toInteger(Object value) {
|
static Integer toInteger(Object value) {
|
||||||
if (value instanceof Integer) {
|
if (value instanceof Integer) {
|
||||||
return (Integer) value;
|
return (Integer) value;
|
||||||
} else if (value instanceof Number) {
|
}
|
||||||
|
else if (value instanceof Number) {
|
||||||
return ((Number) value).intValue();
|
return ((Number) value).intValue();
|
||||||
} else if (value instanceof String) {
|
}
|
||||||
|
else if (value instanceof String) {
|
||||||
try {
|
try {
|
||||||
return (int) Double.parseDouble((String) value);
|
return (int) Double.parseDouble((String) value);
|
||||||
} catch (NumberFormatException ignored) {
|
}
|
||||||
|
catch (NumberFormatException ignored) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
|
@ -72,12 +80,15 @@ class JSON {
|
||||||
static Long toLong(Object value) {
|
static Long toLong(Object value) {
|
||||||
if (value instanceof Long) {
|
if (value instanceof Long) {
|
||||||
return (Long) value;
|
return (Long) value;
|
||||||
} else if (value instanceof Number) {
|
}
|
||||||
|
else if (value instanceof Number) {
|
||||||
return ((Number) value).longValue();
|
return ((Number) value).longValue();
|
||||||
} else if (value instanceof String) {
|
}
|
||||||
|
else if (value instanceof String) {
|
||||||
try {
|
try {
|
||||||
return (long) Double.parseDouble((String) value);
|
return (long) Double.parseDouble((String) value);
|
||||||
} catch (NumberFormatException ignored) {
|
}
|
||||||
|
catch (NumberFormatException ignored) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
|
@ -86,7 +97,8 @@ class JSON {
|
||||||
static String toString(Object value) {
|
static String toString(Object value) {
|
||||||
if (value instanceof String) {
|
if (value instanceof String) {
|
||||||
return (String) value;
|
return (String) value;
|
||||||
} else if (value != null) {
|
}
|
||||||
|
else if (value != null) {
|
||||||
return String.valueOf(value);
|
return String.valueOf(value);
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
|
@ -96,7 +108,8 @@ class JSON {
|
||||||
String requiredType) throws JSONException {
|
String requiredType) throws JSONException {
|
||||||
if (actual == null) {
|
if (actual == null) {
|
||||||
throw new JSONException("Value at " + indexOrName + " is null.");
|
throw new JSONException("Value at " + indexOrName + " is null.");
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
throw new JSONException("Value " + actual + " at " + indexOrName
|
throw new JSONException("Value " + actual + " at " + indexOrName
|
||||||
+ " of type " + actual.getClass().getName()
|
+ " of type " + actual.getClass().getName()
|
||||||
+ " cannot be converted to " + requiredType);
|
+ " cannot be converted to " + requiredType);
|
||||||
|
@ -107,7 +120,8 @@ class JSON {
|
||||||
throws JSONException {
|
throws JSONException {
|
||||||
if (actual == null) {
|
if (actual == null) {
|
||||||
throw new JSONException("Value is null.");
|
throw new JSONException("Value is null.");
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
throw new JSONException("Value " + actual
|
throw new JSONException("Value " + actual
|
||||||
+ " of type " + actual.getClass().getName()
|
+ " of type " + actual.getClass().getName()
|
||||||
+ " cannot be converted to " + requiredType);
|
+ " cannot be converted to " + requiredType);
|
||||||
|
|
|
@ -54,7 +54,7 @@ public class JSONArray {
|
||||||
* Creates a {@code JSONArray} with no values.
|
* Creates a {@code JSONArray} with no values.
|
||||||
*/
|
*/
|
||||||
public JSONArray() {
|
public JSONArray() {
|
||||||
values = new ArrayList<Object>();
|
this.values = new ArrayList<Object>();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -69,7 +69,7 @@ public class JSONArray {
|
||||||
public JSONArray(Collection copyFrom) {
|
public JSONArray(Collection copyFrom) {
|
||||||
this();
|
this();
|
||||||
if (copyFrom != null) {
|
if (copyFrom != null) {
|
||||||
for (Iterator it = copyFrom.iterator(); it.hasNext();) {
|
for (Iterator it = copyFrom.iterator(); it.hasNext(); ) {
|
||||||
put(JSONObject.wrap(it.next()));
|
put(JSONObject.wrap(it.next()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -83,6 +83,7 @@ public class JSONArray {
|
||||||
* {@code JSONArray}.
|
* {@code JSONArray}.
|
||||||
* @throws JSONException if the parse fails or doesn't yield a
|
* @throws JSONException if the parse fails or doesn't yield a
|
||||||
* {@code JSONArray}.
|
* {@code JSONArray}.
|
||||||
|
* @throws JSONException if processing of json failed
|
||||||
*/
|
*/
|
||||||
public JSONArray(JSONTokener readFrom) throws JSONException {
|
public JSONArray(JSONTokener readFrom) throws JSONException {
|
||||||
/*
|
/*
|
||||||
|
@ -91,8 +92,9 @@ public class JSONArray {
|
||||||
*/
|
*/
|
||||||
Object object = readFrom.nextValue();
|
Object object = readFrom.nextValue();
|
||||||
if (object instanceof JSONArray) {
|
if (object instanceof JSONArray) {
|
||||||
values = ((JSONArray) object).values;
|
this.values = ((JSONArray) object).values;
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
throw JSON.typeMismatch(object, "JSONArray");
|
throw JSON.typeMismatch(object, "JSONArray");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -110,13 +112,15 @@ public class JSONArray {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new {@code JSONArray} with values from the given primitive array.
|
* Creates a new {@code JSONArray} with values from the given primitive array.
|
||||||
|
* @param array a primitive array
|
||||||
|
* @throws JSONException if processing of json failed
|
||||||
*/
|
*/
|
||||||
public JSONArray(Object array) throws JSONException {
|
public JSONArray(Object array) throws JSONException {
|
||||||
if (!array.getClass().isArray()) {
|
if (!array.getClass().isArray()) {
|
||||||
throw new JSONException("Not a primitive array: " + array.getClass());
|
throw new JSONException("Not a primitive array: " + array.getClass());
|
||||||
}
|
}
|
||||||
final int length = Array.getLength(array);
|
final int length = Array.getLength(array);
|
||||||
values = new ArrayList<Object>(length);
|
this.values = new ArrayList<Object>(length);
|
||||||
for (int i = 0; i < length; ++i) {
|
for (int i = 0; i < length; ++i) {
|
||||||
put(JSONObject.wrap(Array.get(array, i)));
|
put(JSONObject.wrap(Array.get(array, i)));
|
||||||
}
|
}
|
||||||
|
@ -124,18 +128,20 @@ public class JSONArray {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the number of values in this array.
|
* Returns the number of values in this array.
|
||||||
|
* @return the length of this array
|
||||||
*/
|
*/
|
||||||
public int length() {
|
public int length() {
|
||||||
return values.size();
|
return this.values.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Appends {@code value} to the end of this array.
|
* Appends {@code value} to the end of this array.
|
||||||
*
|
*
|
||||||
|
* @param value the value
|
||||||
* @return this array.
|
* @return this array.
|
||||||
*/
|
*/
|
||||||
public JSONArray put(boolean value) {
|
public JSONArray put(boolean value) {
|
||||||
values.add(value);
|
this.values.add(value);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -145,29 +151,30 @@ public class JSONArray {
|
||||||
* @param value a finite value. May not be {@link Double#isNaN() NaNs} or
|
* @param value a finite value. May not be {@link Double#isNaN() NaNs} or
|
||||||
* {@link Double#isInfinite() infinities}.
|
* {@link Double#isInfinite() infinities}.
|
||||||
* @return this array.
|
* @return this array.
|
||||||
|
* @throws JSONException if processing of json failed
|
||||||
*/
|
*/
|
||||||
public JSONArray put(double value) throws JSONException {
|
public JSONArray put(double value) throws JSONException {
|
||||||
values.add(JSON.checkDouble(value));
|
this.values.add(JSON.checkDouble(value));
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Appends {@code value} to the end of this array.
|
* Appends {@code value} to the end of this array.
|
||||||
*
|
* @param value the value
|
||||||
* @return this array.
|
* @return this array.
|
||||||
*/
|
*/
|
||||||
public JSONArray put(int value) {
|
public JSONArray put(int value) {
|
||||||
values.add(value);
|
this.values.add(value);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Appends {@code value} to the end of this array.
|
* Appends {@code value} to the end of this array.
|
||||||
*
|
* @param value the value
|
||||||
* @return this array.
|
* @return this array.
|
||||||
*/
|
*/
|
||||||
public JSONArray put(long value) {
|
public JSONArray put(long value) {
|
||||||
values.add(value);
|
this.values.add(value);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -182,7 +189,7 @@ public class JSONArray {
|
||||||
* @return this array.
|
* @return this array.
|
||||||
*/
|
*/
|
||||||
public JSONArray put(Object value) {
|
public JSONArray put(Object value) {
|
||||||
values.add(value);
|
this.values.add(value);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -190,8 +197,10 @@ public class JSONArray {
|
||||||
* Sets the value at {@code index} to {@code value}, null padding this array
|
* Sets the value at {@code index} to {@code value}, null padding this array
|
||||||
* to the required length if necessary. If a value already exists at {@code
|
* to the required length if necessary. If a value already exists at {@code
|
||||||
* index}, it will be replaced.
|
* index}, it will be replaced.
|
||||||
*
|
* @param index the index to set the value to
|
||||||
|
* @param value the value
|
||||||
* @return this array.
|
* @return this array.
|
||||||
|
* @throws JSONException if processing of json failed
|
||||||
*/
|
*/
|
||||||
public JSONArray put(int index, boolean value) throws JSONException {
|
public JSONArray put(int index, boolean value) throws JSONException {
|
||||||
return put(index, (Boolean) value);
|
return put(index, (Boolean) value);
|
||||||
|
@ -201,10 +210,11 @@ public class JSONArray {
|
||||||
* Sets the value at {@code index} to {@code value}, null padding this array
|
* Sets the value at {@code index} to {@code value}, null padding this array
|
||||||
* to the required length if necessary. If a value already exists at {@code
|
* to the required length if necessary. If a value already exists at {@code
|
||||||
* index}, it will be replaced.
|
* index}, it will be replaced.
|
||||||
*
|
* @param index the index to set the value to
|
||||||
* @param value a finite value. May not be {@link Double#isNaN() NaNs} or
|
* @param value a finite value. May not be {@link Double#isNaN() NaNs} or
|
||||||
* {@link Double#isInfinite() infinities}.
|
* {@link Double#isInfinite() infinities}.
|
||||||
* @return this array.
|
* @return this array.
|
||||||
|
* @throws JSONException if processing of json failed
|
||||||
*/
|
*/
|
||||||
public JSONArray put(int index, double value) throws JSONException {
|
public JSONArray put(int index, double value) throws JSONException {
|
||||||
return put(index, (Double) value);
|
return put(index, (Double) value);
|
||||||
|
@ -214,8 +224,10 @@ public class JSONArray {
|
||||||
* Sets the value at {@code index} to {@code value}, null padding this array
|
* Sets the value at {@code index} to {@code value}, null padding this array
|
||||||
* to the required length if necessary. If a value already exists at {@code
|
* to the required length if necessary. If a value already exists at {@code
|
||||||
* index}, it will be replaced.
|
* index}, it will be replaced.
|
||||||
*
|
* @param index the index to set the value to
|
||||||
|
* @param value the value
|
||||||
* @return this array.
|
* @return this array.
|
||||||
|
* @throws JSONException if processing of json failed
|
||||||
*/
|
*/
|
||||||
public JSONArray put(int index, int value) throws JSONException {
|
public JSONArray put(int index, int value) throws JSONException {
|
||||||
return put(index, (Integer) value);
|
return put(index, (Integer) value);
|
||||||
|
@ -225,8 +237,10 @@ public class JSONArray {
|
||||||
* Sets the value at {@code index} to {@code value}, null padding this array
|
* Sets the value at {@code index} to {@code value}, null padding this array
|
||||||
* to the required length if necessary. If a value already exists at {@code
|
* to the required length if necessary. If a value already exists at {@code
|
||||||
* index}, it will be replaced.
|
* index}, it will be replaced.
|
||||||
*
|
* @param index the index to set the value to
|
||||||
|
* @param value the value
|
||||||
* @return this array.
|
* @return this array.
|
||||||
|
* @throws JSONException if processing of json failed
|
||||||
*/
|
*/
|
||||||
public JSONArray put(int index, long value) throws JSONException {
|
public JSONArray put(int index, long value) throws JSONException {
|
||||||
return put(index, (Long) value);
|
return put(index, (Long) value);
|
||||||
|
@ -236,28 +250,31 @@ public class JSONArray {
|
||||||
* Sets the value at {@code index} to {@code value}, null padding this array
|
* Sets the value at {@code index} to {@code value}, null padding this array
|
||||||
* to the required length if necessary. If a value already exists at {@code
|
* to the required length if necessary. If a value already exists at {@code
|
||||||
* index}, it will be replaced.
|
* index}, it will be replaced.
|
||||||
*
|
* @param index the index to set the value to
|
||||||
* @param value a {@link JSONObject}, {@link JSONArray}, String, Boolean,
|
* @param value a {@link JSONObject}, {@link JSONArray}, String, Boolean,
|
||||||
* Integer, Long, Double, {@link JSONObject#NULL}, or {@code null}. May
|
* Integer, Long, Double, {@link JSONObject#NULL}, or {@code null}. May
|
||||||
* not be {@link Double#isNaN() NaNs} or {@link Double#isInfinite()
|
* not be {@link Double#isNaN() NaNs} or {@link Double#isInfinite()
|
||||||
* infinities}.
|
* infinities}.
|
||||||
* @return this array.
|
* @return this array.
|
||||||
|
* @throws JSONException if processing of json failed
|
||||||
*/
|
*/
|
||||||
public JSONArray put(int index, Object value) throws JSONException {
|
public JSONArray put(int index, Object value) throws JSONException {
|
||||||
if (value instanceof Number) {
|
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());
|
JSON.checkDouble(((Number) value).doubleValue());
|
||||||
}
|
}
|
||||||
while (values.size() <= index) {
|
while (this.values.size() <= index) {
|
||||||
values.add(null);
|
this.values.add(null);
|
||||||
}
|
}
|
||||||
values.set(index, value);
|
this.values.set(index, value);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns true if this array has no value at {@code index}, or if its value
|
* Returns true if this array has no value at {@code index}, or if its value
|
||||||
* is the {@code null} reference or {@link JSONObject#NULL}.
|
* is the {@code null} reference or {@link JSONObject#NULL}.
|
||||||
|
* @param index the index to set the value to
|
||||||
|
* @return true if this array has no value at {@code index}
|
||||||
*/
|
*/
|
||||||
public boolean isNull(int index) {
|
public boolean isNull(int index) {
|
||||||
Object value = opt(index);
|
Object value = opt(index);
|
||||||
|
@ -266,49 +283,56 @@ public class JSONArray {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the value at {@code index}.
|
* Returns the value at {@code index}.
|
||||||
*
|
* @param index the index to get the value from
|
||||||
|
* @return the value at {@code index}.
|
||||||
* @throws JSONException if this array has no value at {@code index}, or if
|
* @throws JSONException if this array has no value at {@code index}, or if
|
||||||
* that value is the {@code null} reference. This method returns
|
* that value is the {@code null} reference. This method returns
|
||||||
* normally if the value is {@code JSONObject#NULL}.
|
* normally if the value is {@code JSONObject#NULL}.
|
||||||
*/
|
*/
|
||||||
public Object get(int index) throws JSONException {
|
public Object get(int index) throws JSONException {
|
||||||
try {
|
try {
|
||||||
Object value = values.get(index);
|
Object value = this.values.get(index);
|
||||||
if (value == null) {
|
if (value == null) {
|
||||||
throw new JSONException("Value at " + index + " is null.");
|
throw new JSONException("Value at " + index + " is null.");
|
||||||
}
|
}
|
||||||
return value;
|
return value;
|
||||||
} catch (IndexOutOfBoundsException e) {
|
}
|
||||||
throw new JSONException("Index " + index + " out of range [0.." + values.size() + ")");
|
catch (IndexOutOfBoundsException e) {
|
||||||
|
throw new JSONException("Index " + index + " out of range [0.." + this.values.size() + ")");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the value at {@code index}, or null if the array has no value
|
* Returns the value at {@code index}, or null if the array has no value
|
||||||
* at {@code index}.
|
* at {@code index}.
|
||||||
|
* @param index the index to get the value from
|
||||||
|
* @return the value at {@code index} or {@code null}
|
||||||
*/
|
*/
|
||||||
public Object opt(int index) {
|
public Object opt(int index) {
|
||||||
if (index < 0 || index >= values.size()) {
|
if (index < 0 || index >= this.values.size()) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return values.get(index);
|
return this.values.get(index);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Removes and returns the value at {@code index}, or null if the array has no value
|
* Removes and returns the value at {@code index}, or null if the array has no value
|
||||||
* at {@code index}.
|
* at {@code index}.
|
||||||
|
* @param index the index of the value to remove
|
||||||
|
* @return the previous value at {@code index}
|
||||||
*/
|
*/
|
||||||
public Object remove(int index) {
|
public Object remove(int index) {
|
||||||
if (index < 0 || index >= values.size()) {
|
if (index < 0 || index >= this.values.size()) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return values.remove(index);
|
return this.values.remove(index);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the value at {@code index} if it exists and is a boolean or can
|
* Returns the value at {@code index} if it exists and is a boolean or can
|
||||||
* be coerced to a boolean.
|
* be coerced to a boolean.
|
||||||
*
|
* @param index the index to get the value from
|
||||||
|
* @return the value at {@code index}
|
||||||
* @throws JSONException if the value at {@code index} doesn't exist or
|
* @throws JSONException if the value at {@code index} doesn't exist or
|
||||||
* cannot be coerced to a boolean.
|
* cannot be coerced to a boolean.
|
||||||
*/
|
*/
|
||||||
|
@ -324,6 +348,8 @@ public class JSONArray {
|
||||||
/**
|
/**
|
||||||
* Returns the value at {@code index} if it exists and is a boolean or can
|
* Returns the value at {@code index} if it exists and is a boolean or can
|
||||||
* be coerced to a boolean. Returns false otherwise.
|
* be coerced to a boolean. Returns false otherwise.
|
||||||
|
* @param index the index to get the value from
|
||||||
|
* @return the {@code value} or {@code false}
|
||||||
*/
|
*/
|
||||||
public boolean optBoolean(int index) {
|
public boolean optBoolean(int index) {
|
||||||
return optBoolean(index, false);
|
return optBoolean(index, false);
|
||||||
|
@ -332,6 +358,9 @@ public class JSONArray {
|
||||||
/**
|
/**
|
||||||
* Returns the value at {@code index} if it exists and is a boolean or can
|
* Returns the value at {@code index} if it exists and is a boolean or can
|
||||||
* be coerced to a boolean. Returns {@code fallback} otherwise.
|
* be coerced to a boolean. Returns {@code fallback} otherwise.
|
||||||
|
* @param index the index to get the value from
|
||||||
|
* @param fallback the fallback value
|
||||||
|
* @return the value at {@code index} of {@code fallback}
|
||||||
*/
|
*/
|
||||||
public boolean optBoolean(int index, boolean fallback) {
|
public boolean optBoolean(int index, boolean fallback) {
|
||||||
Object object = opt(index);
|
Object object = opt(index);
|
||||||
|
@ -342,7 +371,8 @@ public class JSONArray {
|
||||||
/**
|
/**
|
||||||
* Returns the value at {@code index} if it exists and is a double or can
|
* Returns the value at {@code index} if it exists and is a double or can
|
||||||
* be coerced to a double.
|
* be coerced to a double.
|
||||||
*
|
* @param index the index to get the value from
|
||||||
|
* @return the {@code value}
|
||||||
* @throws JSONException if the value at {@code index} doesn't exist or
|
* @throws JSONException if the value at {@code index} doesn't exist or
|
||||||
* cannot be coerced to a double.
|
* cannot be coerced to a double.
|
||||||
*/
|
*/
|
||||||
|
@ -358,6 +388,8 @@ public class JSONArray {
|
||||||
/**
|
/**
|
||||||
* Returns the value at {@code index} if it exists and is a double or can
|
* Returns the value at {@code index} if it exists and is a double or can
|
||||||
* be coerced to a double. Returns {@code NaN} otherwise.
|
* be coerced to a double. Returns {@code NaN} otherwise.
|
||||||
|
* @param index the index to get the value from
|
||||||
|
* @return the {@code value} or {@code NaN}
|
||||||
*/
|
*/
|
||||||
public double optDouble(int index) {
|
public double optDouble(int index) {
|
||||||
return optDouble(index, Double.NaN);
|
return optDouble(index, Double.NaN);
|
||||||
|
@ -366,6 +398,9 @@ public class JSONArray {
|
||||||
/**
|
/**
|
||||||
* Returns the value at {@code index} if it exists and is a double or can
|
* Returns the value at {@code index} if it exists and is a double or can
|
||||||
* be coerced to a double. Returns {@code fallback} otherwise.
|
* be coerced to a double. Returns {@code fallback} otherwise.
|
||||||
|
* @param index the index to get the value from
|
||||||
|
* @param fallback the fallback value
|
||||||
|
* @return the value at {@code index} of {@code fallback}
|
||||||
*/
|
*/
|
||||||
public double optDouble(int index, double fallback) {
|
public double optDouble(int index, double fallback) {
|
||||||
Object object = opt(index);
|
Object object = opt(index);
|
||||||
|
@ -376,7 +411,8 @@ public class JSONArray {
|
||||||
/**
|
/**
|
||||||
* Returns the value at {@code index} if it exists and is an int or
|
* Returns the value at {@code index} if it exists and is an int or
|
||||||
* can be coerced to an int.
|
* can be coerced to an int.
|
||||||
*
|
* @param index the index to get the value from
|
||||||
|
* @return the {@code value}
|
||||||
* @throws JSONException if the value at {@code index} doesn't exist or
|
* @throws JSONException if the value at {@code index} doesn't exist or
|
||||||
* cannot be coerced to a int.
|
* cannot be coerced to a int.
|
||||||
*/
|
*/
|
||||||
|
@ -392,6 +428,8 @@ public class JSONArray {
|
||||||
/**
|
/**
|
||||||
* Returns the value at {@code index} if it exists and is an int or
|
* Returns the value at {@code index} if it exists and is an int or
|
||||||
* can be coerced to an int. Returns 0 otherwise.
|
* can be coerced to an int. Returns 0 otherwise.
|
||||||
|
* @param index the index to get the value from
|
||||||
|
* @return the {@code value} or {@code 0}
|
||||||
*/
|
*/
|
||||||
public int optInt(int index) {
|
public int optInt(int index) {
|
||||||
return optInt(index, 0);
|
return optInt(index, 0);
|
||||||
|
@ -400,6 +438,9 @@ public class JSONArray {
|
||||||
/**
|
/**
|
||||||
* Returns the value at {@code index} if it exists and is an int or
|
* Returns the value at {@code index} if it exists and is an int or
|
||||||
* can be coerced to an int. Returns {@code fallback} otherwise.
|
* can be coerced to an int. Returns {@code fallback} otherwise.
|
||||||
|
* @param index the index to get the value from
|
||||||
|
* @param fallback the fallback value
|
||||||
|
* @return the value at {@code index} of {@code fallback}
|
||||||
*/
|
*/
|
||||||
public int optInt(int index, int fallback) {
|
public int optInt(int index, int fallback) {
|
||||||
Object object = opt(index);
|
Object object = opt(index);
|
||||||
|
@ -410,6 +451,8 @@ public class JSONArray {
|
||||||
/**
|
/**
|
||||||
* Returns the value at {@code index} if it exists and is a long or
|
* Returns the value at {@code index} if it exists and is a long or
|
||||||
* can be coerced to a long.
|
* can be coerced to a long.
|
||||||
|
* @param index the index to get the value from
|
||||||
|
* @return the {@code value}
|
||||||
*
|
*
|
||||||
* @throws JSONException if the value at {@code index} doesn't exist or
|
* @throws JSONException if the value at {@code index} doesn't exist or
|
||||||
* cannot be coerced to a long.
|
* cannot be coerced to a long.
|
||||||
|
@ -426,6 +469,8 @@ public class JSONArray {
|
||||||
/**
|
/**
|
||||||
* Returns the value at {@code index} if it exists and is a long or
|
* Returns the value at {@code index} if it exists and is a long or
|
||||||
* can be coerced to a long. Returns 0 otherwise.
|
* can be coerced to a long. Returns 0 otherwise.
|
||||||
|
* @param index the index to get the value from
|
||||||
|
* @return the {@code value} or {@code 0}
|
||||||
*/
|
*/
|
||||||
public long optLong(int index) {
|
public long optLong(int index) {
|
||||||
return optLong(index, 0L);
|
return optLong(index, 0L);
|
||||||
|
@ -434,6 +479,9 @@ public class JSONArray {
|
||||||
/**
|
/**
|
||||||
* Returns the value at {@code index} if it exists and is a long or
|
* Returns the value at {@code index} if it exists and is a long or
|
||||||
* can be coerced to a long. Returns {@code fallback} otherwise.
|
* can be coerced to a long. Returns {@code fallback} otherwise.
|
||||||
|
* @param index the index to get the value from
|
||||||
|
* @param fallback the fallback value
|
||||||
|
* @return the value at {@code index} of {@code fallback}
|
||||||
*/
|
*/
|
||||||
public long optLong(int index, long fallback) {
|
public long optLong(int index, long fallback) {
|
||||||
Object object = opt(index);
|
Object object = opt(index);
|
||||||
|
@ -444,7 +492,8 @@ public class JSONArray {
|
||||||
/**
|
/**
|
||||||
* Returns the value at {@code index} if it exists, coercing it if
|
* Returns the value at {@code index} if it exists, coercing it if
|
||||||
* necessary.
|
* necessary.
|
||||||
*
|
* @param index the index to get the value from
|
||||||
|
* @return the {@code value}
|
||||||
* @throws JSONException if no such value exists.
|
* @throws JSONException if no such value exists.
|
||||||
*/
|
*/
|
||||||
public String getString(int index) throws JSONException {
|
public String getString(int index) throws JSONException {
|
||||||
|
@ -459,6 +508,8 @@ public class JSONArray {
|
||||||
/**
|
/**
|
||||||
* Returns the value at {@code index} if it exists, coercing it if
|
* Returns the value at {@code index} if it exists, coercing it if
|
||||||
* necessary. Returns the empty string if no such value exists.
|
* necessary. Returns the empty string if no such value exists.
|
||||||
|
* @param index the index to get the value from
|
||||||
|
* @return the {@code value} or an empty string
|
||||||
*/
|
*/
|
||||||
public String optString(int index) {
|
public String optString(int index) {
|
||||||
return optString(index, "");
|
return optString(index, "");
|
||||||
|
@ -467,6 +518,9 @@ public class JSONArray {
|
||||||
/**
|
/**
|
||||||
* Returns the value at {@code index} if it exists, coercing it if
|
* Returns the value at {@code index} if it exists, coercing it if
|
||||||
* necessary. Returns {@code fallback} if no such value exists.
|
* necessary. Returns {@code fallback} if no such value exists.
|
||||||
|
* @param index the index to get the value from
|
||||||
|
* @param fallback the fallback value
|
||||||
|
* @return the value at {@code index} of {@code fallback}
|
||||||
*/
|
*/
|
||||||
public String optString(int index, String fallback) {
|
public String optString(int index, String fallback) {
|
||||||
Object object = opt(index);
|
Object object = opt(index);
|
||||||
|
@ -477,7 +531,8 @@ public class JSONArray {
|
||||||
/**
|
/**
|
||||||
* Returns the value at {@code index} if it exists and is a {@code
|
* Returns the value at {@code index} if it exists and is a {@code
|
||||||
* JSONArray}.
|
* JSONArray}.
|
||||||
*
|
* @param index the index to get the value from
|
||||||
|
* @return the array at {@code index}
|
||||||
* @throws JSONException if the value doesn't exist or is not a {@code
|
* @throws JSONException if the value doesn't exist or is not a {@code
|
||||||
* JSONArray}.
|
* JSONArray}.
|
||||||
*/
|
*/
|
||||||
|
@ -485,7 +540,8 @@ public class JSONArray {
|
||||||
Object object = get(index);
|
Object object = get(index);
|
||||||
if (object instanceof JSONArray) {
|
if (object instanceof JSONArray) {
|
||||||
return (JSONArray) object;
|
return (JSONArray) object;
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
throw JSON.typeMismatch(index, object, "JSONArray");
|
throw JSON.typeMismatch(index, object, "JSONArray");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -493,6 +549,8 @@ public class JSONArray {
|
||||||
/**
|
/**
|
||||||
* Returns the value at {@code index} if it exists and is a {@code
|
* Returns the value at {@code index} if it exists and is a {@code
|
||||||
* JSONArray}. Returns null otherwise.
|
* JSONArray}. Returns null otherwise.
|
||||||
|
* @param index the index to get the value from
|
||||||
|
* @return the array at {@code index} or {@code null}
|
||||||
*/
|
*/
|
||||||
public JSONArray optJSONArray(int index) {
|
public JSONArray optJSONArray(int index) {
|
||||||
Object object = opt(index);
|
Object object = opt(index);
|
||||||
|
@ -502,7 +560,8 @@ public class JSONArray {
|
||||||
/**
|
/**
|
||||||
* Returns the value at {@code index} if it exists and is a {@code
|
* Returns the value at {@code index} if it exists and is a {@code
|
||||||
* JSONObject}.
|
* JSONObject}.
|
||||||
*
|
* @param index the index to get the value from
|
||||||
|
* @return the object at {@code index}
|
||||||
* @throws JSONException if the value doesn't exist or is not a {@code
|
* @throws JSONException if the value doesn't exist or is not a {@code
|
||||||
* JSONObject}.
|
* JSONObject}.
|
||||||
*/
|
*/
|
||||||
|
@ -510,7 +569,8 @@ public class JSONArray {
|
||||||
Object object = get(index);
|
Object object = get(index);
|
||||||
if (object instanceof JSONObject) {
|
if (object instanceof JSONObject) {
|
||||||
return (JSONObject) object;
|
return (JSONObject) object;
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
throw JSON.typeMismatch(index, object, "JSONObject");
|
throw JSON.typeMismatch(index, object, "JSONObject");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -518,6 +578,8 @@ public class JSONArray {
|
||||||
/**
|
/**
|
||||||
* Returns the value at {@code index} if it exists and is a {@code
|
* Returns the value at {@code index} if it exists and is a {@code
|
||||||
* JSONObject}. Returns null otherwise.
|
* JSONObject}. Returns null otherwise.
|
||||||
|
* @param index the index to get the value from
|
||||||
|
* @return the object at {@code index} or {@code null}
|
||||||
*/
|
*/
|
||||||
public JSONObject optJSONObject(int index) {
|
public JSONObject optJSONObject(int index) {
|
||||||
Object object = opt(index);
|
Object object = opt(index);
|
||||||
|
@ -530,10 +592,13 @@ public class JSONArray {
|
||||||
* index from 0 through to the shorter array's length. Names that are not
|
* index from 0 through to the shorter array's length. Names that are not
|
||||||
* strings will be coerced to strings. This method returns null if either
|
* strings will be coerced to strings. This method returns null if either
|
||||||
* array is empty.
|
* array is empty.
|
||||||
|
* @param names the property names
|
||||||
|
* @return a json object
|
||||||
|
* @throws JSONException if processing of json failed
|
||||||
*/
|
*/
|
||||||
public JSONObject toJSONObject(JSONArray names) throws JSONException {
|
public JSONObject toJSONObject(JSONArray names) throws JSONException {
|
||||||
JSONObject result = new JSONObject();
|
JSONObject result = new JSONObject();
|
||||||
int length = Math.min(names.length(), values.size());
|
int length = Math.min(names.length(), this.values.size());
|
||||||
if (length == 0) {
|
if (length == 0) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -550,15 +615,18 @@ public class JSONArray {
|
||||||
* characters escaped. For example, the array containing the strings '12"
|
* characters escaped. For example, the array containing the strings '12"
|
||||||
* pizza', 'taco' and 'soda' joined on '+' returns this:
|
* pizza', 'taco' and 'soda' joined on '+' returns this:
|
||||||
* <pre>"12\" pizza"+"taco"+"soda"</pre>
|
* <pre>"12\" pizza"+"taco"+"soda"</pre>
|
||||||
|
* @param separator the separator to use
|
||||||
|
* @return the joined value
|
||||||
|
* @throws JSONException if processing of json failed
|
||||||
*/
|
*/
|
||||||
public String join(String separator) throws JSONException {
|
public String join(String separator) throws JSONException {
|
||||||
JSONStringer stringer = new JSONStringer();
|
JSONStringer stringer = new JSONStringer();
|
||||||
stringer.open(JSONStringer.Scope.NULL, "");
|
stringer.open(JSONStringer.Scope.NULL, "");
|
||||||
for (int i = 0, size = values.size(); i < size; i++) {
|
for (int i = 0, size = this.values.size(); i < size; i++) {
|
||||||
if (i > 0) {
|
if (i > 0) {
|
||||||
stringer.out.append(separator);
|
stringer.out.append(separator);
|
||||||
}
|
}
|
||||||
stringer.value(values.get(i));
|
stringer.value(this.values.get(i));
|
||||||
}
|
}
|
||||||
stringer.close(JSONStringer.Scope.NULL, JSONStringer.Scope.NULL, "");
|
stringer.close(JSONStringer.Scope.NULL, JSONStringer.Scope.NULL, "");
|
||||||
return stringer.out.toString();
|
return stringer.out.toString();
|
||||||
|
@ -567,13 +635,16 @@ public class JSONArray {
|
||||||
/**
|
/**
|
||||||
* Encodes this array as a compact JSON string, such as:
|
* Encodes this array as a compact JSON string, such as:
|
||||||
* <pre>[94043,90210]</pre>
|
* <pre>[94043,90210]</pre>
|
||||||
|
* @return a compact JSON string representation of this array
|
||||||
*/
|
*/
|
||||||
@Override public String toString() {
|
@Override
|
||||||
|
public String toString() {
|
||||||
try {
|
try {
|
||||||
JSONStringer stringer = new JSONStringer();
|
JSONStringer stringer = new JSONStringer();
|
||||||
writeTo(stringer);
|
writeTo(stringer);
|
||||||
return stringer.toString();
|
return stringer.toString();
|
||||||
} catch (JSONException e) {
|
}
|
||||||
|
catch (JSONException e) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -589,6 +660,8 @@ public class JSONArray {
|
||||||
*
|
*
|
||||||
* @param indentSpaces the number of spaces to indent for each level of
|
* @param indentSpaces the number of spaces to indent for each level of
|
||||||
* nesting.
|
* nesting.
|
||||||
|
* @return a human readable JSON string of this array
|
||||||
|
* @throws JSONException if processing of json failed
|
||||||
*/
|
*/
|
||||||
public String toString(int indentSpaces) throws JSONException {
|
public String toString(int indentSpaces) throws JSONException {
|
||||||
JSONStringer stringer = new JSONStringer(indentSpaces);
|
JSONStringer stringer = new JSONStringer(indentSpaces);
|
||||||
|
@ -598,18 +671,20 @@ public class JSONArray {
|
||||||
|
|
||||||
void writeTo(JSONStringer stringer) throws JSONException {
|
void writeTo(JSONStringer stringer) throws JSONException {
|
||||||
stringer.array();
|
stringer.array();
|
||||||
for (Object value : values) {
|
for (Object value : this.values) {
|
||||||
stringer.value(value);
|
stringer.value(value);
|
||||||
}
|
}
|
||||||
stringer.endArray();
|
stringer.endArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override public boolean equals(Object o) {
|
@Override
|
||||||
return o instanceof JSONArray && ((JSONArray) o).values.equals(values);
|
public boolean equals(Object o) {
|
||||||
|
return o instanceof JSONArray && ((JSONArray) o).values.equals(this.values);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override public int hashCode() {
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
// diverge from the original, which doesn't implement hashCode
|
// diverge from the original, which doesn't implement hashCode
|
||||||
return values.hashCode();
|
return this.values.hashCode();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -96,10 +96,13 @@ public class JSONObject {
|
||||||
* method returns "null".
|
* method returns "null".
|
||||||
*/
|
*/
|
||||||
public static final Object NULL = new Object() {
|
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
|
return o == this || o == null; // API specifies this broken equals implementation
|
||||||
}
|
}
|
||||||
@Override public String toString() {
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
return "null";
|
return "null";
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -110,7 +113,7 @@ public class JSONObject {
|
||||||
* Creates a {@code JSONObject} with no name/value mappings.
|
* Creates a {@code JSONObject} with no name/value mappings.
|
||||||
*/
|
*/
|
||||||
public JSONObject() {
|
public JSONObject() {
|
||||||
nameValuePairs = new HashMap<String, Object>();
|
this.nameValuePairs = new HashMap<String, Object>();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -134,7 +137,7 @@ public class JSONObject {
|
||||||
if (key == null) {
|
if (key == null) {
|
||||||
throw new NullPointerException("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();
|
Object object = readFrom.nextValue();
|
||||||
if (object instanceof JSONObject) {
|
if (object instanceof JSONObject) {
|
||||||
this.nameValuePairs = ((JSONObject) object).nameValuePairs;
|
this.nameValuePairs = ((JSONObject) object).nameValuePairs;
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
throw JSON.typeMismatch(object, "JSONObject");
|
throw JSON.typeMismatch(object, "JSONObject");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -176,32 +180,38 @@ public class JSONObject {
|
||||||
* Creates a new {@code JSONObject} by copying mappings for the listed names
|
* 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
|
* from the given object. Names that aren't present in {@code copyFrom} will
|
||||||
* be skipped.
|
* 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 {
|
public JSONObject(JSONObject copyFrom, String[] names) throws JSONException {
|
||||||
this();
|
this();
|
||||||
for (String name : names) {
|
for (String name : names) {
|
||||||
Object value = copyFrom.opt(name);
|
Object value = copyFrom.opt(name);
|
||||||
if (value != null) {
|
if (value != null) {
|
||||||
nameValuePairs.put(name, value);
|
this.nameValuePairs.put(name, value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the number of name/value mappings in this object.
|
* Returns the number of name/value mappings in this object.
|
||||||
|
* @return the number of name/value mappings in this object
|
||||||
*/
|
*/
|
||||||
public int length() {
|
public int length() {
|
||||||
return nameValuePairs.size();
|
return this.nameValuePairs.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Maps {@code name} to {@code value}, clobbering any existing name/value
|
* Maps {@code name} to {@code value}, clobbering any existing name/value
|
||||||
* mapping with the same name.
|
* mapping with the same name.
|
||||||
*
|
* @param name the name of the property
|
||||||
|
* @param value the value of the property
|
||||||
* @return this object.
|
* @return this object.
|
||||||
|
* @throws JSONException if an error occurs
|
||||||
*/
|
*/
|
||||||
public JSONObject put(String name, boolean value) throws JSONException {
|
public JSONObject put(String name, boolean value) throws JSONException {
|
||||||
nameValuePairs.put(checkName(name), value);
|
this.nameValuePairs.put(checkName(name), value);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -209,12 +219,14 @@ public class JSONObject {
|
||||||
* Maps {@code name} to {@code value}, clobbering any existing name/value
|
* Maps {@code name} to {@code value}, clobbering any existing name/value
|
||||||
* mapping with the same name.
|
* 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
|
* @param value a finite value. May not be {@link Double#isNaN() NaNs} or
|
||||||
* {@link Double#isInfinite() infinities}.
|
* {@link Double#isInfinite() infinities}.
|
||||||
* @return this object.
|
* @return this object.
|
||||||
|
* @throws JSONException if an error occurs
|
||||||
*/
|
*/
|
||||||
public JSONObject put(String name, double value) throws JSONException {
|
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;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -222,10 +234,13 @@ public class JSONObject {
|
||||||
* Maps {@code name} to {@code value}, clobbering any existing name/value
|
* Maps {@code name} to {@code value}, clobbering any existing name/value
|
||||||
* mapping with the same name.
|
* mapping with the same name.
|
||||||
*
|
*
|
||||||
|
* @param name the name of the property
|
||||||
|
* @param value the value of the property
|
||||||
* @return this object.
|
* @return this object.
|
||||||
|
* @throws JSONException if an error occurs
|
||||||
*/
|
*/
|
||||||
public JSONObject put(String name, int value) throws JSONException {
|
public JSONObject put(String name, int value) throws JSONException {
|
||||||
nameValuePairs.put(checkName(name), value);
|
this.nameValuePairs.put(checkName(name), value);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -233,10 +248,13 @@ public class JSONObject {
|
||||||
* Maps {@code name} to {@code value}, clobbering any existing name/value
|
* Maps {@code name} to {@code value}, clobbering any existing name/value
|
||||||
* mapping with the same name.
|
* mapping with the same name.
|
||||||
*
|
*
|
||||||
|
* @param name the name of the property
|
||||||
|
* @param value the value of the property
|
||||||
* @return this object.
|
* @return this object.
|
||||||
|
* @throws JSONException if an error occurs
|
||||||
*/
|
*/
|
||||||
public JSONObject put(String name, long value) throws JSONException {
|
public JSONObject put(String name, long value) throws JSONException {
|
||||||
nameValuePairs.put(checkName(name), value);
|
this.nameValuePairs.put(checkName(name), value);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -245,28 +263,34 @@ public class JSONObject {
|
||||||
* mapping with the same name. If the value is {@code null}, any existing
|
* mapping with the same name. If the value is {@code null}, any existing
|
||||||
* mapping for {@code name} is removed.
|
* mapping for {@code name} is removed.
|
||||||
*
|
*
|
||||||
|
* @param name the name of the property
|
||||||
* @param value a {@link JSONObject}, {@link JSONArray}, String, Boolean,
|
* @param value a {@link JSONObject}, {@link JSONArray}, String, Boolean,
|
||||||
* Integer, Long, Double, {@link #NULL}, or {@code null}. May not be
|
* Integer, Long, Double, {@link #NULL}, or {@code null}. May not be
|
||||||
* {@link Double#isNaN() NaNs} or {@link Double#isInfinite()
|
* {@link Double#isNaN() NaNs} or {@link Double#isInfinite()
|
||||||
* infinities}.
|
* infinities}.
|
||||||
* @return this object.
|
* @return this object.
|
||||||
|
* @throws JSONException if an error occurs
|
||||||
*/
|
*/
|
||||||
public JSONObject put(String name, Object value) throws JSONException {
|
public JSONObject put(String name, Object value) throws JSONException {
|
||||||
if (value == null) {
|
if (value == null) {
|
||||||
nameValuePairs.remove(name);
|
this.nameValuePairs.remove(name);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
if (value instanceof Number) {
|
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());
|
JSON.checkDouble(((Number) value).doubleValue());
|
||||||
}
|
}
|
||||||
nameValuePairs.put(checkName(name), value);
|
this.nameValuePairs.put(checkName(name), value);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Equivalent to {@code put(name, value)} when both parameters are non-null;
|
* Equivalent to {@code put(name, value)} when both parameters are non-null;
|
||||||
* does nothing otherwise.
|
* 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 {
|
public JSONObject putOpt(String name, Object value) throws JSONException {
|
||||||
if (name == null || value == null) {
|
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
|
* mapped to {@code name}. In aggregate, this allows values to be added to a
|
||||||
* mapping one at a time.
|
* mapping one at a time.
|
||||||
*
|
*
|
||||||
|
* @param name the name of the property
|
||||||
* @param value a {@link JSONObject}, {@link JSONArray}, String, Boolean,
|
* @param value a {@link JSONObject}, {@link JSONArray}, String, Boolean,
|
||||||
* Integer, Long, Double, {@link #NULL} or null. May not be {@link
|
* Integer, Long, Double, {@link #NULL} or null. May not be {@link
|
||||||
* Double#isNaN() NaNs} or {@link Double#isInfinite() infinities}.
|
* 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 {
|
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) {
|
if (current == null) {
|
||||||
return put(name, value);
|
return put(name, value);
|
||||||
}
|
}
|
||||||
|
@ -301,11 +328,12 @@ public class JSONObject {
|
||||||
if (current instanceof JSONArray) {
|
if (current instanceof JSONArray) {
|
||||||
JSONArray array = (JSONArray) current;
|
JSONArray array = (JSONArray) current;
|
||||||
array.put(value);
|
array.put(value);
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
JSONArray array = new JSONArray();
|
JSONArray array = new JSONArray();
|
||||||
array.put(current);
|
array.put(current);
|
||||||
array.put(value);
|
array.put(value);
|
||||||
nameValuePairs.put(name, array);
|
this.nameValuePairs.put(name, array);
|
||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
@ -320,37 +348,43 @@ public class JSONObject {
|
||||||
/**
|
/**
|
||||||
* Removes the named mapping if it exists; does nothing otherwise.
|
* 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
|
* @return the value previously mapped by {@code name}, or null if there was
|
||||||
* no such mapping.
|
* no such mapping.
|
||||||
*/
|
*/
|
||||||
public Object remove(String name) {
|
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
|
* Returns true if this object has no mapping for {@code name} or if it has
|
||||||
* a mapping whose value is {@link #NULL}.
|
* 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) {
|
public boolean isNull(String name) {
|
||||||
Object value = nameValuePairs.get(name);
|
Object value = this.nameValuePairs.get(name);
|
||||||
return value == null || value == NULL;
|
return value == null || value == NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns true if this object has a mapping for {@code name}. The mapping
|
* Returns true if this object has a mapping for {@code name}. The mapping
|
||||||
* may be {@link #NULL}.
|
* 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) {
|
public boolean has(String name) {
|
||||||
return nameValuePairs.containsKey(name);
|
return this.nameValuePairs.containsKey(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the value mapped by {@code 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.
|
* @throws JSONException if no such mapping exists.
|
||||||
*/
|
*/
|
||||||
public Object get(String name) throws JSONException {
|
public Object get(String name) throws JSONException {
|
||||||
Object result = nameValuePairs.get(name);
|
Object result = this.nameValuePairs.get(name);
|
||||||
if (result == null) {
|
if (result == null) {
|
||||||
throw new JSONException("No value for " + name);
|
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
|
* Returns the value mapped by {@code name}, or null if no such mapping
|
||||||
* exists.
|
* exists.
|
||||||
|
* @param name the name of the property
|
||||||
|
* @return the value or {@code null}
|
||||||
*/
|
*/
|
||||||
public Object opt(String name) {
|
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
|
* Returns the value mapped by {@code name} if it exists and is a boolean or
|
||||||
* can be coerced to a boolean.
|
* 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
|
* @throws JSONException if the mapping doesn't exist or cannot be coerced
|
||||||
* to a boolean.
|
* 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
|
* Returns the value mapped by {@code name} if it exists and is a boolean or
|
||||||
* can be coerced to a boolean. Returns false otherwise.
|
* 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) {
|
public boolean optBoolean(String name) {
|
||||||
return optBoolean(name, false);
|
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
|
* 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.
|
* 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) {
|
public boolean optBoolean(String name, boolean fallback) {
|
||||||
Object object = opt(name);
|
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
|
* Returns the value mapped by {@code name} if it exists and is a double or
|
||||||
* can be coerced to a double.
|
* 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
|
* @throws JSONException if the mapping doesn't exist or cannot be coerced
|
||||||
* to a double.
|
* 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
|
* 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.
|
* 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) {
|
public double optDouble(String name) {
|
||||||
return optDouble(name, Double.NaN);
|
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
|
* 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.
|
* 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) {
|
public double optDouble(String name, double fallback) {
|
||||||
Object object = opt(name);
|
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
|
* Returns the value mapped by {@code name} if it exists and is an int or
|
||||||
* can be coerced to an int.
|
* 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
|
* @throws JSONException if the mapping doesn't exist or cannot be coerced
|
||||||
* to an int.
|
* 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
|
* Returns the value mapped by {@code name} if it exists and is an int or
|
||||||
* can be coerced to an int. Returns 0 otherwise.
|
* 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) {
|
public int optInt(String name) {
|
||||||
return optInt(name, 0);
|
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
|
* 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.
|
* 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) {
|
public int optInt(String name, int fallback) {
|
||||||
Object object = opt(name);
|
Object object = opt(name);
|
||||||
|
@ -472,6 +528,8 @@ public class JSONObject {
|
||||||
* can be coerced to a long. Note that JSON represents numbers as doubles,
|
* 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.
|
* 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
|
* @throws JSONException if the mapping doesn't exist or cannot be coerced
|
||||||
* to a long.
|
* 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
|
* 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,
|
* 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.
|
* 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) {
|
public long optLong(String name) {
|
||||||
return optLong(name, 0L);
|
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
|
* 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 as doubles, so this is <a href="#lossy">lossy</a>; use strings to transfer
|
||||||
* numbers via JSON.
|
* 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) {
|
public long optLong(String name, long fallback) {
|
||||||
Object object = opt(name);
|
Object object = opt(name);
|
||||||
|
@ -508,7 +571,8 @@ public class JSONObject {
|
||||||
/**
|
/**
|
||||||
* Returns the value mapped by {@code name} if it exists, coercing it if
|
* Returns the value mapped by {@code name} if it exists, coercing it if
|
||||||
* necessary.
|
* necessary.
|
||||||
*
|
* @param name the name of the property
|
||||||
|
* @return the value
|
||||||
* @throws JSONException if no such mapping exists.
|
* @throws JSONException if no such mapping exists.
|
||||||
*/
|
*/
|
||||||
public String getString(String name) throws JSONException {
|
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
|
* Returns the value mapped by {@code name} if it exists, coercing it if
|
||||||
* necessary. Returns the empty string if no such mapping exists.
|
* 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) {
|
public String optString(String name) {
|
||||||
return optString(name, "");
|
return optString(name, "");
|
||||||
|
@ -531,6 +597,9 @@ public class JSONObject {
|
||||||
/**
|
/**
|
||||||
* Returns the value mapped by {@code name} if it exists, coercing it if
|
* Returns the value mapped by {@code name} if it exists, coercing it if
|
||||||
* necessary. Returns {@code fallback} if no such mapping exists.
|
* 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) {
|
public String optString(String name, String fallback) {
|
||||||
Object object = opt(name);
|
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
|
* Returns the value mapped by {@code name} if it exists and is a {@code
|
||||||
* JSONArray}.
|
* JSONArray}.
|
||||||
*
|
* @param name the name of the property
|
||||||
|
* @return the value
|
||||||
* @throws JSONException if the mapping doesn't exist or is not a {@code
|
* @throws JSONException if the mapping doesn't exist or is not a {@code
|
||||||
* JSONArray}.
|
* JSONArray}.
|
||||||
*/
|
*/
|
||||||
|
@ -549,7 +619,8 @@ public class JSONObject {
|
||||||
Object object = get(name);
|
Object object = get(name);
|
||||||
if (object instanceof JSONArray) {
|
if (object instanceof JSONArray) {
|
||||||
return (JSONArray) object;
|
return (JSONArray) object;
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
throw JSON.typeMismatch(name, object, "JSONArray");
|
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
|
* Returns the value mapped by {@code name} if it exists and is a {@code
|
||||||
* JSONArray}. Returns null otherwise.
|
* JSONArray}. Returns null otherwise.
|
||||||
|
* @param name the name of the property
|
||||||
|
* @return the value or {@code null}
|
||||||
*/
|
*/
|
||||||
public JSONArray optJSONArray(String name) {
|
public JSONArray optJSONArray(String name) {
|
||||||
Object object = opt(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
|
* Returns the value mapped by {@code name} if it exists and is a {@code
|
||||||
* JSONObject}.
|
* JSONObject}.
|
||||||
*
|
* @param name the name of the property
|
||||||
|
* @return the value
|
||||||
* @throws JSONException if the mapping doesn't exist or is not a {@code
|
* @throws JSONException if the mapping doesn't exist or is not a {@code
|
||||||
* JSONObject}.
|
* JSONObject}.
|
||||||
*/
|
*/
|
||||||
|
@ -574,7 +648,8 @@ public class JSONObject {
|
||||||
Object object = get(name);
|
Object object = get(name);
|
||||||
if (object instanceof JSONObject) {
|
if (object instanceof JSONObject) {
|
||||||
return (JSONObject) object;
|
return (JSONObject) object;
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
throw JSON.typeMismatch(name, object, "JSONObject");
|
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
|
* Returns the value mapped by {@code name} if it exists and is a {@code
|
||||||
* JSONObject}. Returns null otherwise.
|
* JSONObject}. Returns null otherwise.
|
||||||
|
* @param name the name of the property
|
||||||
|
* @return the value or {@code null}
|
||||||
*/
|
*/
|
||||||
public JSONObject optJSONObject(String name) {
|
public JSONObject optJSONObject(String name) {
|
||||||
Object object = opt(name);
|
Object object = opt(name);
|
||||||
|
@ -592,8 +669,10 @@ public class JSONObject {
|
||||||
* Returns an array with the values corresponding to {@code names}. The
|
* Returns an array with the values corresponding to {@code names}. The
|
||||||
* array contains null for names that aren't mapped. This method returns
|
* array contains null for names that aren't mapped. This method returns
|
||||||
* null if {@code names} is either null or empty.
|
* 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();
|
JSONArray result = new JSONArray();
|
||||||
if (names == null) {
|
if (names == null) {
|
||||||
return null;
|
return null;
|
||||||
|
@ -615,32 +694,37 @@ public class JSONObject {
|
||||||
* remove the corresponding mapping from this object. If this object is
|
* remove the corresponding mapping from this object. If this object is
|
||||||
* modified after the iterator is returned, the iterator's behavior is
|
* modified after the iterator is returned, the iterator's behavior is
|
||||||
* undefined. The order of the keys is undefined.
|
* undefined. The order of the keys is undefined.
|
||||||
|
* @return the keys
|
||||||
*/
|
*/
|
||||||
/* Return a raw type for API compatibility */
|
/* Return a raw type for API compatibility */
|
||||||
public Iterator keys() {
|
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 an array containing the string names in this object. This method
|
||||||
* returns null if this object contains no mappings.
|
* returns null if this object contains no mappings.
|
||||||
|
* @return the array
|
||||||
*/
|
*/
|
||||||
public JSONArray names() {
|
public JSONArray names() {
|
||||||
return nameValuePairs.isEmpty()
|
return this.nameValuePairs.isEmpty()
|
||||||
? null
|
? 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:
|
* Encodes this object as a compact JSON string, such as:
|
||||||
* <pre>{"query":"Pizza","locations":[94043,90210]}</pre>
|
* <pre>{"query":"Pizza","locations":[94043,90210]}</pre>
|
||||||
|
* @return a string representation of the object.
|
||||||
*/
|
*/
|
||||||
@Override public String toString() {
|
@Override
|
||||||
|
public String toString() {
|
||||||
try {
|
try {
|
||||||
JSONStringer stringer = new JSONStringer();
|
JSONStringer stringer = new JSONStringer();
|
||||||
writeTo(stringer);
|
writeTo(stringer);
|
||||||
return stringer.toString();
|
return stringer.toString();
|
||||||
} catch (JSONException e) {
|
}
|
||||||
|
catch (JSONException e) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -659,6 +743,8 @@ public class JSONObject {
|
||||||
*
|
*
|
||||||
* @param indentSpaces the number of spaces to indent for each level of
|
* @param indentSpaces the number of spaces to indent for each level of
|
||||||
* nesting.
|
* nesting.
|
||||||
|
* @return a string representation of the object.
|
||||||
|
* @throws JSONException if an error occurs
|
||||||
*/
|
*/
|
||||||
public String toString(int indentSpaces) throws JSONException {
|
public String toString(int indentSpaces) throws JSONException {
|
||||||
JSONStringer stringer = new JSONStringer(indentSpaces);
|
JSONStringer stringer = new JSONStringer(indentSpaces);
|
||||||
|
@ -668,7 +754,7 @@ public class JSONObject {
|
||||||
|
|
||||||
void writeTo(JSONStringer stringer) throws JSONException {
|
void writeTo(JSONStringer stringer) throws JSONException {
|
||||||
stringer.object();
|
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.key(entry.getKey()).value(entry.getValue());
|
||||||
}
|
}
|
||||||
stringer.endObject();
|
stringer.endObject();
|
||||||
|
@ -679,6 +765,8 @@ public class JSONObject {
|
||||||
*
|
*
|
||||||
* @param number a finite value. May not be {@link Double#isNaN() NaNs} or
|
* @param number a finite value. May not be {@link Double#isNaN() NaNs} or
|
||||||
* {@link Double#isInfinite() infinities}.
|
* {@link Double#isInfinite() infinities}.
|
||||||
|
* @return the encoded value
|
||||||
|
* @throws JSONException if an error occurs
|
||||||
*/
|
*/
|
||||||
public static String numberToString(Number number) throws JSONException {
|
public static String numberToString(Number number) throws JSONException {
|
||||||
if (number == null) {
|
if (number == null) {
|
||||||
|
@ -707,6 +795,7 @@ public class JSONObject {
|
||||||
*
|
*
|
||||||
* @param data the string to encode. Null will be interpreted as an empty
|
* @param data the string to encode. Null will be interpreted as an empty
|
||||||
* string.
|
* string.
|
||||||
|
* @return the quoted value
|
||||||
*/
|
*/
|
||||||
public static String quote(String data) {
|
public static String quote(String data) {
|
||||||
if (data == null) {
|
if (data == null) {
|
||||||
|
@ -718,7 +807,8 @@ public class JSONObject {
|
||||||
stringer.value(data);
|
stringer.value(data);
|
||||||
stringer.close(JSONStringer.Scope.NULL, JSONStringer.Scope.NULL, "");
|
stringer.close(JSONStringer.Scope.NULL, JSONStringer.Scope.NULL, "");
|
||||||
return stringer.toString();
|
return stringer.toString();
|
||||||
} catch (JSONException e) {
|
}
|
||||||
|
catch (JSONException e) {
|
||||||
throw new AssertionError();
|
throw new AssertionError();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -734,6 +824,8 @@ public class JSONObject {
|
||||||
* If the object is a primitive wrapper type or {@code String}, returns the object.
|
* 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}.
|
* Otherwise if the object is from a {@code java} package, returns the result of {@code toString}.
|
||||||
* If wrapping fails, returns null.
|
* If wrapping fails, returns null.
|
||||||
|
* @param o the object to wrap
|
||||||
|
* @return the wrapped object
|
||||||
*/
|
*/
|
||||||
public static Object wrap(Object o) {
|
public static Object wrap(Object o) {
|
||||||
if (o == null) {
|
if (o == null) {
|
||||||
|
@ -748,7 +840,8 @@ public class JSONObject {
|
||||||
try {
|
try {
|
||||||
if (o instanceof Collection) {
|
if (o instanceof Collection) {
|
||||||
return new JSONArray((Collection) o);
|
return new JSONArray((Collection) o);
|
||||||
} else if (o.getClass().isArray()) {
|
}
|
||||||
|
else if (o.getClass().isArray()) {
|
||||||
return new JSONArray(o);
|
return new JSONArray(o);
|
||||||
}
|
}
|
||||||
if (o instanceof Map) {
|
if (o instanceof Map) {
|
||||||
|
@ -768,7 +861,8 @@ public class JSONObject {
|
||||||
if (o.getClass().getPackage().getName().startsWith("java.")) {
|
if (o.getClass().getPackage().getName().startsWith("java.")) {
|
||||||
return o.toString();
|
return o.toString();
|
||||||
}
|
}
|
||||||
} catch (Exception ignored) {
|
}
|
||||||
|
catch (Exception ignored) {
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -20,7 +20,7 @@ import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
// Note: this class was written without inspecting the non-free org.json source code.
|
// Note: this class was written without inspecting the non-free org.json sourcecode.
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Implements {@link JSONObject#toString} and {@link JSONArray#toString}. Most
|
* Implements {@link JSONObject#toString} and {@link JSONArray#toString}. Most
|
||||||
|
@ -104,7 +104,7 @@ public class JSONStringer {
|
||||||
* A special bracketless array needed by JSONStringer.join() and
|
* A special bracketless array needed by JSONStringer.join() and
|
||||||
* JSONObject.quote() only. Not used for JSON encoding.
|
* JSONObject.quote() only. Not used for JSON encoding.
|
||||||
*/
|
*/
|
||||||
NULL
|
NULL,
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -120,13 +120,13 @@ public class JSONStringer {
|
||||||
private final String indent;
|
private final String indent;
|
||||||
|
|
||||||
public JSONStringer() {
|
public JSONStringer() {
|
||||||
indent = null;
|
this.indent = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
JSONStringer(int indentSpaces) {
|
JSONStringer(int indentSpaces) {
|
||||||
char[] indentChars = new char[indentSpaces];
|
char[] indentChars = new char[indentSpaces];
|
||||||
Arrays.fill(indentChars, ' ');
|
Arrays.fill(indentChars, ' ');
|
||||||
indent = new String(indentChars);
|
this.indent = new String(indentChars);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -134,6 +134,7 @@ public class JSONStringer {
|
||||||
* a call to {@link #endArray}.
|
* a call to {@link #endArray}.
|
||||||
*
|
*
|
||||||
* @return this stringer.
|
* @return this stringer.
|
||||||
|
* @throws JSONException if processing of json failed
|
||||||
*/
|
*/
|
||||||
public JSONStringer array() throws JSONException {
|
public JSONStringer array() throws JSONException {
|
||||||
return open(Scope.EMPTY_ARRAY, "[");
|
return open(Scope.EMPTY_ARRAY, "[");
|
||||||
|
@ -143,6 +144,7 @@ public class JSONStringer {
|
||||||
* Ends encoding the current array.
|
* Ends encoding the current array.
|
||||||
*
|
*
|
||||||
* @return this stringer.
|
* @return this stringer.
|
||||||
|
* @throws JSONException if processing of json failed
|
||||||
*/
|
*/
|
||||||
public JSONStringer endArray() throws JSONException {
|
public JSONStringer endArray() throws JSONException {
|
||||||
return close(Scope.EMPTY_ARRAY, Scope.NONEMPTY_ARRAY, "]");
|
return close(Scope.EMPTY_ARRAY, Scope.NONEMPTY_ARRAY, "]");
|
||||||
|
@ -153,6 +155,7 @@ public class JSONStringer {
|
||||||
* with a call to {@link #endObject}.
|
* with a call to {@link #endObject}.
|
||||||
*
|
*
|
||||||
* @return this stringer.
|
* @return this stringer.
|
||||||
|
* @throws JSONException if processing of json failed
|
||||||
*/
|
*/
|
||||||
public JSONStringer object() throws JSONException {
|
public JSONStringer object() throws JSONException {
|
||||||
return open(Scope.EMPTY_OBJECT, "{");
|
return open(Scope.EMPTY_OBJECT, "{");
|
||||||
|
@ -162,6 +165,7 @@ public class JSONStringer {
|
||||||
* Ends encoding the current object.
|
* Ends encoding the current object.
|
||||||
*
|
*
|
||||||
* @return this stringer.
|
* @return this stringer.
|
||||||
|
* @throws JSONException if processing of json failed
|
||||||
*/
|
*/
|
||||||
public JSONStringer endObject() throws JSONException {
|
public JSONStringer endObject() throws JSONException {
|
||||||
return close(Scope.EMPTY_OBJECT, Scope.NONEMPTY_OBJECT, "}");
|
return close(Scope.EMPTY_OBJECT, Scope.NONEMPTY_OBJECT, "}");
|
||||||
|
@ -170,20 +174,28 @@ public class JSONStringer {
|
||||||
/**
|
/**
|
||||||
* Enters a new scope by appending any necessary whitespace and the given
|
* Enters a new scope by appending any necessary whitespace and the given
|
||||||
* bracket.
|
* bracket.
|
||||||
|
* @param empty any necessary whitespace
|
||||||
|
* @param openBracket the open bracket
|
||||||
|
* @return this object
|
||||||
|
* @throws JSONException if processing of json failed
|
||||||
*/
|
*/
|
||||||
JSONStringer open(Scope empty, String openBracket) throws JSONException {
|
JSONStringer open(Scope empty, String openBracket) throws JSONException {
|
||||||
if (stack.isEmpty() && out.length() > 0) {
|
if (this.stack.isEmpty() && this.out.length() > 0) {
|
||||||
throw new JSONException("Nesting problem: multiple top-level roots");
|
throw new JSONException("Nesting problem: multiple top-level roots");
|
||||||
}
|
}
|
||||||
beforeValue();
|
beforeValue();
|
||||||
stack.add(empty);
|
this.stack.add(empty);
|
||||||
out.append(openBracket);
|
this.out.append(openBracket);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Closes the current scope by appending any necessary whitespace and the
|
* Closes the current scope by appending any necessary whitespace and the
|
||||||
* given bracket.
|
* given bracket.
|
||||||
|
* @param empty any necessary whitespace
|
||||||
|
* @param nonempty the current scope
|
||||||
|
* @param closeBracket the close bracket
|
||||||
|
* @throws JSONException if processing of json failed
|
||||||
*/
|
*/
|
||||||
JSONStringer close(Scope empty, Scope nonempty, String closeBracket) throws JSONException {
|
JSONStringer close(Scope empty, Scope nonempty, String closeBracket) throws JSONException {
|
||||||
Scope context = peek();
|
Scope context = peek();
|
||||||
|
@ -191,29 +203,32 @@ public class JSONStringer {
|
||||||
throw new JSONException("Nesting problem");
|
throw new JSONException("Nesting problem");
|
||||||
}
|
}
|
||||||
|
|
||||||
stack.remove(stack.size() - 1);
|
this.stack.remove(this.stack.size() - 1);
|
||||||
if (context == nonempty) {
|
if (context == nonempty) {
|
||||||
newline();
|
newline();
|
||||||
}
|
}
|
||||||
out.append(closeBracket);
|
this.out.append(closeBracket);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the value on the top of the stack.
|
* Returns the value on the top of the stack.
|
||||||
|
* @return the scope
|
||||||
|
* @throws JSONException if processing of json failed
|
||||||
*/
|
*/
|
||||||
private Scope peek() throws JSONException {
|
private Scope peek() throws JSONException {
|
||||||
if (stack.isEmpty()) {
|
if (this.stack.isEmpty()) {
|
||||||
throw new JSONException("Nesting problem");
|
throw new JSONException("Nesting problem");
|
||||||
}
|
}
|
||||||
return stack.get(stack.size() - 1);
|
return this.stack.get(this.stack.size() - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Replace the value on the top of the stack with the given value.
|
* Replace the value on the top of the stack with the given value.
|
||||||
|
* @param topOfStack the scope at the top of the stack
|
||||||
*/
|
*/
|
||||||
private void replaceTop(Scope topOfStack) {
|
private void replaceTop(Scope topOfStack) {
|
||||||
stack.set(stack.size() - 1, topOfStack);
|
this.stack.set(this.stack.size() - 1, topOfStack);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -223,9 +238,10 @@ public class JSONStringer {
|
||||||
* Integer, Long, Double or null. May not be {@link Double#isNaN() NaNs}
|
* Integer, Long, Double or null. May not be {@link Double#isNaN() NaNs}
|
||||||
* or {@link Double#isInfinite() infinities}.
|
* or {@link Double#isInfinite() infinities}.
|
||||||
* @return this stringer.
|
* @return this stringer.
|
||||||
|
* @throws JSONException if processing of json failed
|
||||||
*/
|
*/
|
||||||
public JSONStringer value(Object value) throws JSONException {
|
public JSONStringer value(Object value) throws JSONException {
|
||||||
if (stack.isEmpty()) {
|
if (this.stack.isEmpty()) {
|
||||||
throw new JSONException("Nesting problem");
|
throw new JSONException("Nesting problem");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -233,7 +249,8 @@ public class JSONStringer {
|
||||||
((JSONArray) value).writeTo(this);
|
((JSONArray) value).writeTo(this);
|
||||||
return this;
|
return this;
|
||||||
|
|
||||||
} else if (value instanceof JSONObject) {
|
}
|
||||||
|
else if (value instanceof JSONObject) {
|
||||||
((JSONObject) value).writeTo(this);
|
((JSONObject) value).writeTo(this);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
@ -243,12 +260,14 @@ public class JSONStringer {
|
||||||
if (value == null
|
if (value == null
|
||||||
|| value instanceof Boolean
|
|| value instanceof Boolean
|
||||||
|| value == JSONObject.NULL) {
|
|| value == JSONObject.NULL) {
|
||||||
out.append(value);
|
this.out.append(value);
|
||||||
|
|
||||||
} else if (value instanceof Number) {
|
}
|
||||||
out.append(JSONObject.numberToString((Number) value));
|
else if (value instanceof Number) {
|
||||||
|
this.out.append(JSONObject.numberToString((Number) value));
|
||||||
|
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
string(value.toString());
|
string(value.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -258,14 +277,16 @@ public class JSONStringer {
|
||||||
/**
|
/**
|
||||||
* Encodes {@code value} to this stringer.
|
* Encodes {@code value} to this stringer.
|
||||||
*
|
*
|
||||||
|
* @param value the value to encode
|
||||||
* @return this stringer.
|
* @return this stringer.
|
||||||
|
* @throws JSONException if processing of json failed
|
||||||
*/
|
*/
|
||||||
public JSONStringer value(boolean value) throws JSONException {
|
public JSONStringer value(boolean value) throws JSONException {
|
||||||
if (stack.isEmpty()) {
|
if (this.stack.isEmpty()) {
|
||||||
throw new JSONException("Nesting problem");
|
throw new JSONException("Nesting problem");
|
||||||
}
|
}
|
||||||
beforeValue();
|
beforeValue();
|
||||||
out.append(value);
|
this.out.append(value);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -275,32 +296,35 @@ public class JSONStringer {
|
||||||
* @param value a finite value. May not be {@link Double#isNaN() NaNs} or
|
* @param value a finite value. May not be {@link Double#isNaN() NaNs} or
|
||||||
* {@link Double#isInfinite() infinities}.
|
* {@link Double#isInfinite() infinities}.
|
||||||
* @return this stringer.
|
* @return this stringer.
|
||||||
|
* @throws JSONException if processing of json failed
|
||||||
*/
|
*/
|
||||||
public JSONStringer value(double value) throws JSONException {
|
public JSONStringer value(double value) throws JSONException {
|
||||||
if (stack.isEmpty()) {
|
if (this.stack.isEmpty()) {
|
||||||
throw new JSONException("Nesting problem");
|
throw new JSONException("Nesting problem");
|
||||||
}
|
}
|
||||||
beforeValue();
|
beforeValue();
|
||||||
out.append(JSONObject.numberToString(value));
|
this.out.append(JSONObject.numberToString(value));
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Encodes {@code value} to this stringer.
|
* Encodes {@code value} to this stringer.
|
||||||
*
|
*
|
||||||
|
* @param value the value to encode
|
||||||
* @return this stringer.
|
* @return this stringer.
|
||||||
|
* @throws JSONException if processing of json failed
|
||||||
*/
|
*/
|
||||||
public JSONStringer value(long value) throws JSONException {
|
public JSONStringer value(long value) throws JSONException {
|
||||||
if (stack.isEmpty()) {
|
if (this.stack.isEmpty()) {
|
||||||
throw new JSONException("Nesting problem");
|
throw new JSONException("Nesting problem");
|
||||||
}
|
}
|
||||||
beforeValue();
|
beforeValue();
|
||||||
out.append(value);
|
this.out.append(value);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void string(String value) {
|
private void string(String value) {
|
||||||
out.append("\"");
|
this.out.append("\"");
|
||||||
for (int i = 0, length = value.length(); i < length; i++) {
|
for (int i = 0, length = value.length(); i < length; i++) {
|
||||||
char c = value.charAt(i);
|
char c = value.charAt(i);
|
||||||
|
|
||||||
|
@ -314,50 +338,51 @@ public class JSONStringer {
|
||||||
case '"':
|
case '"':
|
||||||
case '\\':
|
case '\\':
|
||||||
case '/':
|
case '/':
|
||||||
out.append('\\').append(c);
|
this.out.append('\\').append(c);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case '\t':
|
case '\t':
|
||||||
out.append("\\t");
|
this.out.append("\\t");
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case '\b':
|
case '\b':
|
||||||
out.append("\\b");
|
this.out.append("\\b");
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case '\n':
|
case '\n':
|
||||||
out.append("\\n");
|
this.out.append("\\n");
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case '\r':
|
case '\r':
|
||||||
out.append("\\r");
|
this.out.append("\\r");
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case '\f':
|
case '\f':
|
||||||
out.append("\\f");
|
this.out.append("\\f");
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
if (c <= 0x1F) {
|
if (c <= 0x1F) {
|
||||||
out.append(String.format("\\u%04x", (int) c));
|
this.out.append(String.format("\\u%04x", (int) c));
|
||||||
} else {
|
}
|
||||||
out.append(c);
|
else {
|
||||||
|
this.out.append(c);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
out.append("\"");
|
this.out.append("\"");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void newline() {
|
private void newline() {
|
||||||
if (indent == null) {
|
if (this.indent == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
out.append("\n");
|
this.out.append("\n");
|
||||||
for (int i = 0; i < stack.size(); i++) {
|
for (int i = 0; i < this.stack.size(); i++) {
|
||||||
out.append(indent);
|
this.out.append(this.indent);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -366,6 +391,7 @@ public class JSONStringer {
|
||||||
*
|
*
|
||||||
* @param name the name of the forthcoming value. May not be null.
|
* @param name the name of the forthcoming value. May not be null.
|
||||||
* @return this stringer.
|
* @return this stringer.
|
||||||
|
* @throws JSONException if processing of json failed
|
||||||
*/
|
*/
|
||||||
public JSONStringer key(String name) throws JSONException {
|
public JSONStringer key(String name) throws JSONException {
|
||||||
if (name == null) {
|
if (name == null) {
|
||||||
|
@ -379,12 +405,14 @@ public class JSONStringer {
|
||||||
/**
|
/**
|
||||||
* Inserts any necessary separators and whitespace before a name. Also
|
* Inserts any necessary separators and whitespace before a name. Also
|
||||||
* adjusts the stack to expect the key's value.
|
* adjusts the stack to expect the key's value.
|
||||||
|
* @throws JSONException if processing of json failed
|
||||||
*/
|
*/
|
||||||
private void beforeKey() throws JSONException {
|
private void beforeKey() throws JSONException {
|
||||||
Scope context = peek();
|
Scope context = peek();
|
||||||
if (context == Scope.NONEMPTY_OBJECT) { // first in object
|
if (context == Scope.NONEMPTY_OBJECT) { // first in object
|
||||||
out.append(',');
|
this.out.append(',');
|
||||||
} else if (context != Scope.EMPTY_OBJECT) { // not in an object!
|
}
|
||||||
|
else if (context != Scope.EMPTY_OBJECT) { // not in an object!
|
||||||
throw new JSONException("Nesting problem");
|
throw new JSONException("Nesting problem");
|
||||||
}
|
}
|
||||||
newline();
|
newline();
|
||||||
|
@ -395,9 +423,10 @@ public class JSONStringer {
|
||||||
* Inserts any necessary separators and whitespace before a literal value,
|
* Inserts any necessary separators and whitespace before a literal value,
|
||||||
* inline array, or inline object. Also adjusts the stack to expect either a
|
* inline array, or inline object. Also adjusts the stack to expect either a
|
||||||
* closing bracket or another element.
|
* closing bracket or another element.
|
||||||
|
* @throws JSONException if processing of json failed
|
||||||
*/
|
*/
|
||||||
private void beforeValue() throws JSONException {
|
private void beforeValue() throws JSONException {
|
||||||
if (stack.isEmpty()) {
|
if (this.stack.isEmpty()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -405,13 +434,16 @@ public class JSONStringer {
|
||||||
if (context == Scope.EMPTY_ARRAY) { // first in array
|
if (context == Scope.EMPTY_ARRAY) { // first in array
|
||||||
replaceTop(Scope.NONEMPTY_ARRAY);
|
replaceTop(Scope.NONEMPTY_ARRAY);
|
||||||
newline();
|
newline();
|
||||||
} else if (context == Scope.NONEMPTY_ARRAY) { // another in array
|
}
|
||||||
out.append(',');
|
else if (context == Scope.NONEMPTY_ARRAY) { // another in array
|
||||||
|
this.out.append(',');
|
||||||
newline();
|
newline();
|
||||||
} else if (context == Scope.DANGLING_KEY) { // value for key
|
}
|
||||||
out.append(indent == null ? ":" : ": ");
|
else if (context == Scope.DANGLING_KEY) { // value for key
|
||||||
|
this.out.append(this.indent == null ? ":" : ": ");
|
||||||
replaceTop(Scope.NONEMPTY_OBJECT);
|
replaceTop(Scope.NONEMPTY_OBJECT);
|
||||||
} else if (context != Scope.NULL) {
|
}
|
||||||
|
else if (context != Scope.NULL) {
|
||||||
throw new JSONException("Nesting problem");
|
throw new JSONException("Nesting problem");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -425,8 +457,10 @@ public class JSONStringer {
|
||||||
* <p><strong>Warning:</strong> although it contradicts the general contract
|
* <p><strong>Warning:</strong> although it contradicts the general contract
|
||||||
* of {@link Object#toString}, this method returns null if the stringer
|
* of {@link Object#toString}, this method returns null if the stringer
|
||||||
* contains no data.
|
* contains no data.
|
||||||
|
* @return the encoded JSON string.
|
||||||
*/
|
*/
|
||||||
@Override public String toString() {
|
@Override
|
||||||
return out.length() == 0 ? null : out.toString();
|
public String toString() {
|
||||||
|
return this.out.length() == 0 ? null : this.out.toString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -107,14 +107,14 @@ public class JSONTokener {
|
||||||
return nextString((char) c);
|
return nextString((char) c);
|
||||||
|
|
||||||
default:
|
default:
|
||||||
pos--;
|
this.pos--;
|
||||||
return readLiteral();
|
return readLiteral();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private int nextCleanInternal() throws JSONException {
|
private int nextCleanInternal() throws JSONException {
|
||||||
while (pos < in.length()) {
|
while (this.pos < this.in.length()) {
|
||||||
int c = in.charAt(pos++);
|
int c = this.in.charAt(this.pos++);
|
||||||
switch (c) {
|
switch (c) {
|
||||||
case '\t':
|
case '\t':
|
||||||
case ' ':
|
case ' ':
|
||||||
|
@ -123,25 +123,25 @@ public class JSONTokener {
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
case '/':
|
case '/':
|
||||||
if (pos == in.length()) {
|
if (this.pos == this.in.length()) {
|
||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
char peek = in.charAt(pos);
|
char peek = this.in.charAt(this.pos);
|
||||||
switch (peek) {
|
switch (peek) {
|
||||||
case '*':
|
case '*':
|
||||||
// skip a /* c-style comment */
|
// skip a /* c-style comment */
|
||||||
pos++;
|
this.pos++;
|
||||||
int commentEnd = in.indexOf("*/", pos);
|
int commentEnd = this.in.indexOf("*/", this.pos);
|
||||||
if (commentEnd == -1) {
|
if (commentEnd == -1) {
|
||||||
throw syntaxError("Unterminated comment");
|
throw syntaxError("Unterminated comment");
|
||||||
}
|
}
|
||||||
pos = commentEnd + 2;
|
this.pos = commentEnd + 2;
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
case '/':
|
case '/':
|
||||||
// skip a // end-of-line comment
|
// skip a // end-of-line comment
|
||||||
pos++;
|
this.pos++;
|
||||||
skipToEndOfLine();
|
skipToEndOfLine();
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
@ -172,10 +172,10 @@ public class JSONTokener {
|
||||||
* caller.
|
* caller.
|
||||||
*/
|
*/
|
||||||
private void skipToEndOfLine() {
|
private void skipToEndOfLine() {
|
||||||
for (; pos < in.length(); pos++) {
|
for (; this.pos < this.in.length(); this.pos++) {
|
||||||
char c = in.charAt(pos);
|
char c = this.in.charAt(this.pos);
|
||||||
if (c == '\r' || c == '\n') {
|
if (c == '\r' || c == '\n') {
|
||||||
pos++;
|
this.pos++;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -188,8 +188,10 @@ public class JSONTokener {
|
||||||
* not include it in the returned string.
|
* not include it in the returned string.
|
||||||
*
|
*
|
||||||
* @param quote either ' or ".
|
* @param quote either ' or ".
|
||||||
|
* @return the string up to but not including {@code quote}
|
||||||
* @throws NumberFormatException if any unicode escape sequences are
|
* @throws NumberFormatException if any unicode escape sequences are
|
||||||
* malformed.
|
* malformed.
|
||||||
|
* @throws JSONException if processing of json failed
|
||||||
*/
|
*/
|
||||||
public String nextString(char quote) throws JSONException {
|
public String nextString(char quote) throws JSONException {
|
||||||
/*
|
/*
|
||||||
|
@ -200,30 +202,31 @@ public class JSONTokener {
|
||||||
StringBuilder builder = null;
|
StringBuilder builder = null;
|
||||||
|
|
||||||
/* the index of the first character not yet appended to the builder. */
|
/* the index of the first character not yet appended to the builder. */
|
||||||
int start = pos;
|
int start = this.pos;
|
||||||
|
|
||||||
while (pos < in.length()) {
|
while (this.pos < this.in.length()) {
|
||||||
int c = in.charAt(pos++);
|
int c = this.in.charAt(this.pos++);
|
||||||
if (c == quote) {
|
if (c == quote) {
|
||||||
if (builder == null) {
|
if (builder == null) {
|
||||||
// a new string avoids leaking memory
|
// a new string avoids leaking memory
|
||||||
return new String(in.substring(start, pos - 1));
|
return new String(this.in.substring(start, this.pos - 1));
|
||||||
} else {
|
}
|
||||||
builder.append(in, start, pos - 1);
|
else {
|
||||||
|
builder.append(this.in, start, this.pos - 1);
|
||||||
return builder.toString();
|
return builder.toString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (c == '\\') {
|
if (c == '\\') {
|
||||||
if (pos == in.length()) {
|
if (this.pos == this.in.length()) {
|
||||||
throw syntaxError("Unterminated escape sequence");
|
throw syntaxError("Unterminated escape sequence");
|
||||||
}
|
}
|
||||||
if (builder == null) {
|
if (builder == null) {
|
||||||
builder = new StringBuilder();
|
builder = new StringBuilder();
|
||||||
}
|
}
|
||||||
builder.append(in, start, pos - 1);
|
builder.append(this.in, start, this.pos - 1);
|
||||||
builder.append(readEscapeCharacter());
|
builder.append(readEscapeCharacter());
|
||||||
start = pos;
|
start = this.pos;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -236,18 +239,20 @@ public class JSONTokener {
|
||||||
* been read. This supports both unicode escapes "u000A" and two-character
|
* been read. This supports both unicode escapes "u000A" and two-character
|
||||||
* escapes "\n".
|
* escapes "\n".
|
||||||
*
|
*
|
||||||
|
* @return the unescaped char
|
||||||
* @throws NumberFormatException if any unicode escape sequences are
|
* @throws NumberFormatException if any unicode escape sequences are
|
||||||
* malformed.
|
* malformed.
|
||||||
|
* @throws JSONException if processing of json failed
|
||||||
*/
|
*/
|
||||||
private char readEscapeCharacter() throws JSONException {
|
private char readEscapeCharacter() throws JSONException {
|
||||||
char escaped = in.charAt(pos++);
|
char escaped = this.in.charAt(this.pos++);
|
||||||
switch (escaped) {
|
switch (escaped) {
|
||||||
case 'u':
|
case 'u':
|
||||||
if (pos + 4 > in.length()) {
|
if (this.pos + 4 > this.in.length()) {
|
||||||
throw syntaxError("Unterminated escape sequence");
|
throw syntaxError("Unterminated escape sequence");
|
||||||
}
|
}
|
||||||
String hex = in.substring(pos, pos + 4);
|
String hex = this.in.substring(this.pos, this.pos + 4);
|
||||||
pos += 4;
|
this.pos += 4;
|
||||||
return (char) Integer.parseInt(hex, 16);
|
return (char) Integer.parseInt(hex, 16);
|
||||||
|
|
||||||
case 't':
|
case 't':
|
||||||
|
@ -277,17 +282,22 @@ public class JSONTokener {
|
||||||
* Reads a null, boolean, numeric or unquoted string literal value. Numeric
|
* Reads a null, boolean, numeric or unquoted string literal value. Numeric
|
||||||
* values will be returned as an Integer, Long, or Double, in that order of
|
* values will be returned as an Integer, Long, or Double, in that order of
|
||||||
* preference.
|
* preference.
|
||||||
|
* @return a literal value
|
||||||
|
* @throws JSONException if processing of json failed
|
||||||
*/
|
*/
|
||||||
private Object readLiteral() throws JSONException {
|
private Object readLiteral() throws JSONException {
|
||||||
String literal = nextToInternal("{}[]/\\:,=;# \t\f");
|
String literal = nextToInternal("{}[]/\\:,=;# \t\f");
|
||||||
|
|
||||||
if (literal.length() == 0) {
|
if (literal.length() == 0) {
|
||||||
throw syntaxError("Expected literal value");
|
throw syntaxError("Expected literal value");
|
||||||
} else if ("null".equalsIgnoreCase(literal)) {
|
}
|
||||||
|
else if ("null".equalsIgnoreCase(literal)) {
|
||||||
return JSONObject.NULL;
|
return JSONObject.NULL;
|
||||||
} else if ("true".equalsIgnoreCase(literal)) {
|
}
|
||||||
|
else if ("true".equalsIgnoreCase(literal)) {
|
||||||
return Boolean.TRUE;
|
return Boolean.TRUE;
|
||||||
} else if ("false".equalsIgnoreCase(literal)) {
|
}
|
||||||
|
else if ("false".equalsIgnoreCase(literal)) {
|
||||||
return Boolean.FALSE;
|
return Boolean.FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -298,7 +308,8 @@ public class JSONTokener {
|
||||||
if (number.startsWith("0x") || number.startsWith("0X")) {
|
if (number.startsWith("0x") || number.startsWith("0X")) {
|
||||||
number = number.substring(2);
|
number = number.substring(2);
|
||||||
base = 16;
|
base = 16;
|
||||||
} else if (number.startsWith("0") && number.length() > 1) {
|
}
|
||||||
|
else if (number.startsWith("0") && number.length() > 1) {
|
||||||
number = number.substring(1);
|
number = number.substring(1);
|
||||||
base = 8;
|
base = 8;
|
||||||
}
|
}
|
||||||
|
@ -306,10 +317,12 @@ public class JSONTokener {
|
||||||
long longValue = Long.parseLong(number, base);
|
long longValue = Long.parseLong(number, base);
|
||||||
if (longValue <= Integer.MAX_VALUE && longValue >= Integer.MIN_VALUE) {
|
if (longValue <= Integer.MAX_VALUE && longValue >= Integer.MIN_VALUE) {
|
||||||
return (int) longValue;
|
return (int) longValue;
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
return longValue;
|
return longValue;
|
||||||
}
|
}
|
||||||
} catch (NumberFormatException e) {
|
}
|
||||||
|
catch (NumberFormatException e) {
|
||||||
/*
|
/*
|
||||||
* This only happens for integral numbers greater than
|
* This only happens for integral numbers greater than
|
||||||
* Long.MAX_VALUE, numbers in exponential form (5e-10) and
|
* Long.MAX_VALUE, numbers in exponential form (5e-10) and
|
||||||
|
@ -321,7 +334,8 @@ public class JSONTokener {
|
||||||
/* ...next try to parse as a floating point... */
|
/* ...next try to parse as a floating point... */
|
||||||
try {
|
try {
|
||||||
return Double.valueOf(literal);
|
return Double.valueOf(literal);
|
||||||
} catch (NumberFormatException ignored) {
|
}
|
||||||
|
catch (NumberFormatException ignored) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ... finally give up. We have an unquoted string */
|
/* ... finally give up. We have an unquoted string */
|
||||||
|
@ -331,21 +345,25 @@ public class JSONTokener {
|
||||||
/**
|
/**
|
||||||
* Returns the string up to but not including any of the given characters or
|
* Returns the string up to but not including any of the given characters or
|
||||||
* a newline character. This does not consume the excluded character.
|
* a newline character. This does not consume the excluded character.
|
||||||
|
* @return the string up to but not including any of the given characters or
|
||||||
|
* a newline character
|
||||||
*/
|
*/
|
||||||
private String nextToInternal(String excluded) {
|
private String nextToInternal(String excluded) {
|
||||||
int start = pos;
|
int start = this.pos;
|
||||||
for (; pos < in.length(); pos++) {
|
for (; this.pos < this.in.length(); this.pos++) {
|
||||||
char c = in.charAt(pos);
|
char c = this.in.charAt(this.pos);
|
||||||
if (c == '\r' || c == '\n' || excluded.indexOf(c) != -1) {
|
if (c == '\r' || c == '\n' || excluded.indexOf(c) != -1) {
|
||||||
return in.substring(start, pos);
|
return this.in.substring(start, this.pos);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return in.substring(start);
|
return this.in.substring(start);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reads a sequence of key/value pairs and the trailing closing brace '}' of
|
* Reads a sequence of key/value pairs and the trailing closing brace '}' of
|
||||||
* an object. The opening brace '{' should have already been read.
|
* an object. The opening brace '{' should have already been read.
|
||||||
|
* @return an object
|
||||||
|
* @throws JSONException if processing of json failed
|
||||||
*/
|
*/
|
||||||
private JSONObject readObject() throws JSONException {
|
private JSONObject readObject() throws JSONException {
|
||||||
JSONObject result = new JSONObject();
|
JSONObject result = new JSONObject();
|
||||||
|
@ -354,8 +372,9 @@ public class JSONTokener {
|
||||||
int first = nextCleanInternal();
|
int first = nextCleanInternal();
|
||||||
if (first == '}') {
|
if (first == '}') {
|
||||||
return result;
|
return result;
|
||||||
} else if (first != -1) {
|
}
|
||||||
pos--;
|
else if (first != -1) {
|
||||||
|
this.pos--;
|
||||||
}
|
}
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
|
@ -363,7 +382,8 @@ public class JSONTokener {
|
||||||
if (!(name instanceof String)) {
|
if (!(name instanceof String)) {
|
||||||
if (name == null) {
|
if (name == null) {
|
||||||
throw syntaxError("Names cannot be null");
|
throw syntaxError("Names cannot be null");
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
throw syntaxError("Names must be strings, but " + name
|
throw syntaxError("Names must be strings, but " + name
|
||||||
+ " is of type " + name.getClass().getName());
|
+ " is of type " + name.getClass().getName());
|
||||||
}
|
}
|
||||||
|
@ -378,8 +398,8 @@ public class JSONTokener {
|
||||||
if (separator != ':' && separator != '=') {
|
if (separator != ':' && separator != '=') {
|
||||||
throw syntaxError("Expected ':' after " + name);
|
throw syntaxError("Expected ':' after " + name);
|
||||||
}
|
}
|
||||||
if (pos < in.length() && in.charAt(pos) == '>') {
|
if (this.pos < this.in.length() && this.in.charAt(this.pos) == '>') {
|
||||||
pos++;
|
this.pos++;
|
||||||
}
|
}
|
||||||
|
|
||||||
result.put((String) name, nextValue());
|
result.put((String) name, nextValue());
|
||||||
|
@ -401,6 +421,8 @@ public class JSONTokener {
|
||||||
* array. The opening brace '[' should have already been read. Note that
|
* array. The opening brace '[' should have already been read. Note that
|
||||||
* "[]" yields an empty array, but "[,]" returns a two-element array
|
* "[]" yields an empty array, but "[,]" returns a two-element array
|
||||||
* equivalent to "[null,null]".
|
* equivalent to "[null,null]".
|
||||||
|
* @return an array
|
||||||
|
* @throws JSONException if processing of json failed
|
||||||
*/
|
*/
|
||||||
private JSONArray readArray() throws JSONException {
|
private JSONArray readArray() throws JSONException {
|
||||||
JSONArray result = new JSONArray();
|
JSONArray result = new JSONArray();
|
||||||
|
@ -424,7 +446,7 @@ public class JSONTokener {
|
||||||
hasTrailingSeparator = true;
|
hasTrailingSeparator = true;
|
||||||
continue;
|
continue;
|
||||||
default:
|
default:
|
||||||
pos--;
|
this.pos--;
|
||||||
}
|
}
|
||||||
|
|
||||||
result.put(nextValue());
|
result.put(nextValue());
|
||||||
|
@ -445,6 +467,8 @@ public class JSONTokener {
|
||||||
/**
|
/**
|
||||||
* Returns an exception containing the given message plus the current
|
* Returns an exception containing the given message plus the current
|
||||||
* position and the entire input string.
|
* position and the entire input string.
|
||||||
|
* @param message the message
|
||||||
|
* @return an exception
|
||||||
*/
|
*/
|
||||||
public JSONException syntaxError(String message) {
|
public JSONException syntaxError(String message) {
|
||||||
return new JSONException(message + this);
|
return new JSONException(message + this);
|
||||||
|
@ -452,10 +476,12 @@ public class JSONTokener {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the current position and the entire input string.
|
* Returns the current position and the entire input string.
|
||||||
|
* @return the current position and the entire input string.
|
||||||
*/
|
*/
|
||||||
@Override public String toString() {
|
@Override
|
||||||
|
public String toString() {
|
||||||
// consistent with the original implementation
|
// consistent with the original implementation
|
||||||
return " at character " + pos + " of " + in;
|
return " at character " + this.pos + " of " + this.in;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -466,26 +492,14 @@ public class JSONTokener {
|
||||||
* implementation and may be used by some clients.
|
* implementation and may be used by some clients.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns true until the input has been exhausted.
|
|
||||||
*/
|
|
||||||
public boolean more() {
|
public boolean more() {
|
||||||
return pos < in.length();
|
return this.pos < this.in.length();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the next available character, or the null character '\0' if all
|
|
||||||
* input has been exhausted. The return value of this method is ambiguous
|
|
||||||
* for JSON strings that contain the character '\0'.
|
|
||||||
*/
|
|
||||||
public char next() {
|
public char next() {
|
||||||
return pos < in.length() ? in.charAt(pos++) : '\0';
|
return this.pos < this.in.length() ? this.in.charAt(this.pos++) : '\0';
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the next available character if it equals {@code c}. Otherwise an
|
|
||||||
* exception is thrown.
|
|
||||||
*/
|
|
||||||
public char next(char c) throws JSONException {
|
public char next(char c) throws JSONException {
|
||||||
char result = next();
|
char result = next();
|
||||||
if (result != c) {
|
if (result != c) {
|
||||||
|
@ -494,53 +508,20 @@ public class JSONTokener {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the next character that is not whitespace and does not belong to
|
|
||||||
* a comment. If the input is exhausted before such a character can be
|
|
||||||
* found, the null character '\0' is returned. The return value of this
|
|
||||||
* method is ambiguous for JSON strings that contain the character '\0'.
|
|
||||||
*/
|
|
||||||
public char nextClean() throws JSONException {
|
public char nextClean() throws JSONException {
|
||||||
int nextCleanInt = nextCleanInternal();
|
int nextCleanInt = nextCleanInternal();
|
||||||
return nextCleanInt == -1 ? '\0' : (char) nextCleanInt;
|
return nextCleanInt == -1 ? '\0' : (char) nextCleanInt;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the next {@code length} characters of the input.
|
|
||||||
*
|
|
||||||
* <p>The returned string shares its backing character array with this
|
|
||||||
* tokener's input string. If a reference to the returned string may be held
|
|
||||||
* indefinitely, you should use {@code new String(result)} to copy it first
|
|
||||||
* to avoid memory leaks.
|
|
||||||
*
|
|
||||||
* @throws JSONException if the remaining input is not long enough to
|
|
||||||
* satisfy this request.
|
|
||||||
*/
|
|
||||||
public String next(int length) throws JSONException {
|
public String next(int length) throws JSONException {
|
||||||
if (pos + length > in.length()) {
|
if (this.pos + length > this.in.length()) {
|
||||||
throw syntaxError(length + " is out of bounds");
|
throw syntaxError(length + " is out of bounds");
|
||||||
}
|
}
|
||||||
String result = in.substring(pos, pos + length);
|
String result = this.in.substring(this.pos, this.pos + length);
|
||||||
pos += length;
|
this.pos += length;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the {@link String#trim trimmed} string holding the characters up
|
|
||||||
* to but not including the first of:
|
|
||||||
* <ul>
|
|
||||||
* <li>any character in {@code excluded}
|
|
||||||
* <li>a newline character '\n'
|
|
||||||
* <li>a carriage return '\r'
|
|
||||||
* </ul>
|
|
||||||
*
|
|
||||||
* <p>The returned string shares its backing character array with this
|
|
||||||
* tokener's input string. If a reference to the returned string may be held
|
|
||||||
* indefinitely, you should use {@code new String(result)} to copy it first
|
|
||||||
* to avoid memory leaks.
|
|
||||||
*
|
|
||||||
* @return a possibly-empty string
|
|
||||||
*/
|
|
||||||
public String nextTo(String excluded) {
|
public String nextTo(String excluded) {
|
||||||
if (excluded == null) {
|
if (excluded == null) {
|
||||||
throw new NullPointerException("excluded == null");
|
throw new NullPointerException("excluded == null");
|
||||||
|
@ -548,63 +529,43 @@ public class JSONTokener {
|
||||||
return nextToInternal(excluded).trim();
|
return nextToInternal(excluded).trim();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Equivalent to {@code nextTo(String.valueOf(excluded))}.
|
|
||||||
*/
|
|
||||||
public String nextTo(char excluded) {
|
public String nextTo(char excluded) {
|
||||||
return nextToInternal(String.valueOf(excluded)).trim();
|
return nextToInternal(String.valueOf(excluded)).trim();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Advances past all input up to and including the next occurrence of
|
|
||||||
* {@code thru}. If the remaining input doesn't contain {@code thru}, the
|
|
||||||
* input is exhausted.
|
|
||||||
*/
|
|
||||||
public void skipPast(String thru) {
|
public void skipPast(String thru) {
|
||||||
int thruStart = in.indexOf(thru, pos);
|
int thruStart = this.in.indexOf(thru, this.pos);
|
||||||
pos = thruStart == -1 ? in.length() : (thruStart + thru.length());
|
this.pos = thruStart == -1 ? this.in.length() : (thruStart + thru.length());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Advances past all input up to but not including the next occurrence of
|
|
||||||
* {@code to}. If the remaining input doesn't contain {@code to}, the input
|
|
||||||
* is unchanged.
|
|
||||||
*/
|
|
||||||
public char skipTo(char to) {
|
public char skipTo(char to) {
|
||||||
int index = in.indexOf(to, pos);
|
int index = this.in.indexOf(to, this.pos);
|
||||||
if (index != -1) {
|
if (index != -1) {
|
||||||
pos = index;
|
this.pos = index;
|
||||||
return to;
|
return to;
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
return '\0';
|
return '\0';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Unreads the most recent character of input. If no input characters have
|
|
||||||
* been read, the input is unchanged.
|
|
||||||
*/
|
|
||||||
public void back() {
|
public void back() {
|
||||||
if (--pos == -1) {
|
if (--this.pos == -1) {
|
||||||
pos = 0;
|
this.pos = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the integer [0..15] value for the given hex character, or -1
|
|
||||||
* for non-hex input.
|
|
||||||
*
|
|
||||||
* @param hex a character in the ranges [0-9], [A-F] or [a-f]. Any other
|
|
||||||
* character will yield a -1 result.
|
|
||||||
*/
|
|
||||||
public static int dehexchar(char hex) {
|
public static int dehexchar(char hex) {
|
||||||
if (hex >= '0' && hex <= '9') {
|
if (hex >= '0' && hex <= '9') {
|
||||||
return hex - '0';
|
return hex - '0';
|
||||||
} else if (hex >= 'A' && hex <= 'F') {
|
}
|
||||||
|
else if (hex >= 'A' && hex <= 'F') {
|
||||||
return hex - 'A' + 10;
|
return hex - 'A' + 10;
|
||||||
} else if (hex >= 'a' && hex <= 'f') {
|
}
|
||||||
|
else if (hex >= 'a' && hex <= 'f') {
|
||||||
return hex - 'a' + 10;
|
return hex - 'a' + 10;
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue