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