|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object java.util.AbstractMap<K,V> java.util.HashMap org.apache.wink.json4j.JSONObject
public class JSONObject
Models a JSON Object.
Extension of HashMap that only allows String keys, and values which are JSON-able (such as a Java Bean).
JSON-able values are: null, and instances of String, Boolean, Number, JSONObject and JSONArray.
Instances of this class are not thread-safe.
Nested Class Summary |
---|
Nested classes/interfaces inherited from class java.util.AbstractMap |
---|
AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K,V> |
Field Summary | |
---|---|
static Object |
NULL
A constant definition reference to Java null. |
Constructor Summary | |
---|---|
JSONObject()
Create a new instance of this class. |
|
JSONObject(InputStream is)
Create a new instance of this class from the data provided from the input stream. |
|
JSONObject(InputStream is,
boolean strict)
Create a new instance of this class from the data provided from the input stream. |
|
JSONObject(JSONObject obj,
String[] keys)
Create a new instance of this class taking selected values from the underlying one. |
|
JSONObject(Map map)
Create a new instance of this class using the contents of the provided map. |
|
JSONObject(Map map,
boolean includeSuperclass)
Create a new instance of this class using the contents of the provided map. |
|
JSONObject(Object javaBean)
Create a new instance of this class using the properties of the provided JavaBean. |
|
JSONObject(Object javaBean,
boolean includeSuperclass)
Create a new instance of this class using the properties of the provided JavaBean. |
|
JSONObject(Reader rdr)
Create a new instance of this class from the data provided from the reader. |
|
JSONObject(Reader rdr,
boolean strict)
Create a new instance of this class from the data provided from the reader. |
|
JSONObject(String str)
Create a new instance of this class from the provided JSON object string. |
|
JSONObject(String str,
boolean strict)
Create a new instance of this class from the provided JSON object string. |
Method Summary | |
---|---|
JSONObject |
append(String key,
Object value)
Method to append the 'value' object to the element at entry 'key'. |
Object |
get(String key)
Method to obtain the object value for a key. |
boolean |
getBoolean(String key)
Utility method to obtain the specified key as a 'boolean' value Only boolean true, false, and the String true and false will return. |
double |
getDouble(String key)
Utility method to obtain the specified key as a 'double' value Only values of Number will be converted to double, all other types will generate an exception Provided for compatibility to other JSON models. |
int |
getInt(String key)
Utility method to obtain the specified key as a 'int' value Only values of Number will be converted to integer, all other types will generate an exception. |
JSONArray |
getJSONArray(String key)
Utility method to obtain the specified key as a JSONArray Only values that are instances of JSONArray will be returned. |
JSONObject |
getJSONObject(String key)
Utility method to obtain the specified key as a JSONObject Only values that are instances of JSONObject will be returned. |
long |
getLong(String key)
Utility method to obtain the specified key as a 'long' value Only values of Number will be converted to long, all other types will generate an exception. |
static String[] |
getNames(JSONObject obj)
Utility function that returns a String[] of all the names of the keys (attributes) of this JSONObject |
short |
getShort(String key)
Utility method to obtain the specified key as a 'short' value Only values of Number will be converted to short, all other types will generate an exception. |
String |
getString(String key)
Utility method to obtain the specified key as a 'string' value Only values that can be easily converted to string will be returned. |
boolean |
has(String key)
Method to test if a key exists in the JSONObject. |
boolean |
isNull(String key)
Method to test if a key is mapped to null. |
static boolean |
isValidObject(Object object)
Return whether the object is a valid value for a property. |
static boolean |
isValidType(Class clazz)
Return whether the class is a valid type of value for a property. |
Iterator |
keys()
Utility function that returns an iterator of all the keys (attributes) of this JSONObject |
int |
length()
Method to return the number of keys in this JSONObject. |
JSONArray |
names()
Utility function that returns a JSONArray of all the names of the keys (attributes) of this JSONObject |
Object |
opt(String key)
Method to obtain the object value for a key. |
boolean |
optBoolean(String key)
Utility method to obtain the specified key as a 'boolean' value Only returns true if the value is boolean true or the string 'true'. |
boolean |
optBoolean(String key,
boolean defaultValue)
Utility method to obtain the specified key as a 'boolean' value Only returns true if the value is boolean true or the string 'true'. |
double |
optDouble(String key)
Utility method to obtain the specified key as a 'double' value Only values of Number will be converted to double. |
double |
optDouble(String key,
double defaultValue)
Utility method to obtain the specified key as a 'double' value Only values of Number will be converted to double. |
int |
optInt(String key)
Utility method to obtain the specified key as a 'int' value Provided for compatibility to other JSON models. |
int |
optInt(String key,
int defaultValue)
Utility method to obtain the specified key as a 'int' value Provided for compatibility to other JSON models. |
JSONArray |
optJSONArray(String key)
Utility method to obtain the specified key as a JSONArray Only values that are instances of JSONArray will be returned. |
JSONArray |
optJSONArray(String key,
JSONArray defaultValue)
Utility method to obtain the specified key as a JSONArray Only values that are instances of JSONArray will be returned. |
JSONObject |
optJSONObject(String key)
Utility method to obtain the specified key as a JSONObject Only values that are instances of JSONObject will be returned. |
JSONObject |
optJSONObject(String key,
JSONObject defaultValue)
Utility method to obtain the specified key as a JSONObject Only values that are instances of JSONObject will be returned. |
long |
optLong(String key)
Utility method to obtain the specified key as a 'long' value Only values of Number will be converted to long. |
long |
optLong(String key,
long defaultValue)
Utility method to obtain the specified key as a 'long' value Only values of Number will be converted to long. |
short |
optShort(String key)
Utility method to obtain the specified key as a 'short' value Only values of Number will be converted to short. |
short |
optShort(String key,
short defaultValue)
Utility method to obtain the specified key as a 'short' value Only values of Number will be converted to short. |
String |
optString(String key)
Utility method to obtain the specified key as a 'string' value Only values that can be easily converted to string will be returned. |
String |
optString(String key,
String defaultValue)
Utility method to obtain the specified key as a 'string' value Only values that can be easily converted to string will be returned. |
Object |
put(Object key,
Object value)
(non-Javadoc) |
Object |
put(Object key,
Object value,
boolean includeSuperclass)
(non-Javadoc) |
JSONObject |
put(String key,
boolean value)
Method to add an atomic boolean to the JSONObject. |
JSONObject |
put(String key,
Collection value)
Method to add a Collection as a new JSONArray contained in this JSONObject |
JSONObject |
put(String key,
Collection value,
boolean includeSuperclass)
Method to add a Collection as a new JSONArray contained in this JSONObject |
JSONObject |
put(String key,
double value)
Method to add an atomic double to the JSONObject. |
JSONObject |
put(String key,
int value)
Method to add an atomic integer to the JSONObject. |
JSONObject |
put(String key,
long value)
Method to add an atomic long to the JSONObject. |
JSONObject |
put(String key,
Map value)
Method to add a Map as a new JSONObject contained in this JSONObject Same as calling put(String, Map, true); |
JSONObject |
put(String key,
Map value,
boolean includeSuperclass)
Method to add a Map as a new JSONObject contained in this JSONObject |
JSONObject |
put(String key,
Object value)
Similar to default HashMap put, except it returns JSONObject instead of Object. |
JSONObject |
put(String key,
Object[] value)
Method to add an Object array as a new JSONArray contained in this JSONObject |
JSONObject |
put(String key,
Object[] value,
boolean includeSuperclass)
Method to add an Object array as a new JSONArray contained in this JSONObject |
JSONObject |
put(String key,
short value)
Method to add an atomic short to the JSONObject. |
JSONObject |
putOnce(String key,
Object value)
Put a key/value pair into the JSONObject, but only if key/value are both not null, and only if the key is not present already. |
JSONObject |
putOpt(String key,
Object value)
Put a key/value pair into the JSONObject, but only if the key and value are non-null. |
Iterator |
sortedKeys()
Utility function that returns an iterator of all the keys (attributes) of this JSONObject sorted in lexicographic manner (String.compareTo). |
JSONArray |
toJSONArray(JSONArray names)
Produce a JSONArray containing the values of the members of this JSONObject |
String |
toString()
Over-ridden toString() method. |
String |
toString(boolean verbose)
Verbose capable toString method. |
String |
toString(int indentDepth)
Function to return a string of JSON text with specified indention. |
String |
write()
Convert this object into a String of JSON text. |
String |
write(boolean verbose)
Convert this object into a String of JSON text, specifying whether to use verbose (tab-indented) output or not. |
String |
write(int indentDepth)
Convert this object into a String of JSON text, specifying how many spaces should be used for each indent. |
OutputStream |
write(OutputStream os)
Write this object to the stream as JSON text in UTF-8 encoding. |
OutputStream |
write(OutputStream os,
boolean verbose)
Convert this object into a stream of JSON text. |
OutputStream |
write(OutputStream os,
int indentDepth)
Write this object to the stream as JSON text in UTF-8 encoding, specifying how many spaces should be used for each indent. |
Writer |
write(Writer writer)
Write this object to the writer as JSON text. |
Writer |
write(Writer writer,
boolean verbose)
Write this object to the writer as JSON text in UTF-8 encoding, specifying whether to use verbose (tab-indented) output or not. |
Writer |
write(Writer writer,
int indentDepth)
Write this object to the writer as JSON text, specifying how many spaces should be used for each indent. |
Methods inherited from class java.util.HashMap |
---|
clear, clone, containsKey, containsValue, entrySet, get, isEmpty, keySet, putAll, remove, size, values |
Methods inherited from class java.util.AbstractMap |
---|
equals, hashCode |
Methods inherited from class java.lang.Object |
---|
finalize, getClass, notify, notifyAll, wait, wait, wait |
Methods inherited from interface java.util.Map |
---|
equals, hashCode |
Field Detail |
---|
public static final Object NULL
Constructor Detail |
---|
public JSONObject()
public JSONObject(JSONObject obj, String[] keys) throws JSONException
obj
- The JSONObject to extract values from.keys
- The keys to take from the JSONObject and apply to this instance.
JSONException
- Thrown if a key is duplicated in the string[] keyspublic JSONObject(String str) throws JSONException
str
- The JSON string to parse.
JSONException
- Thrown when the string passed is null, or malformed JSON..public JSONObject(String str, boolean strict) throws JSONException
str
- The JSON string to parse.strict
- Whether or not to parse in 'strict' mode, meaning all strings must be quoted (including identifiers), and comments are not allowed.
JSONException
- Thrown when the string passed is null, or malformed JSON..public JSONObject(Reader rdr) throws JSONException
JSONException
- Thrown when the string passed is null, or malformed JSON..public JSONObject(Reader rdr, boolean strict) throws JSONException
rdr
- The reader from which to read the JSON.strict
- Whether or not to parse in 'strict' mode, meaning all strings must be quoted (including identifiers), and comments are not allowed.
JSONException
- Thrown when the string passed is null, or malformed JSON..public JSONObject(InputStream is) throws JSONException
is
- The InputStream from which to read the JSON.
JSONException
- Thrown when the string passed is null, or malformed JSON..public JSONObject(InputStream is, boolean strict) throws JSONException
is
- The InputStream from which to read the JSON.strict
- Whether or not to parse in 'strict' mode, meaning all strings must be quoted (including identifiers), and comments are not allowed.
JSONException
- Thrown when the string passed is null, or malformed JSON..public JSONObject(Map map)
map
- The map of key/value pairs to insert into this JSON objectpublic JSONObject(Object javaBean) throws JSONException
javaBean
- A java bean with getters for properties.
JSONException
- Thrown when contents in the map cannot be made JSONable.
NullPointerException
- Thrown if the map is null, or a key in the map is null..public JSONObject(Object javaBean, boolean includeSuperclass) throws JSONException
javaBean
- A java bean with getters for properties.includeSuperclass
- Boolean indicating that if the object is a JavaBean, include superclass getter properties.
JSONException
- Thrown when contents in the map cannot be made JSONable.
NullPointerException
- Thrown if the map is null, or a key in the map is null..public JSONObject(Map map, boolean includeSuperclass) throws JSONException
map
- The map of key/value pairs to insert into this JSON objectincludeSuperclass
- For values of the map which are JavaBeans, include superclass getter properties.
JSONException
- Thrown when contents in the map cannot be made JSONable.
NullPointerException
- Thrown if the map is null, or a key in the map is null..Method Detail |
---|
public static boolean isValidObject(Object object)
object
- The object to check for validity as a JSON property value.
public static boolean isValidType(Class clazz)
clazz
- The class type to check for validity as a JSON object type.
public OutputStream write(OutputStream os) throws JSONException
write
in interface JSONArtifact
os
- The output stream to write data to.
JSONException
- Thrown on IO errors during serialization.public OutputStream write(OutputStream os, boolean verbose) throws JSONException
write
in interface JSONArtifact
os
- The output stream to write data to.verbose
- Whether or not to write the JSON text in a verbose format.
JSONException
- Thrown on IO errors during serialization.public OutputStream write(OutputStream os, int indentDepth) throws JSONException
write
in interface JSONArtifact
indentDepth
- How many spaces to use for each indent level. Should be one to eight.
Less than one means no intending, greater than 8 and it will just use tab.
JSONException
- Thrown on IO errors during serialization.public Writer write(Writer writer) throws JSONException
write
in interface JSONArtifact
writer
- The writer which to write the JSON text to.
JSONException
- Thrown on IO errors during serialization.public Writer write(Writer writer, boolean verbose) throws JSONException
write
in interface JSONArtifact
writer
- The writer which to write the JSON text to.
JSONException
- Thrown on IO errors during serialization.public Writer write(Writer writer, int indentDepth) throws JSONException
write
in interface JSONArtifact
writer
- The writer which to write the JSON text to.indentDepth
- How many spaces to use for each indent. The value should be between one to eight.
JSONException
- Thrown on errors during serialization.public String write(int indentDepth) throws JSONException
write
in interface JSONArtifact
indentDepth
- How many spaces to use for each indent. The value should be between one to eight.
Less than one means no indenting, greater than 8 and it will just use tab.
JSONException
- Thrown on errors during serialization.public String write(boolean verbose) throws JSONException
write
in interface JSONArtifact
verbose
- Whether or not to write in compressed format.
JSONException
- Thrown on errors during serialization.public String write() throws JSONException
write
in interface JSONArtifact
JSONException
- Thrown on IO errors during serialization.public Object get(String key) throws JSONException
key
- The key (attribute) name to obtain the value for.
JSONException
- Thrown if the noted key is not in the map of key/value pairs.public Object opt(String key)
key
- The key (attribute) name to obtain the value for.public Object put(Object key, Object value, boolean includeSuperclass) throws JSONException
key
- The key to put in the JSONObjectvalue
- The value to put in the JSONObjectincludeSuperclass
- Boolean indicating that if the object is a JavaBean, include superclass getter properties.
JSONException.
- Thrown if key is null, not a string, or the value could not be converted.
JSONException
HashMap.put(java.lang.Object, java.lang.Object)
public Object put(Object key, Object value)
put
in interface Map
put
in class HashMap
This is the same as calling put(key, value, true);
public JSONObject put(String key, Object value) throws JSONException
JSONException.
- Thrown if key is null, not a string, or the value could not be converted to JSON.
JSONException
HashMap.put(java.lang.Object, java.lang.Object)
public JSONObject put(String key, boolean value) throws JSONException
value
- The boolean value.
JSONException.
- Thrown if key is null or not a string.
JSONException
public JSONObject put(String key, double value) throws JSONException
value
- The double value.
JSONException.
- Thrown if key is null or not a string.
JSONException
public JSONObject put(String key, int value) throws JSONException
value
- The integer value.
JSONException.
- Thrown if key is null or not a string.
JSONException
public JSONObject put(String key, short value) throws JSONException
value
- The integer value.
JSONException.
- Thrown if key is null or not a string.
JSONException
public JSONObject put(String key, long value) throws JSONException
key
- The key/attribute name to set the long to.value
- The long value.
JSONException.
- Thrown if key is null or not a string.
JSONException
public JSONObject put(String key, Map value, boolean includeSuperclass) throws JSONException
key
- The key/attribute name to set the new JSONObject to.value
- The Map object to convert to a JSONObject and store.includeSuperclass
- For values of the map which are JavaBeans and are converted, include superclass getter properties.
JSONException
- Thrown when contents in the map cannot be converted to something JSONable.public JSONObject put(String key, Map value) throws JSONException
key
- The key/attribute name to set the new JSONObject to.value
- The Map object to convert to a JSONObject and store.
JSONException
- Thrown when contents in the map cannot be converted to something JSONable.public JSONObject put(String key, Collection value, boolean includeSuperclass) throws JSONException
key
- The key/attribute name to set the collection to.value
- The Map object to convert to a JSONObject and store.includeSuperclass
- For values of the map which are JavaBeans and are converted, include superclass getter properties.
JSONException
- Thrown when contents in the Collection cannot be converted to something JSONable.public JSONObject put(String key, Collection value) throws JSONException
key
- The key/attribute name to set the collection to.value
- The Map object to convert to a JSONObject and store.
JSONException
- Thrown when contents in the Collection cannot be converted to something JSONable.public JSONObject put(String key, Object[] value) throws JSONException
key
- The key/attribute name to set the collection to.value
- The Object array to convert to a JSONArray and store.
JSONException
- Thrown when contents in the Collection cannot be converted to something JSONable.public JSONObject put(String key, Object[] value, boolean includeSuperclass) throws JSONException
key
- The key/attribute name to set the collection to.value
- The Object array to convert to a JSONArray and store.includeSuperclass
- For values of the Object array which are JavaBeans and are converted, include superclass getter properties.
JSONException
- Thrown when contents in the Collection cannot be converted to something JSONable.public boolean getBoolean(String key) throws JSONException
key
- The key to look up.
throws JSONException Thrown when the type returned by get(key) is not a boolean instance, or the strings 'true' or 'false'.
JSONException
public boolean optBoolean(String key)
key
- The key to look up.
public boolean optBoolean(String key, boolean defaultValue)
key
- The key to look up.defaultValue
- The default value to return.
public double getDouble(String key) throws JSONException
key
- The key to look up.
throws JSONException Thrown when the type returned by get(key) is not a Double instance, or cannot be converted to a double.
JSONException
public double optDouble(String key)
key
- The key to look up.
public double optDouble(String key, double defaultValue)
key
- The key to look up.defaultValue
- The default double value to return in case of NaN/null values in map.
public short getShort(String key) throws JSONException
key
- The key to look up.
throws JSONException Thrown when the type returned by get(key) is not a Short instance, or cannot be converted to a short.
JSONException
public short optShort(String key)
key
- The key to look up.
public short optShort(String key, short defaultValue)
key
- The key to look up.defaultValue
- The default value to return in the case of null/nonNumber values in the map.
public int getInt(String key) throws JSONException
key
- The key to look up.
throws JSONException Thrown when the type returned by get(key) is not a Double instance, or cannot be converted to a double.
JSONException
public int optInt(String key)
key
- The key to look up.
public int optInt(String key, int defaultValue)
key
- The key to look up.defaultValue
- The default int value to return in case of null/non-number values in the map.
public long getLong(String key) throws JSONException
key
- The key to look up.
throws JSONException Thrown when the type returned by get(key) is not a Long instance, or cannot be converted to a long..
JSONException
public long optLong(String key) throws JSONException
key
- The key to look up.
JSONException
public long optLong(String key, long defaultValue) throws JSONException
key
- The key to look up.defaultValue
- The default long value to return in case of null/non Number values in the map.
JSONException
public String getString(String key) throws JSONException
key
- The key to look up.
throws JSONException Thrown when the type returned by get(key) is null.
JSONException
public String optString(String key)
key
- The key to look up.
throws JSONException Thrown when the type returned by get(key) is null.
public String optString(String key, String defaultValue)
key
- The key to look up.defaultValue
- The default String value to return in the case of null values in the map.
public JSONObject getJSONObject(String key) throws JSONException
key
- The key to look up.
throws JSONException Thrown when the type returned by get(key) is not a JSONObject instance.
JSONException
public JSONObject optJSONObject(String key)
key
- The key to look up.
public JSONObject optJSONObject(String key, JSONObject defaultValue)
key
- The key to look up.defaultValue
- The default JSONObject to return in the case of null/non JSONObject values in the map.
public JSONArray getJSONArray(String key) throws JSONException
key
- The key to look up.
throws JSONException Thrown when the type returned by get(key) is not a Long instance, or cannot be converted to a long..
JSONException
public JSONArray optJSONArray(String key) throws JSONException
key
- The key to look up.
JSONException
public JSONArray optJSONArray(String key, JSONArray defaultValue) throws JSONException
key
- The key to look up.defaultValue
- The default value to return if the value in the map is null/not a JSONArray.
JSONException
public JSONObject putOnce(String key, Object value) throws JSONException
key
- The ket to place in the arrayvalue
- The value to place in the array
JSONException
- - Thrown if the key already exists or if key or value is nullpublic JSONObject putOpt(String key, Object value) throws JSONException
key
- The keey (attribute) name to assign to the value.value
- The value to put into the JSONObject.
JSONException
- - if the value is a non-finite numberpublic int length()
public JSONObject append(String key, Object value) throws JSONException
key
- The key/attribute name to append to.value
- The value to append to it.
JSONException
- Thrown if the value to append is not JSONAble.public JSONArray toJSONArray(JSONArray names) throws JSONException
names
- - A JSONArray containing the a list of key strings. This determines the sequence of values in the result.
JSONException
- - if errors occur during storing the values in a JSONArraypublic boolean has(String key)
key
- The key to test.
public boolean isNull(String key)
key
- The key to test for null.
public Iterator keys()
public JSONArray names()
public static String[] getNames(JSONObject obj)
public Iterator sortedKeys()
public String toString()
toString
in class AbstractMap
public String toString(boolean verbose)
verbose
- Whether or not to tab-indent the output.
public String toString(int indentDepth) throws JSONException
JSONException
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |