Class JSON
JSON parser and generator.
This class provides methods to convert POJOs to and from JSON notation.
The mapping from JSON to Java is:
object --> Map<String, Object> 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 toJSON(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
converters for objects that may be registered with
addConvertor(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 interfaceJSON Convertor.static interfaceJSON Generator.static classA Literal JSON generator.static interfaceJSON Output class for use byJSON.Convertible.static classA Reader source for a JSON string.static interfaceA generic source for a JSON representation.static classAn in-memory source for a JSON string. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddConvertor(Class<?> forClass, JSON.Convertor convertor) Registers aJSON.Convertorfor the given class.voidaddConvertorFor(String name, JSON.Convertor convertor) Registers aJSON.Convertorfor a named class.voidappend(Appendable buffer, Object object) Appends the given object as JSON to string buffer.voidappendArray(Appendable buffer, Object array) voidappendArray(Appendable buffer, Collection<?> collection) voidappendBoolean(Appendable buffer, Boolean b) voidappendJSON(Appendable buffer, JSON.Convertible converter) voidappendJSON(Appendable buffer, JSON.Convertor convertor, Object object) voidappendJSON(Appendable buffer, JSON.Generator generator) voidappendMap(Appendable buffer, Map<?, ?> map) voidappendNull(Appendable buffer) voidappendNumber(Appendable buffer, Number number) voidappendString(Appendable buffer, String string) protected static voidcomplete(String seek, JSON.Source source) protected JSONcontextFor(String field) Every time a JSON object field representation{"name": value}is parsed, this method is called to (possibly) return a different JSON instance (for example configured with different converters) to parse the object field.protected JSONEvery time a JSON array representation[...]is parsed, this method is called to (possibly) return a different JSON instance (for example configured with different converters) to parse the array items.protected ObjectvoidescapeString(Appendable buffer, String input) Escapes the characters of the giveninputstring into the given buffer.protected voidescapeUnicode(Appendable buffer, char c) Per JSON specification, unicode characters are by default NOT escaped.Parses the JSON from the given Reader into an object.fromJSON(CharSequence string) Parses the given JSON string into an object.protected JSON.ConvertorgetConvertor(Class<?> forClass) Looks up a convertor for a class.getConvertorFor(String name) Looks up a convertor for a class name.intprotected ObjecthandleUnknown(JSON.Source source, char c) protected Object[]newArray(int size) Deprecated.newMap()Factory method that creates a Map when a JSON representation of{...}is parsed.parse(JSON.Source source) Parses the given JSON source into an object.parse(JSON.Source source, boolean stripOuterComment) Parses the given JSON source into an object.protected ObjectparseArray(JSON.Source source) parseNumber(JSON.Source source) protected ObjectparseObject(JSON.Source source) protected StringparseString(JSON.Source source) removeConvertor(Class<?> forClass) Unregisters aJSON.Convertorfor a class.removeConvertorFor(String name) Unregisters aJSON.Convertorfor a named class.protected 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[].voidsetStringBufferSize(int stringBufferSize) Converts any object to JSON.
-
Constructor Details
-
JSON
public JSON()
-
-
Method Details
-
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)
-
escapeString
Escapes the characters of the given
inputstring into the given buffer.- Parameters:
buffer- the buffer to escape the string intoinput- the string to escape- Throws:
IOException- if appending to the buffer fails- See Also:
-
escapeUnicode
Per JSON specification, unicode characters are by default NOT escaped.
Overriding this method allows for alternate behavior to escape those with your choice of encoding.
protected void escapeUnicode(Appendable buffer, char c) throws IOException { // Unicode is backslash-u escaped buffer.append(String.format("\\u%04x", (int)c)); }- Throws:
IOException
-
toJSON
-
append
Appends the given object as JSON to string buffer.
This method tests the given object type and calls other appends methods for each object type, see for example
appendMap(Appendable, Map).- Parameters:
buffer- the buffer to append toobject- the object to convert to JSON
-
appendNull
-
appendJSON
-
appendJSON
-
appendJSON
-
appendMap
-
appendArray
-
appendArray
-
appendBoolean
-
appendNumber
-
appendString
-
newMap
-
newArray
Deprecated.usesetArrayConverter(Function)instead.Factory method that creates an array when a JSON representation of
[...]is parsed.- Parameters:
size- the size of the array- Returns:
- a new array representing the JSON array
-
contextForArray
Every time a JSON array representation
[...]is parsed, this method is called to (possibly) return a different JSON instance (for example configured with different converters) to parse the array items.- Returns:
- a JSON instance to parse array items
-
contextFor
Every time a JSON object field representation
{"name": value}is parsed, this method is called to (possibly) return a different JSON instance (for example configured with different converters) to parse the object field.- Parameters:
field- the field name- Returns:
- a JSON instance to parse the object field
-
convertTo
-
addConvertor
Registers a
JSON.Convertorfor the given class.- Parameters:
forClass- the class the convertor applies toconvertor- the convertor for the class
-
addConvertorFor
Registers a
JSON.Convertorfor a named class.- Parameters:
name- the name of the class the convertor applies toconvertor- the convertor for the class
-
removeConvertor
Unregisters a
JSON.Convertorfor a class.- Parameters:
forClass- the class the convertor applies to- Returns:
- the convertor for the class
-
removeConvertorFor
Unregisters a
JSON.Convertorfor a named class.- Parameters:
name- the name of the class the convertor applies to- Returns:
- the convertor for the class
-
getConvertor
Looks up 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 its interfaces are tried iteratively.
- Parameters:
forClass- the class to look up the convertor- Returns:
- a
JSON.Convertoror null if none was found for the class
-
getConvertorFor
Looks up a convertor for a class name.
- Parameters:
name- name of the class to look up the convertor- Returns:
- a
JSON.Convertoror null if none were found.
-
getArrayConverter
-
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:
-
parse
Parses the given JSON source into an object.
Although the JSON specification does not allow comments (of any kind) this method optionally strips out outer comments of this form:
// An outer comment line. /* Another outer comment, multiline. // Yet another comment line. { "name": "the real JSON" } */ End of outer comment, multiline.- Parameters:
source- the JSON source to parsestripOuterComment- whether to strip outer comments- Returns:
- the object constructed from the JSON string representation
-
fromJSON
Parses the given JSON string into an object.
- Parameters:
string- the JSON string to parse- Returns:
- the object constructed from the JSON string representation
-
fromJSON
-
parse
Parses the given JSON source into an object.
Although the JSON specification does not allow comments (of any kind) this method strips out initial comments of this form:
// An initial comment line. /* An initial multiline comment */ { "name": "foo" }This method detects the object type and calls other parse methods for each object type, see for example
parseArray(Source).- Parameters:
source- the JSON source to parse- Returns:
- the object constructed from the JSON string representation
-
handleUnknown
-
parseObject
-
parseArray
-
parseString
-
parseNumber
-
seekTo
-
seekTo
-
complete
-
setArrayConverter(Function)instead.