## org.mortbay.util.ajax
Class JSON

    java.lang.Object
      org.mortbay.util.ajax.JSON

    public class JSON

extends 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(StringBuffer)` to generate the JSON. The class `JSON.Literal` may be used to hold pre-gnerated JSON object.

The interface `JSON.Convertor` may be implemented to provide static convertors for objects that may be registered with `registerConvertor(Class, org.mortbay.util.ajax.JSON.Convertor)`. These convertors are looked up by class, interface and super class by `getConvertor(Class)`.

Author:
  gregw

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`

Constructor Summary
`JSON()`

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

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

### setStringBufferSize

    public void setStringBufferSize(int stringBufferSize)

  Parameters:
    `stringBufferSize` - the initial stringBuffer size to use when creating JSON strings (default 256)

### registerConvertor

    public static void registerConvertor(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

    public static void setDefault(JSON json)

### toString

    public static String toString(Object object)

### toString

    public static String toString(Map object)

### toString

    public static String toString(Object[] array)

### parse

    public static Object parse(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 Object parse(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 Object parse(Reader in)
                        throws IOException

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

### parse

    public static Object parse(Reader in,
                               boolean stripOuterComment)
                        throws IOException

  Parameters:
    `s` - 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:
    `IOException`

### parse

    public static Object parse(InputStream in)
                        throws 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:
    `IOException`

### parse

    public static Object parse(InputStream in,
                               boolean stripOuterComment)
                        throws IOException

  Deprecated. use `parse(Reader, boolean)`

  Parameters:
    `s` - 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:
    `IOException`

### toJSON

    public String toJSON(Object object)

  Convert Object to JSON

  Parameters:
    `object` - The object to convert
  Returns:
    The JSON String

### fromJSON

    public Object fromJSON(String json)

  Convert JSON to Object

  Parameters:
    `json` - The json to convert
  Returns:
    The object

### append

    public void append(StringBuffer buffer,
                       Object object)

  Append object as JSON to string buffer.

  Parameters:
    `buffer` -
    `object` -

### appendNull

    public void appendNull(StringBuffer buffer)

### appendJSON

    public void appendJSON(StringBuffer buffer,
                           JSON.Convertor convertor,
                           Object object)

### appendJSON

    public void appendJSON(StringBuffer buffer,
                           JSON.Convertible converter)

### appendJSON

    public void appendJSON(StringBuffer buffer,
                           JSON.Generator generator)

### appendMap

    public void appendMap(StringBuffer buffer,
                          Map object)

### appendArray

    public void appendArray(StringBuffer buffer,
                            Collection collection)

### appendArray

    public void appendArray(StringBuffer buffer,
                            Object array)

### appendBoolean

    public void appendBoolean(StringBuffer buffer,
                              Boolean b)

### appendNumber

    public void appendNumber(StringBuffer buffer,
                             Number number)

### appendString

    public void appendString(StringBuffer buffer,
                             String string)

### toString

    protected String toString(char[] buffer,
                              int offset,
                              int length)

### newMap

    protected Map newMap()

### newArray

    protected Object[] newArray(int size)

### contextForArray

    protected JSON contextForArray()

### contextFor

    protected JSON contextFor(String field)

### convertTo

    protected Object convertTo(Class type,
                               Map map)

### addConvertor

    public void addConvertor(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(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.

### parse

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

### parse

    public Object parse(JSON.Source source)

### parseObject

    protected Object parseObject(JSON.Source source)

### parseNumber

    public Number parseNumber(JSON.Source source)

### seekTo

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

### seekTo

    protected char seekTo(String seek,
                          JSON.Source source)

### complete

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

Copyright © 2008 Mort Bay Consulting. All Rights Reserved.