Class JSON
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 --> 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).
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceJSON Convertible object.static interfaceStatic JSON Convertor.static interfaceJSON Generator.static classA Literal JSON generator A utility instance ofJSON.Generatorthat holds a pre-generated string on JSON text.static interfaceJSON Output class for use byJSON.Convertible.static classstatic interfacestatic class -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddConvertor(Class forClass, JSON.Convertor convertor) Register aJSON.Convertorfor a class or interface.voidaddConvertorFor(String name, JSON.Convertor convertor) Register aJSON.Convertorfor a named class or interface.voidappend(Appendable buffer, Object object) Append object as JSON to string buffer.voidappend(StringBuffer buffer, Object object) Deprecated.voidappendArray(Appendable buffer, Object array) voidappendArray(Appendable buffer, Collection collection) voidappendArray(StringBuffer buffer, Object array) Deprecated.voidappendArray(StringBuffer buffer, Collection collection) Deprecated.voidappendBoolean(Appendable buffer, Boolean b) voidappendBoolean(StringBuffer buffer, Boolean b) Deprecated.voidappendJSON(Appendable buffer, JSON.Convertible converter) voidappendJSON(Appendable buffer, JSON.Convertor convertor, Object object) voidappendJSON(Appendable buffer, JSON.Generator generator) voidappendJSON(StringBuffer buffer, JSON.Convertible converter) Deprecated.voidappendJSON(StringBuffer buffer, JSON.Convertor convertor, Object object) Deprecated.voidappendJSON(StringBuffer buffer, JSON.Generator generator) Deprecated.voidappendMap(Appendable buffer, Map<?, ?> map) voidappendMap(StringBuffer buffer, Map<?, ?> map) Deprecated.voidappendNull(Appendable buffer) voidappendNull(StringBuffer buffer) Deprecated.voidappendNumber(Appendable buffer, Number number) voidappendNumber(StringBuffer buffer, Number number) Deprecated.voidappendString(Appendable buffer, String string) voidappendString(StringBuffer buffer, String string) Deprecated.protected static voidcomplete(String seek, JSON.Source source) protected JSONcontextFor(String field) protected JSONprotected ObjectvoidescapeString(Appendable buffer, String input) protected voidescapeUnicode(Appendable buffer, char c) Per spec, unicode characters are by default NOT escaped.Convert JSON to Objectprotected JSON.ConvertorgetConvertor(Class forClass) Lookup a convertor for a class.getConvertorFor(String name) Lookup a convertor for a named class.static JSONintprotected ObjecthandleUnknown(JSON.Source source, char c) protected Object[]newArray(int size) Deprecated.newMap()static Objectparse(InputStream in) Deprecated.useparse(Reader)static Objectparse(InputStream in, boolean stripOuterComment) Deprecated.static Objectstatic Objectstatic Objectstatic Objectparse(JSON.Source source) parse(JSON.Source source, boolean stripOuterComment) protected ObjectparseArray(JSON.Source source) parseNumber(JSON.Source source) protected ObjectparseObject(JSON.Source source) protected StringparseString(JSON.Source source) static voidregisterConvertor(Class forClass, JSON.Convertor convertor) Register aJSON.Convertorfor a class or interface.removeConvertorFor(String name) Removes a registeredJSON.Convertorfor the given named class or interface.static voidreset()Reset the default JSON behaviors to defaultprotected voidseekTo(char seek, JSON.Source source) protected charseekTo(String seek, JSON.Source source) voidsetArrayConverter(Function<List<?>, Object> arrayConverter) Sets the function to convert JSON arrays from their default Java representation, aList<Object>, to another Java data structure such as anObject[].static voidsetDefault(JSON json) Deprecated.voidsetStringBufferSize(int stringBufferSize) Convert Object to JSONprotected StringtoString(char[] buffer, int offset, int length) static Stringstatic Stringstatic String
-
Field Details
-
DEFAULT
-
-
Constructor Details
-
JSON
public JSON()
-
-
Method Details
-
reset
public static void reset()Reset the default JSON behaviors to default -
getStringBufferSize
public int getStringBufferSize()- Returns:
- the initial stringBuffer size to use when creating JSON strings (default 1024)
-
setStringBufferSize
public void setStringBufferSize(int stringBufferSize) - Parameters:
stringBufferSize- the initial stringBuffer size to use when creating JSON strings (default 1024)
-
registerConvertor
Register aJSON.Convertorfor a class or interface.- Parameters:
forClass- The class or interface that the convertor applies toconvertor- the convertor
-
getDefault
-
setDefault
Deprecated. -
toString
-
toString
-
toString
-
parse
- Parameters:
s- String containing JSON object or array.- Returns:
- A Map, Object array or primitive array parsed from the JSON.
-
parse
- 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
- Parameters:
in- Reader containing JSON object or array.- Returns:
- A Map, Object array or primitive array parsed from the JSON.
- Throws:
IOException- if unable to parse
-
parse
- Parameters:
in- Reader 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- if unable to parse
-
parse
Deprecated.useparse(Reader)- Parameters:
in- Reader containing JSON object or array.- Returns:
- A Map, Object array or primitive array parsed from the JSON.
- Throws:
IOException- if unable to parse
-
parse
@Deprecated public static Object parse(InputStream in, boolean stripOuterComment) throws IOException Deprecated.- Parameters:
in- 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- if unable to parse
-
escapeString
- Throws:
IOException
-
escapeUnicode
Per spec, unicode characters are by default NOT escaped. This overridable allows for alternate behavior to escape those with your choice of encoding.protected void escapeUnicode(Appendable buffer, char c) throws IOException { // Unicode is slash-u escaped buffer.append(String.format("\\u%04x", (int)c)); }- Throws:
IOException
-
toJSON
Convert Object to JSON- Parameters:
object- The object to convert- Returns:
- The JSON String
-
fromJSON
Convert JSON to Object- Parameters:
json- The json to convert- Returns:
- The object
-
append
Deprecated. -
append
Append object as JSON to string buffer.- Parameters:
buffer- the buffer to append toobject- the object to append
-
appendNull
Deprecated. -
appendNull
-
appendJSON
Deprecated. -
appendJSON
-
appendJSON
Deprecated. -
appendJSON
-
appendJSON
Deprecated. -
appendJSON
-
appendMap
Deprecated. -
appendMap
-
appendArray
Deprecated. -
appendArray
-
appendArray
Deprecated. -
appendArray
-
appendBoolean
Deprecated. -
appendBoolean
-
appendNumber
Deprecated. -
appendNumber
-
appendString
Deprecated. -
appendString
-
toString
-
newMap
-
newArray
Deprecated.usesetArrayConverter(Function)instead.- Parameters:
size- the size of the array- Returns:
- a new array
-
contextForArray
-
contextFor
-
convertTo
-
addConvertor
Register aJSON.Convertorfor a class or interface.- Parameters:
forClass- The class or interface that the convertor applies toconvertor- the convertor
-
getConvertor
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.Convertoror null if none were found.
-
addConvertorFor
Register aJSON.Convertorfor a named class or interface.- Parameters:
name- name of a class or an interface that the convertor applies toconvertor- the convertor
-
removeConvertorFor
Removes a registeredJSON.Convertorfor the given named class or interface.- Parameters:
name- name of a class or an interface for a registeredJSON.Convertor- Returns:
- the
JSON.Convertorthat was removed, or null
-
getArrayConverter
- Returns:
- the function to customize the Java representation of JSON arrays
- See Also:
-
setArrayConverter
Sets the function to convert JSON arrays from their default Java representation, a
List<Object>, to another Java data structure such as anObject[].- Parameters:
arrayConverter- the function to customize the Java representation of JSON arrays- See Also:
-
getConvertorFor
Lookup a convertor for a named class.- Parameters:
name- name of the class- Returns:
- a
JSON.Convertoror null if none were found.
-
parse
-
parse
-
handleUnknown
-
parseObject
-
parseArray
-
parseString
-
parseNumber
-
seekTo
-
seekTo
-
complete
-
setArrayConverter(Function)instead.