Class JSONObject

java.lang.Object
java.util.AbstractMap<K,V>
java.util.HashMap
com.ibm.json.java.JSONObject
All Implemented Interfaces:
JSONArtifact, Serializable, Cloneable, Map
Direct Known Subclasses:
OrderedJSONObject

public class JSONObject extends HashMap 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:
  • Constructor Details

    • JSONObject

      public JSONObject()
      Create a new instance of this class.
  • Method Details

    • isValidObject

      public static boolean isValidObject(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

      public static boolean isValidType(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

      public static JSONObject parse(Reader reader) throws 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,
      IOException
    • parse

      public static JSONObject parse(String str) throws 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,
      IOException
    • parse

      public static JSONObject parse(InputStream is) throws 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,
      IOException
    • serialize

      public void serialize(OutputStream os) throws 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:
      IOException - Thrown on IO errors during serialization.
    • serialize

      public void serialize(OutputStream os, boolean verbose) throws 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:
      IOException - Thrown on IO errors during serialization.
    • serialize

      public void serialize(Writer writer) throws 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:
      IOException - Thrown on IO errors during serialization.
    • serialize

      public void serialize(Writer writer, boolean verbose) throws 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:
      IOException - Thrown on IO errors during serialization.
    • serialize

      public String serialize(boolean verbose) throws 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:
      IOException - Thrown on IO errors during serialization.
    • serialize

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

      public Object put(Object key, Object value)
      (non-Javadoc)
      Specified by:
      put in interface Map
      Overrides:
      put in class HashMap
      See Also:
    • toString

      public 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 AbstractMap