get and opt methods for
* accessing the values by name, and put methods for adding or
* replacing values by name. The values can be any of these types:
* Boolean, JSONArray, JSONObject,
* Number, String, or the JSONObject.NULL
* object. A JSONObject constructor can be used to convert an external form
* JSON text into an internal form whose values can be retrieved with the
* get and opt methods, or to convert values into a
* JSON text using the put and toString methods.
* A get method returns a value if one can be found, and throws an
* exception if one cannot be found. An opt method returns a
* default value instead of throwing an exception, and so is useful for
* obtaining optional values.
*
* The generic get() and opt() methods return an
* object, which you can cast or query for type. There are also typed
* get and opt methods that do type checking and type
* coercion for you. The opt methods differ from the get methods in that they
* do not throw. Instead, they return a specified value, such as null.
*
* The put methods add or replace values in an object. For example,
*
myString = new JSONObject().put("JSON", "Hello, World!").toString();
* produces the string {"JSON": "Hello, World"}.
*
* The texts produced by the toString methods strictly conform to
* the JSON syntax rules.
* The constructors are more forgiving in the texts they will accept:
*
, (comma) may appear just
* before the closing brace.' (single
* quote).{ } [ ] / \ : , = ; # and if they do not look like numbers
* and if they are not the reserved words true,
* false, or null.= or => as well as
* by :.; (semicolon) as
* well as by , (comma).0x- (hex) prefix.NULL object than to use Java's null value.
* JSONObject.NULL.equals(null) returns true.
* JSONObject.NULL.toString() returns "null".
*/
public static final Object NULL = new Null();
/**
* Construct an empty JSONObject.
*/
public JSONObject() {
this.map = new TreeMap"get" or "is" followed by an uppercase letter,
* the method is invoked, and a key and the value returned from the getter method
* are put into the new JSONObject.
*
* The key is formed by removing the "get" or "is" prefix.
* If the second remaining character is not upper case, then the first
* character is converted to lower case.
*
* For example, if an object has a method named "getName", and
* if the result of calling object.getName() is "Larry Fine",
* then the JSONObject will contain "name": "Larry Fine".
*
* @param bean An object that has getter methods that should be used
* to make a JSONObject.
*/
public JSONObject(Object bean) {
this();
populateMap(bean);
}
/**
* Construct a JSONObject from an Object, using reflection to find the
* public members. The resulting JSONObject's keys will be the strings
* from the names array, and the values will be the field values associated
* with those keys in the object. If a key is not found or not visible,
* then it will not be copied into the new JSONObject.
* @param object An object that has fields that should be used to make a
* JSONObject.
* @param names An array of strings, the names of the fields to be obtained
* from the object.
*/
public JSONObject(Object object, String names[]) {
this();
Class c = object.getClass();
for (int i = 0; i < names.length; i += 1) {
String name = names[i];
try {
putOpt(name, c.getField(name).get(object));
} catch (Exception ignore) {
}
}
}
/**
* Construct a JSONObject from a source JSON text string.
* This is the most commonly used JSONObject constructor.
* @param source A string beginning
* with { (left brace) and ending
* with } (right brace).
* @exception JSONException If there is a syntax error in the source
* string or a duplicated key.
*/
public JSONObject(String source) throws JSONException {
this(new JSONTokener(source));
}
/**
* Construct a JSONObject from a ResourceBundle.
* @param baseName The ResourceBundle base name.
* @param locale The Locale to load the ResourceBundle for.
* @throws JSONException If any JSONExceptions are detected.
*/
public JSONObject(String baseName, Locale locale) throws JSONException {
this();
ResourceBundle bundle = ResourceBundle.getBundle(baseName, locale,
Thread.currentThread().getContextClassLoader());
// Iterate through the keys in the bundle.
Enumeration
* Warning: This method assumes that the data structure is acyclical.
*
* @return a printable, displayable, portable, transmittable
* representation of the object, beginning
* with { (left brace) and ending
* with } (right brace).
*/
public String toString() {
try {
Iterator
* Warning: This method assumes that the data structure is acyclical.
* @param indentFactor The number of spaces to add to each level of
* indentation.
* @return a printable, displayable, portable, transmittable
* representation of the object, beginning
* with { (left brace) and ending
* with } (right brace).
* @throws JSONException If the object contains an invalid number.
*/
public String toString(int indentFactor) throws JSONException {
return toString(indentFactor, 0);
}
/**
* Make a prettyprinted JSON text of this JSONObject.
*
* Warning: This method assumes that the data structure is acyclical.
* @param indentFactor The number of spaces to add to each level of
* indentation.
* @param indent The indentation of the top level.
* @return a printable, displayable, transmittable
* representation of the object, beginning
* with { (left brace) and ending
* with } (right brace).
* @throws JSONException If the object contains an invalid number.
*/
String toString(int indentFactor, int indent) throws JSONException {
int i;
int length = this.length();
if (length == 0) {
return "{}";
}
Iterator
* Warning: This method assumes that the data structure is acyclical.
* @param value The value to be serialized.
* @return a printable, displayable, transmittable
* representation of the object, beginning
* with { (left brace) and ending
* with } (right brace).
* @throws JSONException If the value is or contains an invalid number.
*/
public static String valueToString(Object value) throws JSONException {
if (value == null || value.equals(null)) {
return "null";
}
if (value instanceof JSONString) {
Object object;
try {
object = ((JSONString)value).toJSONString();
} catch (Exception e) {
throw new JSONException(e);
}
if (object instanceof String) {
return (String)object;
}
throw new JSONException("Bad value from toJSONString: " + object);
}
if (value instanceof Number) {
return numberToString((Number) value);
}
if (value instanceof Boolean || value instanceof JSONObject ||
value instanceof JSONArray) {
return value.toString();
}
if (value instanceof Map) {
@SuppressWarnings("unchecked")
Map
* Warning: This method assumes that the data structure is acyclical.
* @param value The value to be serialized.
* @param indentFactor The number of spaces to add to each level of
* indentation.
* @param indent The indentation of the top level.
* @return a printable, displayable, transmittable
* representation of the object, beginning
* with { (left brace) and ending
* with } (right brace).
* @throws JSONException If the object contains an invalid number.
*/
static String valueToString(
Object value,
int indentFactor,
int indent
) throws JSONException {
if (value == null || value.equals(null)) {
return "null";
}
try {
if (value instanceof JSONString) {
Object o = ((JSONString)value).toJSONString();
if (o instanceof String) {
return (String)o;
}
}
} catch (Exception ignore) {
}
if (value instanceof Number) {
return numberToString((Number) value);
}
if (value instanceof Boolean) {
return value.toString();
}
if (value instanceof JSONObject) {
return ((JSONObject)value).toString(indentFactor, indent);
}
if (value instanceof JSONArray) {
return ((JSONArray)value).toString(indentFactor, indent);
}
if (value instanceof Map) {
@SuppressWarnings("unchecked")
Map
* Warning: This method assumes that the data structure is acyclical.
*
* @return The writer.
* @throws JSONException
*/
public Writer write(Writer writer) throws JSONException {
try {
boolean commanate = false;
Iterator