## org.eclipse.jetty.util.ajax
Class JSON

    java.lang.Object
      org.eclipse.jetty.util.ajax.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 convertors for objects that may be registered with `registerConvertor(Class, org.eclipse.jetty.util.ajax.JSON.Convertor)` . These convertors are looked up by class, interface and super class by `getConvertor(Class)`.

Nested Class Summary
`static interface` `JSON.Convertible` JSON Convertible object.
`static interface` `JSON.Convertor` Static JSON Convertor.
`static interface` `JSON.Generator` JSON Generator.
`static class` `JSON.Literal` A Literal JSON generator A utility instance of `JSON.Generator` that holds a pre-generated string on JSON text.
`static interface` `JSON.Output` JSON Output class for use by `JSON.Convertible`.
`static class` `JSON.ReaderSource`
`static interface` `JSON.Source`
`static class` `JSON.StringSource`

Field Summary
`static JSON` `DEFAULT`

Constructor Summary
`JSON()`

Method Summary
`void` `addConvertor(java.lang.Class forClass, JSON.Convertor convertor)` Register a `JSON.Convertor` for a class or interface.
`void` `addConvertorFor(java.lang.String name, JSON.Convertor convertor)` Register a `JSON.Convertor` for a named class or interface.
`void` `append(java.lang.Appendable buffer, java.lang.Object object)` Append object as JSON to string buffer.
`void` `append(java.lang.StringBuffer buffer, java.lang.Object object)` Deprecated.
`void` `appendArray(java.lang.Appendable buffer, java.util.Collection collection)`
`void` `appendArray(java.lang.Appendable buffer, java.lang.Object array)`
`void` `appendArray(java.lang.StringBuffer buffer, java.util.Collection collection)` Deprecated.
`void` `appendArray(java.lang.StringBuffer buffer, java.lang.Object array)` Deprecated.
`void` `appendBoolean(java.lang.Appendable buffer, java.lang.Boolean b)`
`void` `appendBoolean(java.lang.StringBuffer buffer, java.lang.Boolean b)` Deprecated.
`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` `appendJSON(java.lang.StringBuffer buffer, JSON.Convertible converter)` Deprecated.
`void` `appendJSON(java.lang.StringBuffer buffer, JSON.Convertor convertor, java.lang.Object object)` Deprecated.
`void` `appendJSON(java.lang.StringBuffer buffer, JSON.Generator generator)` Deprecated.
`void` `appendMap(java.lang.Appendable buffer, java.util.Map<?,?> map)`
`void` `appendMap(java.lang.StringBuffer buffer, java.util.Map<?,?> map)` Deprecated.
`void` `appendNull(java.lang.Appendable buffer)`
`void` `appendNull(java.lang.StringBuffer buffer)` Deprecated.
`void` `appendNumber(java.lang.Appendable buffer, java.lang.Number number)`
`void` `appendNumber(java.lang.StringBuffer buffer, java.lang.Number number)` Deprecated.
`void` `appendString(java.lang.Appendable buffer, java.lang.String string)`
`void` `appendString(java.lang.StringBuffer buffer, java.lang.String string)` Deprecated.
`protected static void` `complete(java.lang.String seek, JSON.Source source)`
`protected JSON` `contextFor(java.lang.String field)`
`protected JSON` `contextForArray()`
`protected java.lang.Object` `convertTo(java.lang.Class type, java.util.Map map)`
`java.lang.Object` `fromJSON(java.lang.String json)` Convert JSON to Object
`protected JSON.Convertor` `getConvertor(java.lang.Class forClass)` Lookup a convertor for a class.
`JSON.Convertor` `getConvertorFor(java.lang.String name)` Lookup a convertor for a named class.
`static JSON` `getDefault()`
`int` `getStringBufferSize()`
`protected java.lang.Object` `handleUnknown(JSON.Source source, char c)`
`protected java.lang.Object[]` `newArray(int size)`
`protected java.util.Map<java.lang.String,java.lang.Object>` `newMap()`
`static java.lang.Object` `parse(java.io.InputStream in)` Deprecated. use `parse(Reader)`
`static java.lang.Object` `parse(java.io.InputStream in, boolean stripOuterComment)` Deprecated. use `parse(Reader, boolean)`
`java.lang.Object` `parse(JSON.Source source)`
`java.lang.Object` `parse(JSON.Source source, boolean stripOuterComment)`
`static java.lang.Object` `parse(java.io.Reader in)`
`static java.lang.Object` `parse(java.io.Reader in, boolean stripOuterComment)`
`static java.lang.Object` `parse(java.lang.String s)`
`static java.lang.Object` `parse(java.lang.String s, boolean stripOuterComment)`
`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)`
`static void` `registerConvertor(java.lang.Class forClass, JSON.Convertor convertor)` Register a `JSON.Convertor` for a class or interface.
`protected void` `seekTo(char seek, JSON.Source source)`
`protected char` `seekTo(java.lang.String seek, JSON.Source source)`
`static void` `setDefault(JSON json)` Deprecated.
`void` `setStringBufferSize(int stringBufferSize)`
`java.lang.String` `toJSON(java.lang.Object object)` Convert Object to JSON
`protected java.lang.String` `toString(char[] buffer, int offset, int length)`
`static java.lang.String` `toString(java.util.Map object)`
`static java.lang.String` `toString(java.lang.Object object)`
`static java.lang.String` `toString(java.lang.Object[] array)`

Methods inherited from class java.lang.Object
`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

Field Detail

### DEFAULT

    public static final JSON DEFAULT

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)

### 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`

### 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`

### parse

    @Deprecated
    public static java.lang.Object parse(java.io.InputStream in)
                                  throws java.io.IOException

  Deprecated. use `parse(Reader)`

  Parameters:
    `in` - Reader containing JSON object or array.
  Returns:
    A Map, Object array or primitive array parsed from the JSON.
  Throws:
    `java.io.IOException`

### parse

    @Deprecated
    public static java.lang.Object parse(java.io.InputStream in,
                                                    boolean stripOuterComment)
                                  throws java.io.IOException

  Deprecated. use `parse(Reader, boolean)`

  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`

### 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

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

### 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

### 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)

Copyright © 1995-2010 Mort Bay Consulting. All Rights Reserved.