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
Modifier and TypeClassDescriptionstatic interface
JSON Convertible object.static interface
JSON Convertor.static interface
JSON Generator.static class
A Literal JSON generator.static interface
JSON Output class for use byJSON.Convertible
.static class
A Reader source for a JSON string.static interface
A generic source for a JSON representation.static class
An in-memory source for a JSON string. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
addConvertor
(Class<?> forClass, JSON.Convertor convertor) Registers aJSON.Convertor
for the given class.void
addConvertorFor
(String name, JSON.Convertor convertor) Registers aJSON.Convertor
for a named class.void
append
(Appendable buffer, Object object) Appends the given object as JSON to string buffer.void
appendArray
(Appendable buffer, Object array) void
appendArray
(Appendable buffer, Collection<?> collection) void
appendBoolean
(Appendable buffer, Boolean b) void
appendJSON
(Appendable buffer, JSON.Convertible converter) void
appendJSON
(Appendable buffer, JSON.Convertor convertor, Object object) void
appendJSON
(Appendable buffer, JSON.Generator generator) void
appendMap
(Appendable buffer, Map<?, ?> map) void
appendNull
(Appendable buffer) void
appendNumber
(Appendable buffer, Number number) void
appendString
(Appendable buffer, String string) protected static void
complete
(String seek, JSON.Source source) protected JSON
contextFor
(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 JSON
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.protected Object
void
escapeString
(Appendable buffer, String input) Escapes the characters of the giveninput
string into the given buffer.protected void
escapeUnicode
(Appendable buffer, char c) Per JSON specification, unicode characters are by default NOT escaped.Parses the JSON from the given Reader into an object.Parses the given JSON string into an object.protected JSON.Convertor
getConvertor
(Class<?> forClass) Looks up a convertor for a class.getConvertorFor
(String name) Looks up a convertor for a class name.int
protected Object
handleUnknown
(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 Object
parseArray
(JSON.Source source) parseNumber
(JSON.Source source) protected Object
parseObject
(JSON.Source source) protected String
parseString
(JSON.Source source) removeConvertor
(Class<?> forClass) Unregisters aJSON.Convertor
for a class.removeConvertorFor
(String name) Unregisters aJSON.Convertor
for a named class.protected void
seekTo
(char seek, JSON.Source source) protected char
seekTo
(String seek, JSON.Source source) void
setArrayConverter
(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[]
.void
setStringBufferSize
(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
input
string 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
Converts any object to JSON.
- Parameters:
object
- the object to convert- Returns:
- the JSON string representation of the object
- See Also:
-
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
Factory method that creates a Map when a JSON representation of
{...}
is parsed.- Returns:
- a new Map representing the JSON object
-
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.Convertor
for the given class.- Parameters:
forClass
- the class the convertor applies toconvertor
- the convertor for the class
-
addConvertorFor
Registers a
JSON.Convertor
for a named class.- Parameters:
name
- the name of the class the convertor applies toconvertor
- the convertor for the class
-
removeConvertor
Unregisters a
JSON.Convertor
for a class.- Parameters:
forClass
- the class the convertor applies to- Returns:
- the convertor for the class
-
removeConvertorFor
Unregisters a
JSON.Convertor
for 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.Convertor
or 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.Convertor
or null if none were found.
-
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:
-
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
Parses the JSON from the given Reader into an object.
- Parameters:
reader
- the Reader to read the JSON from- Returns:
- the object constructed from the JSON string representation
-
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.