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).
| Modifier and Type | Class 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 |
| public static interface | JSON.Output
JSON Output class for use by |
| public static class | |
| public static interface | |
| public static class |
| Modifier and Type | Field and Description |
|---|---|
| private static JSON | |
| private Map | |
| private int |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public void | addConvertor(Class<T>
The class or interface that the convertor applies to forClass, JSON.the convertor convertorRegister a |
| public void | |
| public void | |
| public void | |
| public void | |
| public void | |
| public void | |
| public void | |
| public void | |
| public void | |
| public void | |
| public void | |
| protected static void | |
| protected JSON | |
| protected JSON | |
| protected Object | |
| public Object | |
| protected JSON. | Returns: aConvertor or null if none were found.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)
|
| protected Object[] | |
| protected Map | |
| public static Object | |
| public static Object | |
| public static Object | |
| public static Object | |
| public static Object | Returns: A Map, Object array or primitive array parsed from the JSON.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.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 | |
| public Object | |
| 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.the convertor convertorRegister a |
| protected void | |
| protected char | |
| public static void | |
| public void | setStringBufferSize(int
the initial stringBuffer size to use when creating JSON strings (default 256) stringBufferSize)
|
| public String | |
| public static String | |
| public static String | |
| public static String | |
| protected String |
| __default | back to summary |
|---|---|
| private static JSON __default | |
| _convertors | back to summary |
|---|---|
| private Map<K, V> _convertors | |
| _stringBufferSize | back to summary |
|---|---|
| private int _stringBufferSize | |
| JSON | back to summary |
|---|---|
| public JSON() | |
| addConvertor | back to summary |
|---|---|
| public void addConvertor(Class<T> forClass, JSON. Register a | |
| append | back to summary |
|---|---|
| public void append(StringBuffer buffer, Object object) Append object as JSON to string buffer. | |
| appendArray | back to summary |
|---|---|
| public void appendArray(StringBuffer buffer, Collection<E> collection) | |
| appendArray | back to summary |
|---|---|
| public void appendArray(StringBuffer buffer, Object array) | |
| appendBoolean | back to summary |
|---|---|
| public void appendBoolean(StringBuffer buffer, Boolean b) | |
| appendJSON | back to summary |
|---|---|
| public void appendJSON(final StringBuffer buffer, final JSON. | |
| appendJSON | back to summary |
|---|---|
| public void appendJSON(final StringBuffer buffer, JSON. | |
| appendJSON | back to summary |
|---|---|
| public void appendJSON(StringBuffer buffer, JSON. | |
| appendMap | back to summary |
|---|---|
| public void appendMap(StringBuffer buffer, Map<K, V> object) | |
| appendNull | back to summary |
|---|---|
| public void appendNull(StringBuffer buffer) | |
| appendNumber | back to summary |
|---|---|
| public void appendNumber(StringBuffer buffer, Number number) | |
| appendString | back to summary |
|---|---|
| public void appendString(StringBuffer buffer, String string) | |
| complete | back to summary |
|---|---|
| protected static void complete(String seek, JSON. | |
| contextFor | back to summary |
|---|---|
| protected JSON contextFor(String field) | |
| contextForArray | back to summary |
|---|---|
| protected JSON contextForArray() | |
| convertTo | back to summary |
|---|---|
| protected Object convertTo(Class<T> type, Map<K, V> map) | |
| fromJSON | back to summary |
|---|---|
| public Object fromJSON(String json) Convert JSON to Object | |
| getConvertor | back to summary |
|---|---|
| protected JSON. 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. | |
| getDefault | back to summary |
|---|---|
| public static JSON getDefault() | |
| getStringBufferSize | back to summary |
|---|---|
public int getStringBufferSize()
| |
| newArray | back to summary |
|---|---|
| protected Object[] newArray(int size) | |
| newMap | back to summary |
|---|---|
| protected Map | |
| parse | back to summary |
|---|---|
| public static Object parse(String s) | |
| parse | back to summary |
|---|---|
| public static Object parse(String s, boolean stripOuterComment) | |
| parse | back to summary |
|---|---|
| public static Object parse(Reader in) throws IOException | |
| parse | back to summary |
|---|---|
| public static Object parse(Reader in, boolean stripOuterComment) throws IOException | |
| parse | back to summary |
|---|---|
| public static Object parse(InputStream in) throws IOException
Deprecated use
| |
| parse | back to summary |
|---|---|
| public static Object parse(InputStream in, boolean stripOuterComment) throws IOException
Deprecated
| |
| parse | back to summary |
|---|---|
| public Object parse(JSON. | |
| parse | back to summary |
|---|---|
| public Object parse(JSON. | |
| parseArray | back to summary |
|---|---|
| private Object parseArray(JSON. | |
| parseNumber | back to summary |
|---|---|
| public Number parseNumber(JSON. | |
| parseObject | back to summary |
|---|---|
| protected Object parseObject(JSON. | |
| parseString | back to summary |
|---|---|
| private String parseString(JSON. | |
| registerConvertor | back to summary |
|---|---|
| public static void registerConvertor(Class<T> forClass, JSON. Register a | |
| seekTo | back to summary |
|---|---|
| protected void seekTo(char seek, JSON. | |
| seekTo | back to summary |
|---|---|
| protected char seekTo(String seek, JSON. | |
| setDefault | back to summary |
|---|---|
| public static void setDefault(JSON json) | |
| setStringBufferSize | back to summary |
|---|---|
public void setStringBufferSize(int stringBufferSize)
| |
| toJSON | back to summary |
|---|---|
| public String toJSON(Object object) Convert Object to JSON | |
| toString | back to summary |
|---|---|
| public static String toString(Object object) | |
| toString | back to summary |
|---|---|
| public static String toString(Map<K, V> object) | |
| toString | back to summary |
|---|---|
| public static String toString(Object[] array) | |
| toString | back to summary |
|---|---|
| protected String toString(char[] buffer, int offset, int length) | |
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()
| Modifier and Type | Method and Description |
|---|---|
| public void | |
| public void |
| fromJSON | back to summary |
|---|---|
| public void fromJSON(Map<K, V> object) | |
| toJSON | back to summary |
|---|---|
| public void toJSON(JSON. | |
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.
| Modifier and Type | Method and Description |
|---|---|
| public Object | |
| public void |
| fromJSON | back to summary |
|---|---|
| public Object fromJSON(Map<K, V> object) | |
| toJSON | back to summary |
|---|---|
| public void toJSON(Object obj, JSON. | |
| Modifier and Type | Method and Description |
|---|---|
| public void |
| addJSON | back to summary |
|---|---|
| public void addJSON(StringBuffer buffer) | |
JSON.Generator that holds a pre-generated string on JSON text.
| Access | Constructor and Description |
|---|---|
| public | Literal(String
A literal JSON string. json)Construct a literal JSON instance for use by |
| Modifier and Type | Method and Description |
|---|---|
| public void | |
| public String |
| _json | back to summary |
|---|---|
| private String _json | |
| Literal | back to summary |
|---|---|
| public Literal(String json) Construct a literal JSON instance for use by
| |
| addJSON | back to summary |
|---|---|
| public void addJSON(StringBuffer buffer) Implements org. | |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Doc from java. Returns a string representation of the object.
| |
Convertible.
| Modifier and Type | Method and Description |
|---|---|
| public void | |
| public void | |
| public void | |
| public void | |
| public void | |
| public void |
| add | back to summary |
|---|---|
| public void add(Object obj) | |
| add | back to summary |
|---|---|
| public void add(String name, Object value) | |
| add | back to summary |
|---|---|
| public void add(String name, double value) | |
| add | back to summary |
|---|---|
| public void add(String name, long value) | |
| add | back to summary |
|---|---|
| public void add(String name, boolean value) | |
| addClass | back to summary |
|---|---|
| public void addClass(Class<T> c) | |
| Modifier and Type | Field and Description |
|---|---|
| private int | |
| private Reader | |
| private char[] |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| private void | |
| public boolean | |
| public char | |
| public char | |
| public char[] | |
| public void |
| _next | back to summary |
|---|---|
| private int _next | |
| _reader | back to summary |
|---|---|
| private Reader _reader | |
| scratch | back to summary |
|---|---|
| private char[] scratch | |
| ReaderSource | back to summary |
|---|---|
| public ReaderSource(Reader r) | |
| getNext | back to summary |
|---|---|
| private void getNext() | |
| hasNext | back to summary |
|---|---|
| public boolean hasNext() Implements org. | |
| next | back to summary |
|---|---|
| public char next() Implements org. | |
| peek | back to summary |
|---|---|
| public char peek() Implements org. | |
| scratchBuffer | back to summary |
|---|---|
| public char[] scratchBuffer() Implements org. | |
| setReader | back to summary |
|---|---|
| public void setReader(Reader reader) | |
| Modifier and Type | Method and Description |
|---|---|
| public boolean | |
| public char | |
| public char | |
| public char[] |
| hasNext | back to summary |
|---|---|
| public boolean hasNext() | |
| next | back to summary |
|---|---|
| public char next() | |
| peek | back to summary |
|---|---|
| public char peek() | |
| scratchBuffer | back to summary |
|---|---|
| public char[] scratchBuffer() | |
| Modifier and Type | Field and Description |
|---|---|
| private int | |
| private char[] | |
| private final String |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public boolean | |
| public char | |
| public char | |
| public char[] | |
| public String |
| index | back to summary |
|---|---|
| private int index | |
| scratch | back to summary |
|---|---|
| private char[] scratch | |
| string | back to summary |
|---|---|
| private final String string | |
| StringSource | back to summary |
|---|---|
| public StringSource(String s) | |
| hasNext | back to summary |
|---|---|
| public boolean hasNext() Implements org. | |
| next | back to summary |
|---|---|
| public char next() Implements org. | |
| peek | back to summary |
|---|---|
| public char peek() Implements org. | |
| scratchBuffer | back to summary |
|---|---|
| public char[] scratchBuffer() Implements org. | |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Doc from java. Returns a string representation of the object.
| |