public class JSON extends Object
object ==> Map array ==> Object[] number ==> Double or Long string ==> String null ==> null bool ==> BooleanThe 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
converters for objects that may be registered with
registerConvertor(Class, Convertor)
.
These converters are looked up by class, interface and super class by
getConvertor(Class)
.
If a JSON object has a "class" field, then a java class for that name is
loaded and the method convertTo(Class,Map)
is used to find a
JSON.Convertor
for that class.
If a JSON object has a "x-class" field then a direct lookup for a
JSON.Convertor
for that class name is done (without loading the class).Modifier and Type | Class and Description |
---|---|
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 and Description |
---|
JSON() |
Modifier and Type | Method and Description |
---|---|
void |
addConvertor(Class forClass,
JSON.Convertor convertor)
Register a
JSON.Convertor for a class or interface. |
void |
addConvertorFor(String name,
JSON.Convertor convertor)
Register a
JSON.Convertor for a named class or interface. |
void |
append(Appendable buffer,
Object object)
Append object as JSON to string buffer.
|
void |
append(StringBuffer buffer,
Object object)
Deprecated.
|
void |
appendArray(Appendable buffer,
Collection collection) |
void |
appendArray(Appendable buffer,
Object array) |
void |
appendArray(StringBuffer buffer,
Collection collection)
Deprecated.
|
void |
appendArray(StringBuffer buffer,
Object array)
Deprecated.
|
void |
appendBoolean(Appendable buffer,
Boolean b) |
void |
appendBoolean(StringBuffer buffer,
Boolean b)
Deprecated.
|
void |
appendJSON(Appendable buffer,
JSON.Convertible converter) |
void |
appendJSON(Appendable buffer,
JSON.Convertor convertor,
Object object) |
void |
appendJSON(Appendable buffer,
JSON.Generator generator) |
void |
appendJSON(StringBuffer buffer,
JSON.Convertible converter)
Deprecated.
|
void |
appendJSON(StringBuffer buffer,
JSON.Convertor convertor,
Object object)
Deprecated.
|
void |
appendJSON(StringBuffer buffer,
JSON.Generator generator)
Deprecated.
|
void |
appendMap(Appendable buffer,
Map<?,?> map) |
void |
appendMap(StringBuffer buffer,
Map<?,?> map)
Deprecated.
|
void |
appendNull(Appendable buffer) |
void |
appendNull(StringBuffer buffer)
Deprecated.
|
void |
appendNumber(Appendable buffer,
Number number) |
void |
appendNumber(StringBuffer buffer,
Number number)
Deprecated.
|
void |
appendString(Appendable buffer,
String string) |
void |
appendString(StringBuffer buffer,
String string)
Deprecated.
|
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.
|
JSON.Convertor |
getConvertorFor(String name)
Lookup a convertor for a named class.
|
static JSON |
getDefault() |
int |
getStringBufferSize() |
protected Object |
handleUnknown(JSON.Source source,
char c) |
protected Object[] |
newArray(int size) |
protected Map<String,Object> |
newMap() |
static Object |
parse(InputStream in)
Deprecated.
use
parse(Reader) |
static Object |
parse(InputStream in,
boolean stripOuterComment)
Deprecated.
|
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) |
protected Object |
parseArray(JSON.Source source) |
Number |
parseNumber(JSON.Source source) |
protected Object |
parseObject(JSON.Source source) |
protected String |
parseString(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)
Deprecated.
|
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) |
public static final JSON DEFAULT
public int getStringBufferSize()
public void setStringBufferSize(int stringBufferSize)
stringBufferSize
- the initial stringBuffer size to use when creating JSON
strings (default 1024)public static void registerConvertor(Class forClass, JSON.Convertor convertor)
JSON.Convertor
for a class or interface.forClass
- The class or interface that the convertor applies toconvertor
- the convertorpublic static JSON getDefault()
@Deprecated public static void setDefault(JSON json)
public static Object parse(String s)
s
- String containing JSON object or array.public static Object parse(String s, boolean stripOuterComment)
s
- String containing JSON object or array.stripOuterComment
- If true, an outer comment around the JSON is ignored.public static Object parse(Reader in) throws IOException
in
- Reader containing JSON object or array.IOException
public static Object parse(Reader in, boolean stripOuterComment) throws IOException
in
- Reader containing JSON object or array.stripOuterComment
- If true, an outer comment around the JSON is ignored.IOException
@Deprecated public static Object parse(InputStream in) throws IOException
parse(Reader)
in
- Reader containing JSON object or array.IOException
@Deprecated public static Object parse(InputStream in, boolean stripOuterComment) throws IOException
parse(Reader, boolean)
in
- Stream containing JSON object or array.stripOuterComment
- If true, an outer comment around the JSON is ignored.IOException
public String toJSON(Object object)
object
- The object to convertpublic Object fromJSON(String json)
json
- The json to convert@Deprecated public void append(StringBuffer buffer, Object object)
public void append(Appendable buffer, Object object)
buffer
- the buffer to append toobject
- the object to append@Deprecated public void appendNull(StringBuffer buffer)
public void appendNull(Appendable buffer)
@Deprecated public void appendJSON(StringBuffer buffer, JSON.Convertor convertor, Object object)
public void appendJSON(Appendable buffer, JSON.Convertor convertor, Object object)
@Deprecated public void appendJSON(StringBuffer buffer, JSON.Convertible converter)
public void appendJSON(Appendable buffer, JSON.Convertible converter)
@Deprecated public void appendJSON(StringBuffer buffer, JSON.Generator generator)
public void appendJSON(Appendable buffer, JSON.Generator generator)
@Deprecated public void appendMap(StringBuffer buffer, Map<?,?> map)
public void appendMap(Appendable buffer, Map<?,?> map)
@Deprecated public void appendArray(StringBuffer buffer, Collection collection)
public void appendArray(Appendable buffer, Collection collection)
@Deprecated public void appendArray(StringBuffer buffer, Object array)
public void appendArray(Appendable buffer, Object array)
@Deprecated public void appendBoolean(StringBuffer buffer, Boolean b)
public void appendBoolean(Appendable buffer, Boolean b)
@Deprecated public void appendNumber(StringBuffer buffer, Number number)
public void appendNumber(Appendable buffer, Number number)
@Deprecated public void appendString(StringBuffer buffer, String string)
public void appendString(Appendable buffer, String string)
protected String toString(char[] buffer, int offset, int length)
protected Object[] newArray(int size)
protected JSON contextForArray()
public void addConvertor(Class forClass, JSON.Convertor convertor)
JSON.Convertor
for a class or interface.forClass
- The class or interface that the convertor applies toconvertor
- the convertorprotected JSON.Convertor getConvertor(Class forClass)
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.
forClass
- The classJSON.Convertor
or null if none were found.public void addConvertorFor(String name, JSON.Convertor convertor)
JSON.Convertor
for a named class or interface.name
- name of a class or an interface that the convertor applies toconvertor
- the convertorpublic JSON.Convertor getConvertorFor(String name)
name
- name of the classJSON.Convertor
or null if none were found.public Object parse(JSON.Source source, boolean stripOuterComment)
public Object parse(JSON.Source source)
protected Object handleUnknown(JSON.Source source, char c)
protected Object parseObject(JSON.Source source)
protected Object parseArray(JSON.Source source)
protected String parseString(JSON.Source source)
public Number parseNumber(JSON.Source source)
protected void seekTo(char seek, JSON.Source source)
protected char seekTo(String seek, JSON.Source source)
protected static void complete(String seek, JSON.Source source)
Copyright © 1995-2015 Webtide. All Rights Reserved.