|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.apache.tapestry5.json.JSONCollection org.apache.tapestry5.json.JSONObject
public final class JSONObject
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.
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:
,
(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).0-
(octal) or 0x-
(hex) prefix.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 |
---|
public static final Object NULL
NULL
object than to use Java's
null
value. JSONObject.NULL.equals(null)
returns true
.
JSONObject.NULL.toString()
returns "null"
.
Constructor Detail |
---|
public JSONObject()
public JSONObject(String... keysAndValues)
public JSONObject(JSONObject source, String... propertyNames)
source
- A JSONObject.propertyNames
- The strings to copy.
RuntimeException
- If a value is a non-finite number.public JSONObject(String string)
string
- A string beginning with {
(left brace) and ending with }
(right brace).
RuntimeException
- If there is a syntax error in the source string.Method Detail |
---|
public JSONObject accumulate(String key, Object value)
key
- A key string.value
- An object to be accumulated under the key.
{@link
- RuntimeException} If the value is an invalid number or if the key is null.public JSONObject append(String key, Object value)
key
- A key string.value
- An object to be accumulated under the key.
public Object get(String key)
key
- A key string.
RuntimeException
- if the key is not found.opt(String)
public boolean getBoolean(String key)
key
- A key string.
RuntimeException
- if the value does not exist, is not a Boolean or the String "true" or "false".public double getDouble(String key)
key
- A key string.
public int getInt(String key)
key
- A key string.
RuntimeException
- if the key is not found or if the value cannot be converted to an integer.public JSONArray getJSONArray(String key)
key
- A key string.
RuntimeException
- if the key is not found or if the value is not a JSONArray.public JSONObject getJSONObject(String key)
key
- A key string.
RuntimeException
- if the key is not found or if the value is not a JSONObject.public long getLong(String key)
key
- A key string.
RuntimeException
- if the key is not found or if the value cannot be converted to a long.public String getString(String key)
key
- A key string.
RuntimeException
- if the key is not found.public boolean has(String key)
key
- A key string.
public boolean isNull(String key)
key
- A key string.
public Set<String> keys()
public int length()
public JSONArray names()
public Object opt(String key)
key
- A key string.
get(String)
public JSONObject put(String key, Object value)
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.
RuntimeException
- If the value is non-finite number or if the key is null.public static String quote(String string)
string
- A String
public Object remove(String key)
key
- The name to be removed.
public boolean equals(Object obj)
equals
in class Object
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |