Class JSON


  • public class JSON
    extends java.lang.Object

    JSON parser and generator.

    This class provides methods to convert POJOs to and from JSON notation.

    The mapping from JSON to Java is:

       object --> Map<String, Object>
       array  --> Object[]
       number --> Double or Long
       string --> String
       null   --> null
       bool   --> Boolean
     

    The Java to JSON mapping is:

       String --> string
       Number --> number
       Map    --> object
       List   --> array
       Array  --> array
       null   --> null
       Boolean--> boolean
       Object --> string (dubious!)
     

    The interface JSON.Convertible may be implemented by classes that wish to externalize and initialize specific fields to and from JSON objects. Only directed acyclic graphs of objects are supported.

    The interface JSON.Generator may be implemented by classes that know how to render themselves as JSON and the toJSON(Object) method will use JSON.Generator.addJSON(Appendable) to generate the JSON.

    The class JSON.Literal may be used to hold pre-generated JSON object.

    The interface JSON.Convertor may be implemented to provide converters for objects that may be registered with addConvertor(Class, Convertor). These converters are looked up by class, interface and super class by getConvertor(Class).

    If a JSON object has a class field, then a Java class for that name is loaded and the method convertTo(Class, Map) is used to find a JSON.Convertor for that class.

    If a JSON object has a x-class field then a direct lookup for a JSON.Convertor for that class name is done (without loading the class).

    • Constructor Summary

      Constructors 
      Constructor Description
      JSON()  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addConvertor​(java.lang.Class<?> forClass, JSON.Convertor convertor)
      Registers a JSON.Convertor for the given class.
      void addConvertorFor​(java.lang.String name, JSON.Convertor convertor)
      Registers a JSON.Convertor for a named class.
      void append​(java.lang.Appendable buffer, java.lang.Object object)
      Appends the given object as JSON to string buffer.
      void appendArray​(java.lang.Appendable buffer, java.lang.Object array)  
      void appendArray​(java.lang.Appendable buffer, java.util.Collection<?> collection)  
      void appendBoolean​(java.lang.Appendable buffer, java.lang.Boolean b)  
      void appendJSON​(java.lang.Appendable buffer, JSON.Convertible converter)  
      void appendJSON​(java.lang.Appendable buffer, JSON.Convertor convertor, java.lang.Object object)  
      void appendJSON​(java.lang.Appendable buffer, JSON.Generator generator)  
      void appendMap​(java.lang.Appendable buffer, java.util.Map<?,​?> map)  
      void appendNull​(java.lang.Appendable buffer)  
      void appendNumber​(java.lang.Appendable buffer, java.lang.Number number)  
      void appendString​(java.lang.Appendable buffer, java.lang.String string)  
      protected static void complete​(java.lang.String seek, JSON.Source source)  
      protected JSON contextFor​(java.lang.String field)
      Every time a JSON object field representation {"name": value} is parsed, this method is called to (possibly) return a different JSON instance (for example configured with different converters) to parse the object field.
      protected JSON contextForArray()
      Every time a JSON array representation [...] is parsed, this method is called to (possibly) return a different JSON instance (for example configured with different converters) to parse the array items.
      protected java.lang.Object convertTo​(java.lang.Class<?> type, java.util.Map<java.lang.String,​java.lang.Object> map)  
      void escapeString​(java.lang.Appendable buffer, java.lang.String input)
      Escapes the characters of the given input string into the given buffer.
      protected void escapeUnicode​(java.lang.Appendable buffer, char c)
      Per JSON specification, unicode characters are by default NOT escaped.
      java.lang.Object fromJSON​(java.io.Reader reader)
      Parses the JSON from the given Reader into an object.
      java.lang.Object fromJSON​(java.lang.String string)
      Parses the given JSON string into an object.
      protected JSON.Convertor getConvertor​(java.lang.Class<?> forClass)
      Looks up a convertor for a class.
      JSON.Convertor getConvertorFor​(java.lang.String name)
      Looks up a convertor for a class name.
      int getStringBufferSize()  
      protected java.lang.Object handleUnknown​(JSON.Source source, char c)  
      protected java.lang.Object[] newArray​(int size)
      Factory method that creates an array when a JSON representation of [...] is parsed.
      protected java.util.Map<java.lang.String,​java.lang.Object> newMap()
      Factory method that creates a Map when a JSON representation of {...} is parsed.
      java.lang.Object parse​(JSON.Source source)
      Parses the given JSON source into an object.
      java.lang.Object parse​(JSON.Source source, boolean stripOuterComment)
      Parses the given JSON source into an object.
      protected java.lang.Object parseArray​(JSON.Source source)  
      java.lang.Number parseNumber​(JSON.Source source)  
      protected java.lang.Object parseObject​(JSON.Source source)  
      protected java.lang.String parseString​(JSON.Source source)  
      JSON.Convertor removeConvertor​(java.lang.Class<?> forClass)
      Unregisters a JSON.Convertor for a class.
      JSON.Convertor removeConvertorFor​(java.lang.String name)
      Unregisters a JSON.Convertor for a named class.
      protected void seekTo​(char seek, JSON.Source source)  
      protected char seekTo​(java.lang.String seek, JSON.Source source)  
      void setStringBufferSize​(int stringBufferSize)  
      java.lang.String toJSON​(java.lang.Object object)
      Converts any object to JSON.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • JSON

        public JSON()
    • Method Detail

      • getStringBufferSize

        public int getStringBufferSize()
        Returns:
        the initial stringBuffer size to use when creating JSON strings (default 1024)
      • setStringBufferSize

        public void setStringBufferSize​(int stringBufferSize)
        Parameters:
        stringBufferSize - the initial stringBuffer size to use when creating JSON strings (default 1024)
      • escapeString

        public void escapeString​(java.lang.Appendable buffer,
                                 java.lang.String input)
                          throws java.io.IOException

        Escapes the characters of the given input string into the given buffer.

        Parameters:
        buffer - the buffer to escape the string into
        input - the string to escape
        Throws:
        java.io.IOException - if appending to the buffer fails
        See Also:
        escapeUnicode(Appendable, char)
      • escapeUnicode

        protected void escapeUnicode​(java.lang.Appendable buffer,
                                     char c)
                              throws java.io.IOException

        Per JSON specification, unicode characters are by default NOT escaped.

        Overriding this method allows for alternate behavior to escape those with your choice of encoding.

         protected void escapeUnicode(Appendable buffer, char c) throws IOException
         {
             // Unicode is backslash-u escaped
             buffer.append(String.format("\\u%04x", (int)c));
         }
         
        Throws:
        java.io.IOException
      • toJSON

        public java.lang.String toJSON​(java.lang.Object object)

        Converts any object to JSON.

        Parameters:
        object - the object to convert
        Returns:
        the JSON string representation of the object
        See Also:
        append(Appendable, Object)
      • append

        public void append​(java.lang.Appendable buffer,
                           java.lang.Object object)

        Appends the given object as JSON to string buffer.

        This method tests the given object type and calls other appends methods for each object type, see for example appendMap(Appendable, Map).

        Parameters:
        buffer - the buffer to append to
        object - the object to convert to JSON
      • appendNull

        public void appendNull​(java.lang.Appendable buffer)
      • appendJSON

        public void appendJSON​(java.lang.Appendable buffer,
                               JSON.Convertor convertor,
                               java.lang.Object object)
      • appendJSON

        public void appendJSON​(java.lang.Appendable buffer,
                               JSON.Convertible converter)
      • appendJSON

        public void appendJSON​(java.lang.Appendable buffer,
                               JSON.Generator generator)
      • appendMap

        public void appendMap​(java.lang.Appendable buffer,
                              java.util.Map<?,​?> map)
      • appendArray

        public void appendArray​(java.lang.Appendable buffer,
                                java.util.Collection<?> collection)
      • appendArray

        public void appendArray​(java.lang.Appendable buffer,
                                java.lang.Object array)
      • appendBoolean

        public void appendBoolean​(java.lang.Appendable buffer,
                                  java.lang.Boolean b)
      • appendNumber

        public void appendNumber​(java.lang.Appendable buffer,
                                 java.lang.Number number)
      • appendString

        public void appendString​(java.lang.Appendable buffer,
                                 java.lang.String string)
      • newMap

        protected java.util.Map<java.lang.String,​java.lang.Object> newMap()

        Factory method that creates a Map when a JSON representation of {...} is parsed.

        Returns:
        a new Map representing the JSON object
      • newArray

        protected java.lang.Object[] newArray​(int size)

        Factory method that creates an array when a JSON representation of [...] is parsed.

        Parameters:
        size - the size of the array
        Returns:
        a new array representing the JSON array
      • contextForArray

        protected JSON contextForArray()

        Every time a JSON array representation [...] is parsed, this method is called to (possibly) return a different JSON instance (for example configured with different converters) to parse the array items.

        Returns:
        a JSON instance to parse array items
      • contextFor

        protected JSON contextFor​(java.lang.String field)

        Every time a JSON object field representation {"name": value} is parsed, this method is called to (possibly) return a different JSON instance (for example configured with different converters) to parse the object field.

        Parameters:
        field - the field name
        Returns:
        a JSON instance to parse the object field
      • convertTo

        protected java.lang.Object convertTo​(java.lang.Class<?> type,
                                             java.util.Map<java.lang.String,​java.lang.Object> map)
      • addConvertor

        public void addConvertor​(java.lang.Class<?> forClass,
                                 JSON.Convertor convertor)

        Registers a JSON.Convertor for the given class.

        Parameters:
        forClass - the class the convertor applies to
        convertor - the convertor for the class
      • addConvertorFor

        public void addConvertorFor​(java.lang.String name,
                                    JSON.Convertor convertor)

        Registers a JSON.Convertor for a named class.

        Parameters:
        name - the name of the class the convertor applies to
        convertor - the convertor for the class
      • removeConvertor

        public JSON.Convertor removeConvertor​(java.lang.Class<?> forClass)

        Unregisters a JSON.Convertor for a class.

        Parameters:
        forClass - the class the convertor applies to
        Returns:
        the convertor for the class
      • removeConvertorFor

        public JSON.Convertor removeConvertorFor​(java.lang.String name)

        Unregisters a JSON.Convertor for a named class.

        Parameters:
        name - the name of the class the convertor applies to
        Returns:
        the convertor for the class
      • getConvertor

        protected JSON.Convertor getConvertor​(java.lang.Class<?> forClass)

        Looks up a convertor for a class.

        If no match is found for the class, then the interfaces for the class are tried. If still no match is found, then the super class and its interfaces are tried iteratively.

        Parameters:
        forClass - the class to look up the convertor
        Returns:
        a JSON.Convertor or null if none was found for the class
      • getConvertorFor

        public JSON.Convertor getConvertorFor​(java.lang.String name)

        Looks up a convertor for a class name.

        Parameters:
        name - name of the class to look up the convertor
        Returns:
        a JSON.Convertor or null if none were found.
      • parse

        public java.lang.Object parse​(JSON.Source source,
                                      boolean stripOuterComment)

        Parses the given JSON source into an object.

        Although the JSON specification does not allow comments (of any kind) this method optionally strips out outer comments of this form:

         // An outer comment line.
         /* Another outer comment, multiline.
         // Yet another comment line.
         {
             "name": "the real JSON"
         }
         */ End of outer comment, multiline.
         
        Parameters:
        source - the JSON source to parse
        stripOuterComment - whether to strip outer comments
        Returns:
        the object constructed from the JSON string representation
      • fromJSON

        public java.lang.Object fromJSON​(java.lang.String string)

        Parses the given JSON string into an object.

        Parameters:
        string - the JSON string to parse
        Returns:
        the object constructed from the JSON string representation
      • fromJSON

        public java.lang.Object fromJSON​(java.io.Reader reader)

        Parses the JSON from the given Reader into an object.

        Parameters:
        reader - the Reader to read the JSON from
        Returns:
        the object constructed from the JSON string representation
      • parse

        public java.lang.Object parse​(JSON.Source source)

        Parses the given JSON source into an object.

        Although the JSON specification does not allow comments (of any kind) this method strips out initial comments of this form:

         // An initial comment line.
         /* An initial
            multiline comment */
         {
             "name": "foo"
         }
         

        This method detects the object type and calls other parse methods for each object type, see for example parseArray(Source).

        Parameters:
        source - the JSON source to parse
        Returns:
        the object constructed from the JSON string representation
      • handleUnknown

        protected java.lang.Object handleUnknown​(JSON.Source source,
                                                 char c)
      • parseObject

        protected java.lang.Object parseObject​(JSON.Source source)
      • parseArray

        protected java.lang.Object parseArray​(JSON.Source source)
      • parseString

        protected java.lang.String parseString​(JSON.Source source)
      • parseNumber

        public java.lang.Number parseNumber​(JSON.Source source)
      • seekTo

        protected void seekTo​(char seek,
                              JSON.Source source)
      • seekTo

        protected char seekTo​(java.lang.String seek,
                              JSON.Source source)
      • complete

        protected static void complete​(java.lang.String seek,
                                       JSON.Source source)