Top Description Inners Fields Constructors Methods
org.mortbay.util.ajax

public Class JSON

extends Object
Class Inheritance
Imports
java.io.Externalizable, .IOException, .InputStream, .Reader, java.lang.reflect.Array, .Constructor, java.util.ArrayList, .Collection, .HashMap, .Iterator, .Map, org.mortbay.log.Log, org.mortbay.util.IO, .Loader, .QuotedStringTokenizer, .TypeUtil

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 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 and Inner Type Summary

Modifier and TypeClass and Description
public static interface
JSON.Convertible

JSON Convertible object.

public static interface
JSON.Convertor

Static JSON Convertor.

public static interface
JSON.Generator

JSON Generator.

public static class
JSON.Literal

A Literal JSON generator A utility instance of JSON.Generator that holds a pre-generated string on JSON text.

public static interface
JSON.Output

JSON Output class for use by Convertible.

public static class
public static interface
public static class

Field Summary

Modifier and TypeField and Description
private static JSON
private Map<K, V>
private int

Constructor Summary

AccessConstructor and Description
public
JSON()

Method Summary

Modifier and TypeMethod and Description
public void
addConvertor(Class<T>
The class or interface that the convertor applies to
forClass
,
JSON.Convertor
the convertor
convertor
)

Register a Convertor for a class or interface.

public void
append(StringBuffer buffer, Object object)

Append object as JSON to string buffer.

public void
appendArray(StringBuffer buffer, Collection<E> collection)

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

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

public void
public void
appendMap(StringBuffer buffer, Map<K, V> object)

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

protected JSON
protected JSON
protected Object
convertTo(Class<T> type, Map<K, V> map)

public Object

Returns:

The object
fromJSON
(String
The json to convert
json
)

Convert JSON to Object

protected JSON.Convertor

Returns:

a Convertor or null if none were found.
getConvertor
(Class<T>
The class
forClass
)

Lookup a convertor for a class.

public static JSON
public int

Returns:

the initial stringBuffer size to use when creating JSON strings (default 256)
getStringBufferSize
()

protected Object[]
newArray(int size)

protected Map<K, V>
public static Object

Returns:

A Map, Object array or primitive array parsed from the JSON.
parse
(String
String containing JSON object or array.
s
)

public static Object

Returns:

A Map, Object array or primitive array parsed from the JSON.
parse
(String
String containing JSON object or array.
s
,
boolean
If true, an outer comment around the JSON is ignored.
stripOuterComment
)

public static Object

Returns:

A Map, Object array or primitive array parsed from the JSON.
parse
(Reader
Reader containing JSON object or array.
in
)

public static Object

Returns:

A Map, Object array or primitive array parsed from the JSON.
parse
(Reader
Stream containing JSON object or array.
in
,
boolean
If true, an outer comment around the JSON is ignored.
stripOuterComment
)

public static Object

Returns:

A Map, Object array or primitive array parsed from the JSON.
parse
(InputStream
Reader containing JSON object or array.
in
)

Deprecated use parse(Reader)
public static Object

Returns:

A Map, Object array or primitive array parsed from the JSON.
parse
(InputStream
Stream containing JSON object or array.
in
,
boolean
If true, an outer comment around the JSON is ignored.
stripOuterComment
)

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

public Object
parse(JSON.Source source)

private Object
public Number
protected Object
private String
public static void
registerConvertor(Class<T>
The class or interface that the convertor applies to
forClass
,
JSON.Convertor
the convertor
convertor
)

Register a Convertor for a class or interface.

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

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

public static void
public void
setStringBufferSize(int
the initial stringBuffer size to use when creating JSON strings (default 256)
stringBufferSize
)

public String

Returns:

The JSON String
toJSON
(Object
The object to convert
object
)

Convert Object to JSON

public static String
toString(Object object)

public static String
toString(Map<K, V> object)

public static String
toString(Object[] array)

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

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

__defaultback to summary
private static JSON __default
_convertorsback to summary
private Map<K, V> _convertors
_stringBufferSizeback to summary
private int _stringBufferSize

Constructor Detail

JSONback to summary
public JSON()

Method Detail

addConvertorback to summary
public void addConvertor(Class<T> forClass, JSON.Convertor convertor)

Register a Convertor for a class or interface.

Parameters
forClass:Class<T>

The class or interface that the convertor applies to

convertor:JSON.Convertor

the convertor

appendback to summary
public void append(StringBuffer buffer, Object object)

Append object as JSON to string buffer.

appendArrayback to summary
public void appendArray(StringBuffer buffer, Collection<E> collection)
appendArrayback to summary
public void appendArray(StringBuffer buffer, Object array)
appendBooleanback to summary
public void appendBoolean(StringBuffer buffer, Boolean b)
appendJSONback to summary
public void appendJSON(final StringBuffer buffer, final JSON.Convertor convertor, final Object object)
appendJSONback to summary
public void appendJSON(final StringBuffer buffer, JSON.Convertible converter)
appendJSONback to summary
public void appendJSON(StringBuffer buffer, JSON.Generator generator)
appendMapback to summary
public void appendMap(StringBuffer buffer, Map<K, V> object)
appendNullback to summary
public void appendNull(StringBuffer buffer)
appendNumberback to summary
public void appendNumber(StringBuffer buffer, Number number)
appendStringback to summary
public void appendString(StringBuffer buffer, String string)
completeback to summary
protected static void complete(String seek, JSON.Source source)
contextForback to summary
protected JSON contextFor(String field)
contextForArrayback to summary
protected JSON contextForArray()
convertToback to summary
protected Object convertTo(Class<T> type, Map<K, V> map)
fromJSONback to summary
public Object fromJSON(String json)

Convert JSON to Object

Parameters
json:String

The json to convert

Returns:Object

The object

getConvertorback to summary
protected JSON.Convertor getConvertor(Class<T> 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:Class<T>

The class

Returns:JSON.Convertor

a Convertor or null if none were found.

getDefaultback to summary
public static JSON getDefault()
getStringBufferSizeback to summary
public int getStringBufferSize()
Returns:int

the initial stringBuffer size to use when creating JSON strings (default 256)

newArrayback to summary
protected Object[] newArray(int size)
newMapback to summary
protected Map<K, V> newMap()
parseback to summary
public static Object parse(String s)
Parameters
s:String

String containing JSON object or array.

Returns:Object

A Map, Object array or primitive array parsed from the JSON.

parseback to summary
public static Object parse(String s, boolean stripOuterComment)
Parameters
s:String

String containing JSON object or array.

stripOuterComment:boolean

If true, an outer comment around the JSON is ignored.

Returns:Object

A Map, Object array or primitive array parsed from the JSON.

parseback to summary
public static Object parse(Reader in) throws IOException
Parameters
in:Reader

Reader containing JSON object or array.

Returns:Object

A Map, Object array or primitive array parsed from the JSON.

parseback to summary
public static Object parse(Reader in, boolean stripOuterComment) throws IOException
Parameters
in:Reader

Stream containing JSON object or array.

stripOuterComment:boolean

If true, an outer comment around the JSON is ignored.

Returns:Object

A Map, Object array or primitive array parsed from the JSON.

parseback to summary
public static Object parse(InputStream in) throws IOException

Deprecated

use parse(Reader)

Parameters
in:InputStream

Reader containing JSON object or array.

Returns:Object

A Map, Object array or primitive array parsed from the JSON.

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

Deprecated

use parse(Reader, boolean)

Parameters
in:InputStream

Stream containing JSON object or array.

stripOuterComment:boolean

If true, an outer comment around the JSON is ignored.

Returns:Object

A Map, Object array or primitive array parsed from the JSON.

parseback to summary
public Object parse(JSON.Source source, boolean stripOuterComment)
parseback to summary
public Object parse(JSON.Source source)
parseArrayback to summary
private Object parseArray(JSON.Source source)
parseNumberback to summary
public Number parseNumber(JSON.Source source)
parseObjectback to summary
protected Object parseObject(JSON.Source source)
parseStringback to summary
private String parseString(JSON.Source source)
registerConvertorback to summary
public static void registerConvertor(Class<T> forClass, JSON.Convertor convertor)

Register a Convertor for a class or interface.

Parameters
forClass:Class<T>

The class or interface that the convertor applies to

convertor:JSON.Convertor

the convertor

seekToback to summary
protected void seekTo(char seek, JSON.Source source)
seekToback to summary
protected char seekTo(String seek, JSON.Source source)
setDefaultback to summary
public static void setDefault(JSON json)
setStringBufferSizeback to summary
public void setStringBufferSize(int stringBufferSize)
Parameters
stringBufferSize:int

the initial stringBuffer size to use when creating JSON strings (default 256)

toJSONback to summary
public String toJSON(Object object)

Convert Object to JSON

Parameters
object:Object

The object to convert

Returns:String

The JSON String

toStringback to summary
public static String toString(Object object)
toStringback to summary
public static String toString(Map<K, V> object)
toStringback to summary
public static String toString(Object[] array)
toStringback to summary
protected String toString(char[] buffer, int offset, int length)
org.mortbay.util.ajax back to summary

public Interface JSON.Convertible


JSON Convertible object. Object can implement this interface in a similar way to the Externalizable interface is used to allow classes to provide their own serialization mechanism.

A JSON.Convertible object may be written to a JSONObject or initialized from a Map of field names to values.

If the JSON is to be convertible back to an Object, then the method Output#addClass(Class) must be called from within toJSON()

Method Summary

Modifier and TypeMethod and Description
public void
fromJSON(Map<K, V> object)

public void

Method Detail

fromJSONback to summary
public void fromJSON(Map<K, V> object)
toJSONback to summary
public void toJSON(JSON.Output out)
org.mortbay.util.ajax back to summary

public Interface JSON.Convertor

Known Direct Implementers
org.mortbay.util.ajax.JSONDateConvertor, org.mortbay.util.ajax.JSONEnumConvertor, org.mortbay.util.ajax.JSONObjectConvertor

Static JSON Convertor.

may be implemented to provide static convertors for objects that may be registered with {@link JSON#registerConvertor(Class, org.mortbay.util.ajax.JSON.Convertor). These convertors are looked up by class, interface and super class by JSON#getConvertor(Class). Convertors should be used when the classes to be converted cannot implement Convertible or Generator.

Method Summary

Modifier and TypeMethod and Description
public Object
fromJSON(Map<K, V> object)

public void

Method Detail

fromJSONback to summary
public Object fromJSON(Map<K, V> object)
toJSONback to summary
public void toJSON(Object obj, JSON.Output out)
org.mortbay.util.ajax back to summary

public Interface JSON.Generator

Known Direct Implementers
org.mortbay.util.ajax.JSON.Literal

JSON Generator. A class that can add it's JSON representation directly to a StringBuffer. This is useful for object instances that are frequently converted and wish to avoid multiple Conversions

Method Summary

Modifier and TypeMethod and Description
public void

Method Detail

addJSONback to summary
public void addJSON(StringBuffer buffer)
org.mortbay.util.ajax back to summary

public Class JSON.Literal

extends Object
implements Generator
Class Inheritance
All Implemented Interfaces
org.mortbay.util.ajax.JSON.Generator

A Literal JSON generator A utility instance of JSON.Generator that holds a pre-generated string on JSON text.

Field Summary

Modifier and TypeField and Description
private String

Constructor Summary

AccessConstructor and Description
public
Literal(String
A literal JSON string.
json
)

Construct a literal JSON instance for use by JSON#toString(Object).

Method Summary

Modifier and TypeMethod and Description
public void
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

_jsonback to summary
private String _json

Constructor Detail

Literalback to summary
public Literal(String json)

Construct a literal JSON instance for use by JSON#toString(Object). If Log#isDebugEnabled() is true, the JSON will be parsed to check validity

Parameters
json:String

A literal JSON string.

Method Detail

addJSONback to summary
public void addJSON(StringBuffer buffer)

Implements org.mortbay.util.ajax.JSON.Generator.addJSON.

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

a string representation of the object.

org.mortbay.util.ajax back to summary

public Interface JSON.Output


JSON Output class for use by Convertible.

Method Summary

Modifier and TypeMethod and Description
public void
add(Object obj)

public void
add(String name, Object value)

public void
add(String name, double value)

public void
add(String name, long value)

public void
add(String name, boolean value)

public void

Method Detail

addback to summary
public void add(Object obj)
addback to summary
public void add(String name, Object value)
addback to summary
public void add(String name, double value)
addback to summary
public void add(String name, long value)
addback to summary
public void add(String name, boolean value)
addClassback to summary
public void addClass(Class<T> c)
org.mortbay.util.ajax back to summary

public Class JSON.ReaderSource

extends Object
implements Source
Class Inheritance
All Implemented Interfaces
org.mortbay.util.ajax.JSON.Source

Field Summary

Modifier and TypeField and Description
private int
private Reader
private char[]

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
private void
public boolean
public char
public char
public char[]
public void
setReader(Reader reader)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

_nextback to summary
private int _next
_readerback to summary
private Reader _reader
scratchback to summary
private char[] scratch

Constructor Detail

ReaderSourceback to summary
public ReaderSource(Reader r)

Method Detail

getNextback to summary
private void getNext()
hasNextback to summary
public boolean hasNext()

Implements org.mortbay.util.ajax.JSON.Source.hasNext.

nextback to summary
public char next()

Implements org.mortbay.util.ajax.JSON.Source.next.

peekback to summary
public char peek()

Implements org.mortbay.util.ajax.JSON.Source.peek.

scratchBufferback to summary
public char[] scratchBuffer()

Implements org.mortbay.util.ajax.JSON.Source.scratchBuffer.

setReaderback to summary
public void setReader(Reader reader)
org.mortbay.util.ajax back to summary

public Interface JSON.Source

Known Direct Implementers
org.mortbay.util.ajax.JSON.StringSource, org.mortbay.util.ajax.JSON.ReaderSource

Method Summary

Modifier and TypeMethod and Description
public boolean
public char
next()

public char
peek()

public char[]

Method Detail

hasNextback to summary
public boolean hasNext()
nextback to summary
public char next()
peekback to summary
public char peek()
scratchBufferback to summary
public char[] scratchBuffer()
org.mortbay.util.ajax back to summary

public Class JSON.StringSource

extends Object
implements Source
Class Inheritance
All Implemented Interfaces
org.mortbay.util.ajax.JSON.Source

Field Summary

Modifier and TypeField and Description
private int
private char[]
private final String

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public boolean
public char
public char
public char[]
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

indexback to summary
private int index
scratchback to summary
private char[] scratch
stringback to summary
private final String string

Constructor Detail

StringSourceback to summary
public StringSource(String s)

Method Detail

hasNextback to summary
public boolean hasNext()

Implements org.mortbay.util.ajax.JSON.Source.hasNext.

nextback to summary
public char next()

Implements org.mortbay.util.ajax.JSON.Source.next.

peekback to summary
public char peek()

Implements org.mortbay.util.ajax.JSON.Source.peek.

scratchBufferback to summary
public char[] scratchBuffer()

Implements org.mortbay.util.ajax.JSON.Source.scratchBuffer.

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

a string representation of the object.