Class JSON


  • public class JSON
    extends java.lang.Object
    JSON Parser and Generator.

    This class provides some static methods to convert POJOs to and from JSON notation. The mapping from JSON to java is:

       object --> Map
       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 toString(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 static converters for objects that may be registered with registerConvertor(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).

    • Field Detail

      • DEFAULT

        public static final JSON DEFAULT
    • Constructor Detail

      • JSON

        public JSON()
    • Method Detail

      • reset

        public static void reset()
        Reset the default JSON behaviors to default
      • 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)
      • registerConvertor

        public static void registerConvertor​(java.lang.Class forClass,
                                             JSON.Convertor convertor)
        Register a JSON.Convertor for a class or interface.
        Parameters:
        forClass - The class or interface that the convertor applies to
        convertor - the convertor
      • getDefault

        public static JSON getDefault()
      • setDefault

        @Deprecated
        public static void setDefault​(JSON json)
        Deprecated.
      • toString

        public static java.lang.String toString​(java.lang.Object object)
      • toString

        public static java.lang.String toString​(java.util.Map object)
      • toString

        public static java.lang.String toString​(java.lang.Object[] array)
      • parse

        public static java.lang.Object parse​(java.lang.String s)
        Parameters:
        s - String containing JSON object or array.
        Returns:
        A Map, Object array or primitive array parsed from the JSON.
      • parse

        public static java.lang.Object parse​(java.lang.String s,
                                             boolean stripOuterComment)
        Parameters:
        s - String containing JSON object or array.
        stripOuterComment - If true, an outer comment around the JSON is ignored.
        Returns:
        A Map, Object array or primitive array parsed from the JSON.
      • parse

        public static java.lang.Object parse​(java.io.Reader in)
                                      throws java.io.IOException
        Parameters:
        in - Reader containing JSON object or array.
        Returns:
        A Map, Object array or primitive array parsed from the JSON.
        Throws:
        java.io.IOException - if unable to parse
      • parse

        public static java.lang.Object parse​(java.io.Reader in,
                                             boolean stripOuterComment)
                                      throws java.io.IOException
        Parameters:
        in - Reader containing JSON object or array.
        stripOuterComment - If true, an outer comment around the JSON is ignored.
        Returns:
        A Map, Object array or primitive array parsed from the JSON.
        Throws:
        java.io.IOException - if unable to parse
      • parse

        @Deprecated
        public static java.lang.Object parse​(java.io.InputStream in)
                                      throws java.io.IOException
        Deprecated.
        Parameters:
        in - Reader containing JSON object or array.
        Returns:
        A Map, Object array or primitive array parsed from the JSON.
        Throws:
        java.io.IOException - if unable to parse
      • parse

        @Deprecated
        public static java.lang.Object parse​(java.io.InputStream in,
                                             boolean stripOuterComment)
                                      throws java.io.IOException
        Deprecated.
        Parameters:
        in - Stream containing JSON object or array.
        stripOuterComment - If true, an outer comment around the JSON is ignored.
        Returns:
        A Map, Object array or primitive array parsed from the JSON.
        Throws:
        java.io.IOException - if unable to parse
      • escapeString

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

        protected void escapeUnicode​(java.lang.Appendable buffer,
                                     char c)
                              throws java.io.IOException
        Per spec, unicode characters are by default NOT escaped. This overridable allows for alternate behavior to escape those with your choice of encoding.
         protected void escapeUnicode(Appendable buffer, char c) throws IOException
         {
             // Unicode is slash-u escaped
             buffer.append(String.format("\\u%04x", (int)c));
         }
         
        Throws:
        java.io.IOException
      • toJSON

        public java.lang.String toJSON​(java.lang.Object object)
        Convert Object to JSON
        Parameters:
        object - The object to convert
        Returns:
        The JSON String
      • fromJSON

        public java.lang.Object fromJSON​(java.lang.String json)
        Convert JSON to Object
        Parameters:
        json - The json to convert
        Returns:
        The object
      • append

        @Deprecated
        public void append​(java.lang.StringBuffer buffer,
                           java.lang.Object object)
        Deprecated.
      • append

        public void append​(java.lang.Appendable buffer,
                           java.lang.Object object)
        Append object as JSON to string buffer.
        Parameters:
        buffer - the buffer to append to
        object - the object to append
      • appendNull

        @Deprecated
        public void appendNull​(java.lang.StringBuffer buffer)
        Deprecated.
      • appendNull

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

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

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

        @Deprecated
        public void appendJSON​(java.lang.StringBuffer buffer,
                               JSON.Convertible converter)
        Deprecated.
      • appendJSON

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

        @Deprecated
        public void appendJSON​(java.lang.StringBuffer buffer,
                               JSON.Generator generator)
        Deprecated.
      • appendJSON

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

        @Deprecated
        public void appendMap​(java.lang.StringBuffer buffer,
                              java.util.Map<?,​?> map)
        Deprecated.
      • appendMap

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

        @Deprecated
        public void appendArray​(java.lang.StringBuffer buffer,
                                java.util.Collection collection)
        Deprecated.
      • appendArray

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

        @Deprecated
        public void appendArray​(java.lang.StringBuffer buffer,
                                java.lang.Object array)
        Deprecated.
      • appendArray

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

        @Deprecated
        public void appendBoolean​(java.lang.StringBuffer buffer,
                                  java.lang.Boolean b)
        Deprecated.
      • appendBoolean

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

        @Deprecated
        public void appendNumber​(java.lang.StringBuffer buffer,
                                 java.lang.Number number)
        Deprecated.
      • appendNumber

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

        @Deprecated
        public void appendString​(java.lang.StringBuffer buffer,
                                 java.lang.String string)
        Deprecated.
      • appendString

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

        protected java.lang.String toString​(char[] buffer,
                                            int offset,
                                            int length)
      • newMap

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

        @Deprecated
        protected java.lang.Object[] newArray​(int size)
        Deprecated.
        Parameters:
        size - the size of the array
        Returns:
        a new array
      • contextForArray

        protected JSON contextForArray()
      • contextFor

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

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

        public void addConvertor​(java.lang.Class forClass,
                                 JSON.Convertor convertor)
        Register a JSON.Convertor for a class or interface.
        Parameters:
        forClass - The class or interface that the convertor applies to
        convertor - the convertor
      • getConvertor

        protected JSON.Convertor getConvertor​(java.lang.Class forClass)
        Lookup 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 it's interfaces are tried recursively.

        Parameters:
        forClass - The class
        Returns:
        a JSON.Convertor or null if none were found.
      • addConvertorFor

        public void addConvertorFor​(java.lang.String name,
                                    JSON.Convertor convertor)
        Register a JSON.Convertor for a named class or interface.
        Parameters:
        name - name of a class or an interface that the convertor applies to
        convertor - the convertor
      • removeConvertorFor

        public JSON.Convertor removeConvertorFor​(java.lang.String name)
        Removes a registered JSON.Convertor for the given named class or interface.
        Parameters:
        name - name of a class or an interface for a registered JSON.Convertor
        Returns:
        the JSON.Convertor that was removed, or null
      • getArrayConverter

        public java.util.function.Function<java.util.List<?>,​java.lang.Object> getArrayConverter()
        Returns:
        the function to customize the Java representation of JSON arrays
        See Also:
        setArrayConverter(Function)
      • setArrayConverter

        public void setArrayConverter​(java.util.function.Function<java.util.List<?>,​java.lang.Object> arrayConverter)

        Sets the function to convert JSON arrays from their default Java representation, a List<Object>, to another Java data structure such as an Object[].

        Parameters:
        arrayConverter - the function to customize the Java representation of JSON arrays
        See Also:
        getArrayConverter()
      • getConvertorFor

        public JSON.Convertor getConvertorFor​(java.lang.String name)
        Lookup a convertor for a named class.
        Parameters:
        name - name of the class
        Returns:
        a JSON.Convertor or null if none were found.
      • parse

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

        public java.lang.Object parse​(JSON.Source source)
      • 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)