org.apache.tapestry5.json
Class JSONObject

java.lang.Object
  extended by org.apache.tapestry5.json.JSONCollection
      extended by org.apache.tapestry5.json.JSONObject

public final class JSONObject
extends JSONCollection

A JSONObject is an unordered collection of name/value pairs. Its external form is a string wrapped in curly braces with colons between the names and values, and commas between the values and names. The internal form is an object having 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, JSONLiteral, 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 coersion for you.

The put methods adds values to 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:


This class, and the other related classes, have been heavily modified from the original source, to fit Tapestry standards and to make use of JDK 1.5 features such as generics. Further, since the interest of Tapestry is primarily constructing JSON (and not parsing it), many of the non-essential methods have been removed (since the original code came with no tests).

Finally, support for the JSONLiteral type has been added, which allow the exact output to be controlled; useful when a JSONObject is being used as a configuration object, and must contain values that are not simple data, such as an inline function (technically making the result not JSON).


Field Summary
static Object NULL
          It is sometimes more convenient and less ambiguous to have a NULL object than to use Java's null value.
 
Constructor Summary
JSONObject()
          Construct an empty JSONObject.
JSONObject(JSONObject source, String... propertyNames)
          Construct a JSONObject from a subset of another JSONObject.
JSONObject(String... keysAndValues)
          Constructs a new JSONObject using a series of String keys and values.
JSONObject(String string)
          Construct a JSONObject from a string.
 
Method Summary
 JSONObject accumulate(String key, Object value)
          Accumulate values under a key.
 JSONObject append(String key, Object value)
          Append values to the array under a key.
 boolean equals(Object obj)
          Returns true if the other object is a JSONObject and its set of properties matches this object's properties.
 Object get(String key)
          Get the value object associated with a key.
 boolean getBoolean(String key)
          Get the boolean value associated with a key.
 double getDouble(String key)
          Get the double value associated with a key.
 int getInt(String key)
          Get the int value associated with a key.
 JSONArray getJSONArray(String key)
          Get the JSONArray value associated with a key.
 JSONObject getJSONObject(String key)
          Get the JSONObject value associated with a key.
 long getLong(String key)
          Get the long value associated with a key.
 String getString(String key)
          Get the string associated with a key.
 boolean has(String key)
          Determine if the JSONObject contains a specific key.
 boolean isNull(String key)
          Determine if the value associated with the key is null or if there is no value.
 Set<String> keys()
          Get an enumeration of the keys of the JSONObject.
 int length()
          Get the number of keys stored in the JSONObject.
 JSONArray names()
          Produce a JSONArray containing the names of the elements of this JSONObject.
 Object opt(String key)
          Get an optional value associated with a key.
 JSONObject put(String key, Object value)
          Put a key/value pair in the JSONObject.
static String quote(String string)
          Produce a string in double quotes with backslash sequences in all the right places.
 Object remove(String key)
          Remove a name and its value, if present.
 
Methods inherited from class org.apache.tapestry5.json.JSONCollection
prettyPrint, print, print, toCompactString, toString, toString
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

NULL

public static final Object NULL
It is sometimes more convenient and less ambiguous to have a NULL object than to use Java's null value. JSONObject.NULL.equals(null) returns true. JSONObject.NULL.toString() returns "null".

Constructor Detail

JSONObject

public JSONObject()
Construct an empty JSONObject.


JSONObject

public JSONObject(String... keysAndValues)
Constructs a new JSONObject using a series of String keys and values.

Since:
5.2.0

JSONObject

public JSONObject(JSONObject source,
                  String... propertyNames)
Construct a JSONObject from a subset of another JSONObject. An array of strings is used to identify the keys that should be copied. Missing keys are ignored.

Parameters:
source - A JSONObject.
propertyNames - The strings to copy.
Throws:
RuntimeException - If a value is a non-finite number.

JSONObject

public JSONObject(String string)
Construct a JSONObject from a string. This is the most commonly used JSONObject constructor.

Parameters:
string - A string beginning with { (left brace) and ending with }  (right brace).
Throws:
RuntimeException - If there is a syntax error in the source string.
Method Detail

accumulate

public JSONObject accumulate(String key,
                             Object value)
Accumulate values under a key. It is similar to the put method except that if there is already an object stored under the key then a JSONArray is stored under the key to hold all of the accumulated values. If there is already a JSONArray, then the new value is appended to it. In contrast, the put method replaces the previous value.

Parameters:
key - A key string.
value - An object to be accumulated under the key.
Returns:
this.
Throws:
{@link - RuntimeException} If the value is an invalid number or if the key is null.

append

public JSONObject append(String key,
                         Object value)
Append values to the array under a key. If the key does not exist in the JSONObject, then the key is put in the JSONObject with its value being a JSONArray containing the value parameter. If the key was already associated with a JSONArray, then the value parameter is appended to it.

Parameters:
key - A key string.
value - An object to be accumulated under the key.
Returns:
this. @ If the key is null or if the current value associated with the key is not a JSONArray.

get

public Object get(String key)
Get the value object associated with a key.

Parameters:
key - A key string.
Returns:
The object associated with the key.
Throws:
RuntimeException - if the key is not found.
See Also:
opt(String)

getBoolean

public boolean getBoolean(String key)
Get the boolean value associated with a key.

Parameters:
key - A key string.
Returns:
The truth.
Throws:
RuntimeException - if the value does not exist, is not a Boolean or the String "true" or "false".

getDouble

public double getDouble(String key)
Get the double value associated with a key.

Parameters:
key - A key string.
Returns:
The numeric value. @throws RuntimeException if the key is not found or if the value is not a Number object and cannot be converted to a number.

getInt

public int getInt(String key)
Get the int value associated with a key. If the number value is too large for an int, it will be clipped.

Parameters:
key - A key string.
Returns:
The integer value.
Throws:
RuntimeException - if the key is not found or if the value cannot be converted to an integer.

getJSONArray

public JSONArray getJSONArray(String key)
Get the JSONArray value associated with a key.

Parameters:
key - A key string.
Returns:
A JSONArray which is the value.
Throws:
RuntimeException - if the key is not found or if the value is not a JSONArray.

getJSONObject

public JSONObject getJSONObject(String key)
Get the JSONObject value associated with a key.

Parameters:
key - A key string.
Returns:
A JSONObject which is the value.
Throws:
RuntimeException - if the key is not found or if the value is not a JSONObject.

getLong

public long getLong(String key)
Get the long value associated with a key. If the number value is too long for a long, it will be clipped.

Parameters:
key - A key string.
Returns:
The long value.
Throws:
RuntimeException - if the key is not found or if the value cannot be converted to a long.

getString

public String getString(String key)
Get the string associated with a key.

Parameters:
key - A key string.
Returns:
A string which is the value.
Throws:
RuntimeException - if the key is not found.

has

public boolean has(String key)
Determine if the JSONObject contains a specific key.

Parameters:
key - A key string.
Returns:
true if the key exists in the JSONObject.

isNull

public boolean isNull(String key)
Determine if the value associated with the key is null or if there is no value.

Parameters:
key - A key string.
Returns:
true if there is no value associated with the key or if the value is the JSONObject.NULL object.

keys

public Set<String> keys()
Get an enumeration of the keys of the JSONObject. Caution: the set should not be modified.

Returns:
An iterator of the keys.

length

public int length()
Get the number of keys stored in the JSONObject.

Returns:
The number of keys in the JSONObject.

names

public JSONArray names()
Produce a JSONArray containing the names of the elements of this JSONObject.

Returns:
A JSONArray containing the key strings, or null if the JSONObject is empty.

opt

public Object opt(String key)
Get an optional value associated with a key.

Parameters:
key - A key string.
Returns:
An object which is the value, or null if there is no value.
See Also:
get(String)

put

public JSONObject put(String key,
                      Object value)
Put a key/value pair in the JSONObject. If the value is null, then the key will be removed from the JSONObject if it is present.

Parameters:
key - A key string.
value - An object which is the value. It should be of one of these types: Boolean, Double, Integer, JSONArray, JSONObject, JSONLiteral, Long, String, or the JSONObject.NULL object.
Returns:
this.
Throws:
RuntimeException - If the value is non-finite number or if the key is null.

quote

public static String quote(String string)
Produce a string in double quotes with backslash sequences in all the right places. A backslash will be inserted within
Parameters:
string - A String
Returns:
A String correctly formatted for insertion in a JSON text.

remove

public Object remove(String key)
Remove a name and its value, if present.

Parameters:
key - The name to be removed.
Returns:
The value that was associated with the name, or null if there was no value.

equals

public boolean equals(Object obj)
Returns true if the other object is a JSONObject and its set of properties matches this object's properties.

Overrides:
equals in class Object


Copyright © 2003-2012 The Apache Software Foundation.