Overview | Package | Class | Use | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

## org.mortbay.util.ajax
Class JSON

    java.lang.Object
      org.mortbay.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(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(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.StringBuffer buffer, java.lang.Object object)` Append object as JSON to string buffer.
`void` | `appendArray(java.lang.StringBuffer buffer, java.util.Collection collection)`
`void` | `appendArray(java.lang.StringBuffer buffer, java.lang.Object array)`
`void` | `appendBoolean(java.lang.StringBuffer buffer, java.lang.Boolean b)`
`void` | `appendJSON(java.lang.StringBuffer buffer, JSON.Convertible converter)`
`void` | `appendJSON(java.lang.StringBuffer buffer, JSON.Convertor convertor, java.lang.Object object)`
`void` | `appendJSON(java.lang.StringBuffer buffer, JSON.Generator generator)`
`void` | `appendMap(java.lang.StringBuffer buffer, java.util.Map object)`
`void` | `appendNull(java.lang.StringBuffer buffer)`
`void` | `appendNumber(java.lang.StringBuffer buffer, java.lang.Number number)`
`void` | `appendString(java.lang.StringBuffer buffer, java.lang.String string)`
`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` | `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)`
`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`

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

    public static void setDefault(JSON json)

### 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:
    `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:
    `java.io.IOException`

### parse

    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

    public static java.lang.Object parse(java.io.InputStream in,
                                         boolean stripOuterComment)
                                  throws java.io.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:
    `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

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

  Append object as JSON to string buffer.

  Parameters:
    `buffer` -
    `object` -

### appendNull

    public void appendNull(java.lang.StringBuffer buffer)

### appendJSON

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

### appendJSON

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

### appendJSON

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

### appendMap

    public void appendMap(java.lang.StringBuffer buffer,
                          java.util.Map object)

### appendArray

    public void appendArray(java.lang.StringBuffer buffer,
                            java.util.Collection collection)

### appendArray

    public void appendArray(java.lang.StringBuffer buffer,
                            java.lang.Object array)

### appendBoolean

    public void appendBoolean(java.lang.StringBuffer buffer,
                              java.lang.Boolean b)

### appendNumber

    public void appendNumber(java.lang.StringBuffer buffer,
                             java.lang.Number number)

### appendString

    public void appendString(java.lang.StringBuffer buffer,
                             java.lang.String string)

### toString

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

### newMap

    protected java.util.Map 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)

Overview | Package | Class | Use | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

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