org.apache.tapestry.json
Class JSONObject

java.lang.Object
  extended by org.apache.tapestry.json.JSONObject

public class JSONObject
extends Object

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, JSONObject, Number, String, or the JSONObject.NULL object.

The constructor can convert an external form string into an internal form Java object. The toString() method creates an external form string.

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 texts produced by the toString() methods are very strict. The constructors are more forgiving in the texts they will accept:

Version:
1
Author:
JSON.org

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 jo, String[] sa)
          Construct a JSONObject from a subset of another JSONObject.
JSONObject(JSONTokener x)
          Construct a JSONObject from a JSONTokener.
JSONObject(Map map)
          Construct a JSONObject from a Map.
JSONObject(String string)
          Construct a JSONObject from a string.
 
Method Summary
 JSONObject accumulate(String key, Object value)
           
 Object get(String key)
           
 boolean getBoolean(String key)
           
 double getDouble(String key)
           
 int getInt(String key)
           
 JSONArray getJSONArray(String key)
           
 JSONObject getJSONObject(String key)
           
 String getString(String key)
           
 boolean has(String key)
           
 boolean isNull(String key)
           
 Iterator keys()
           
 int length()
           
 JSONArray names()
           
static String numberToString(Number n)
          Produce a string from a number.
 Object opt(String key)
           
 boolean optBoolean(String key)
           
 boolean optBoolean(String key, boolean defaultValue)
           
 double optDouble(String key)
           
 double optDouble(String key, double defaultValue)
           
 int optInt(String key)
           
 int optInt(String key, int defaultValue)
           
 JSONArray optJSONArray(String key)
           
 JSONObject optJSONObject(String key)
           
 String optString(String key)
           
 String optString(String key, String defaultValue)
           
 JSONObject put(Object key, boolean value)
           
 JSONObject put(Object key, double value)
           
 JSONObject put(Object key, int value)
           
 JSONObject put(Object key, long value)
           
 JSONObject put(Object key, Object value)
           
 JSONObject putOpt(String key, Object value)
           
static String quote(char value)
           
static String quote(String string)
          Produce a string in double quotes with backslash sequences in all the right places.
 Object remove(String key)
           
 JSONArray toJSONArray(JSONArray names)
           
 String toString()
          
 String toString(int indentFactor)
           
 
Methods inherited from class java.lang.Object
clone, equals, 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(JSONObject jo,
                  String[] sa)
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:
jo - A JSONObject.
sa - An array of strings.

JSONObject

public JSONObject(JSONTokener x)
           throws ParseException
Construct a JSONObject from a JSONTokener.

Parameters:
x - A JSONTokener object containing the source string.
Throws:
ParseException - if there is a syntax error in the source string.

JSONObject

public JSONObject(Map map)
Construct a JSONObject from a Map.

Parameters:
map - A map object that can be used to initialize the contents of the JSONObject.

JSONObject

public JSONObject(String string)
           throws ParseException
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:
ParseException - The string must be properly formatted.
Method Detail

accumulate

public JSONObject accumulate(String key,
                             Object value)

get

public Object get(String key)

getBoolean

public boolean getBoolean(String key)

getDouble

public double getDouble(String key)

getInt

public int getInt(String key)

getJSONArray

public JSONArray getJSONArray(String key)

getJSONObject

public JSONObject getJSONObject(String key)

getString

public String getString(String key)

has

public boolean has(String key)

isNull

public boolean isNull(String key)

keys

public Iterator keys()

length

public int length()

names

public JSONArray names()

numberToString

public static String numberToString(Number n)
Produce a string from a number.

Parameters:
n - A Number
Returns:
A String.
Throws:
ArithmeticException - JSON can only serialize finite numbers.

opt

public Object opt(String key)

optBoolean

public boolean optBoolean(String key)

optBoolean

public boolean optBoolean(String key,
                          boolean defaultValue)

optDouble

public double optDouble(String key)

optDouble

public double optDouble(String key,
                        double defaultValue)

optInt

public int optInt(String key)

optInt

public int optInt(String key,
                  int defaultValue)

optJSONArray

public JSONArray optJSONArray(String key)

optJSONObject

public JSONObject optJSONObject(String key)

optString

public String optString(String key)

optString

public String optString(String key,
                        String defaultValue)

put

public JSONObject put(Object key,
                      boolean value)

put

public JSONObject put(Object key,
                      double value)

put

public JSONObject put(Object key,
                      long value)

put

public JSONObject put(Object key,
                      int value)

put

public JSONObject put(Object key,
                      Object value)

putOpt

public JSONObject putOpt(String key,
                         Object value)

quote

public static String quote(char value)
Parameters:
value -
Returns:
The character quoted.
See Also:
.

quote

public static String quote(String string)
Produce a string in double quotes with backslash sequences in all the right places.

Parameters:
string - A String
Returns:
A String correctly formatted for insertion in a JSON message.

remove

public Object remove(String key)

toJSONArray

public JSONArray toJSONArray(JSONArray names)

toString

public String toString()

Overrides:
toString in class Object

toString

public String toString(int indentFactor)


Copyright © 2006-2008 Apache Software Foundation. All Rights Reserved.