com.ibm.json.java

Class JSONObject

  1. java.lang.Object
  2. extended byjava.util.AbstractMap<K,V>
  3. extended byjava.util.HashMap
  4. extended bycom.ibm.json.java.JSONObject
All implemented interfaces:
JSONArtifact, java.io.Serializable, java.lang.Cloneable, java.util.Map
Direct known subclasses:
JSONMessage, JSONRequest, JSONResponse, JSONResultSetColumn, JSONResultSetRow, OrderedJSONObject

  1. public class JSONObject
  2. extends java.util.HashMap
  3. implements JSONArtifact
Models a JSON Object. Extension of HashMap that only allows String keys, and values which are JSON-able.

JSON-able values are: null, and instances of String, Boolean, Number, JSONObject and JSONArray.

Instances of this class are not thread-safe.
See Also:
Serialized Form

Nested Class Summary

Nested classes/interfaces inherited from class java.util.AbstractMap
java.util.AbstractMap.SimpleEntry<K,V>, java.util.AbstractMap.SimpleImmutableEntry<K,V>

Constructor Summary

Constructor and Description
JSONObject()
Create a new instance of this class.

Method Summary

Modifier and Type Method and Description
  1. static
  2. boolean
isValidObject(java.lang.Object object)
Return whether the object is a valid value for a property.
  1. static
  2. boolean
isValidType(java.lang.Class clazz)
Return whether the class is a valid type of value for a property.
  1. static
  2. JSONObject
parse(java.io.InputStream is)
Convert a stream of JSON text into object form.
  1. static
  2. JSONObject
parse(java.io.Reader reader)
Convert a stream (in reader form) of JSON text into object form.
  1. static
  2. JSONObject
parse(java.lang.String str)
Convert a String of JSON text into object form.
  1. java.lang.Object
put(java.lang.Object key,java.lang.Object value)
(non-Javadoc)
  1. java.lang.String
serialize()
Convert this object into a String of JSON text.
  1. java.lang.String
serialize(boolean verbose)
Convert this object into a String of JSON text, specifying verbosity.
  1. void
serialize(java.io.OutputStream os)
Convert this object into a stream of JSON text.
  1. void
serialize(java.io.OutputStream os,boolean verbose)
Convert this object into a stream of JSON text.
  1. void
serialize(java.io.Writer writer)
Convert this object into a stream of JSON text.
  1. void
serialize(java.io.Writer writer,boolean verbose)
Convert this object into a stream of JSON text, specifying verbosity.
  1. java.lang.String
toString()
Over-ridden toString() method.
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

Constructor Detail

JSONObject

  1. public JSONObject()
Create a new instance of this class.

Method Detail

isValidObject

  1. public static boolean isValidObject( java.lang.Object object)
Return whether the object is a valid value for a property.
Parameters:
object - The object to check for validity as a JSON property value.

isValidType

  1. public static boolean isValidType( java.lang.Class clazz)
Return whether the class is a valid type of value for a property.
Parameters:
clazz - The class type to check for validity as a JSON object type.

parse

  1. public static JSONObject parse( java.io.Reader reader)
  2. throws java.io.IOException
Convert a stream (in reader form) of JSON text into object form.
Parameters:
reader - The reader from which the JSON data is read.
Returns:
The contructed JSON Object.
Throws:
IOEXception - Thrown if an underlying IO error from the reader occurs, or if malformed JSON is read,
java.io.IOException

parse

  1. public static JSONObject parse( java.lang.String str)
  2. throws java.io.IOException
Convert a String of JSON text into object form.
Parameters:
str - The JSON string to parse into a Java Object.
Returns:
The contructed JSON Object.
Throws:
IOEXception - Thrown if malformed JSON is read,
java.io.IOException

parse

  1. public static JSONObject parse( java.io.InputStream is)
  2. throws java.io.IOException
Convert a stream of JSON text into object form.
Parameters:
is - The inputStream from which to read the JSON. It will assume the input stream is in UTF-8 and read it as such.
Returns:
The contructed JSON Object.
Throws:
IOEXception - Thrown if an underlying IO error from the stream occurs, or if malformed JSON is read,
java.io.IOException

serialize

  1. public void serialize(java.io.OutputStream os)
  2. throws java.io.IOException
Convert this object into a stream of JSON text. Same as calling serialize(os,false); Note that encoding is always written as UTF-8, as per JSON spec.
Specified by:
serialize in interface JSONArtifact
Parameters:
os - The output stream to serialize data to.
Throws:
java.io.IOException - Thrown on IO errors during serialization.

serialize

  1. public void serialize(java.io.OutputStream os,
  2. boolean verbose)
  3. throws java.io.IOException
Convert this object into a stream of JSON text. Same as calling serialize(writer,false); Note that encoding is always written as UTF-8, as per JSON spec.
Specified by:
serialize in interface JSONArtifact
Parameters:
os - The output stream to serialize data to.
verbose - Whether or not to write the JSON text in a verbose format.
Throws:
java.io.IOException - Thrown on IO errors during serialization.

serialize

  1. public void serialize(java.io.Writer writer)
  2. throws java.io.IOException
Convert this object into a stream of JSON text. Same as calling serialize(writer,false);
Specified by:
serialize in interface JSONArtifact
Parameters:
writer - The writer which to serialize the JSON text to.
Throws:
java.io.IOException - Thrown on IO errors during serialization.

serialize

  1. public void serialize(java.io.Writer writer,
  2. boolean verbose)
  3. throws java.io.IOException
Convert this object into a stream of JSON text, specifying verbosity.
Specified by:
serialize in interface JSONArtifact
Parameters:
writer - The writer which to serialize the JSON text to.
Throws:
java.io.IOException - Thrown on IO errors during serialization.

serialize

  1. public java.lang.String serialize( boolean verbose)
  2. throws java.io.IOException
Convert this object into a String of JSON text, specifying verbosity.
Specified by:
serialize in interface JSONArtifact
Parameters:
verbose - Whether or not to serialize in compressed for formatted Strings.
Throws:
java.io.IOException - Thrown on IO errors during serialization.

serialize

  1. public java.lang.String serialize( )
  2. throws java.io.IOException
Convert this object into a String of JSON text. Same as serialize(false);
Specified by:
serialize in interface JSONArtifact
Throws:
java.io.IOException - Thrown on IO errors during serialization.

put

  1. public java.lang.Object put(java.lang.Object key,
  2. java.lang.Object value)
(non-Javadoc)
Specified by:
put in interface java.util.Map
Overrides:
put in class java.util.HashMap
See Also:
HashMap.put(java.lang.Object, java.lang.Object)

toString

  1. public java.lang.String toString( )
Over-ridden toString() method. Returns the same value as serialize(), which is a compact JSON String. If an error occurs in the serialization, the return will be of format: JSON Generation Error: []
Overrides:
toString in class java.util.AbstractMap