- java.lang.Object
-
- org.eclipse.jetty.util.ajax.JSON
-
public class JSON extends java.lang.ObjectJSON 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.Convertiblemay 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.Generatormay be implemented by classes that know how to render themselves as JSON and thetoJSON(Object)method will useJSON.Generator.addJSON(Appendable)to generate the JSON.The class
JSON.Literalmay be used to hold pre-generated JSON object.The interface
JSON.Convertormay be implemented to provide converters for objects that may be registered withaddConvertor(Class, Convertor). These converters are looked up by class, interface and super class bygetConvertor(Class).If a JSON object has a
classfield, then a Java class for that name is loaded and the methodconvertTo(Class, Map)is used to find aJSON.Convertorfor that class.If a JSON object has a
x-classfield then a direct lookup for aJSON.Convertorfor that class name is done (without loading the class).
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceJSON.ConvertibleJSON Convertible object.static interfaceJSON.ConvertorJSON Convertor.static interfaceJSON.GeneratorJSON Generator.static classJSON.LiteralA Literal JSON generator.static interfaceJSON.OutputJSON Output class for use byJSON.Convertible.static classJSON.ReaderSourceA Reader source for a JSON string.static interfaceJSON.SourceA generic source for a JSON representation.static classJSON.StringSourceAn in-memory source for a JSON string.
-
Constructor Summary
Constructors Constructor Description JSON()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voidaddConvertor(java.lang.Class<?> forClass, JSON.Convertor convertor)Registers aJSON.Convertorfor the given class.voidaddConvertorFor(java.lang.String name, JSON.Convertor convertor)Registers aJSON.Convertorfor a named class.voidappend(java.lang.Appendable buffer, java.lang.Object object)Appends the given object as JSON to string buffer.voidappendArray(java.lang.Appendable buffer, java.lang.Object array)voidappendArray(java.lang.Appendable buffer, java.util.Collection<?> collection)voidappendBoolean(java.lang.Appendable buffer, java.lang.Boolean b)voidappendJSON(java.lang.Appendable buffer, JSON.Convertible converter)voidappendJSON(java.lang.Appendable buffer, JSON.Convertor convertor, java.lang.Object object)voidappendJSON(java.lang.Appendable buffer, JSON.Generator generator)voidappendMap(java.lang.Appendable buffer, java.util.Map<?,?> map)voidappendNull(java.lang.Appendable buffer)voidappendNumber(java.lang.Appendable buffer, java.lang.Number number)voidappendString(java.lang.Appendable buffer, java.lang.String string)protected static voidcomplete(java.lang.String seek, JSON.Source source)protected JSONcontextFor(java.lang.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 JSONcontextForArray()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 java.lang.ObjectconvertTo(java.lang.Class<?> type, java.util.Map<java.lang.String,java.lang.Object> map)voidescapeString(java.lang.Appendable buffer, java.lang.String input)Escapes the characters of the giveninputstring into the given buffer.protected voidescapeUnicode(java.lang.Appendable buffer, char c)Per JSON specification, unicode characters are by default NOT escaped.java.lang.ObjectfromJSON(java.io.Reader reader)Parses the JSON from the given Reader into an object.java.lang.ObjectfromJSON(java.lang.String string)Parses the given JSON string into an object.java.util.function.Function<java.util.List<?>,java.lang.Object>getArrayConverter()protected JSON.ConvertorgetConvertor(java.lang.Class<?> forClass)Looks up a convertor for a class.JSON.ConvertorgetConvertorFor(java.lang.String name)Looks up a convertor for a class name.intgetStringBufferSize()protected java.lang.ObjecthandleUnknown(JSON.Source source, char c)protected java.lang.Object[]newArray(int size)Deprecated.usesetArrayConverter(Function)instead.protected java.util.Map<java.lang.String,java.lang.Object>newMap()Factory method that creates a Map when a JSON representation of{...}is parsed.java.lang.Objectparse(JSON.Source source)Parses the given JSON source into an object.java.lang.Objectparse(JSON.Source source, boolean stripOuterComment)Parses the given JSON source into an object.protected java.lang.ObjectparseArray(JSON.Source source)java.lang.NumberparseNumber(JSON.Source source)protected java.lang.ObjectparseObject(JSON.Source source)protected java.lang.StringparseString(JSON.Source source)JSON.ConvertorremoveConvertor(java.lang.Class<?> forClass)Unregisters aJSON.Convertorfor a class.JSON.ConvertorremoveConvertorFor(java.lang.String name)Unregisters aJSON.Convertorfor a named class.protected voidseekTo(char seek, JSON.Source source)protected charseekTo(java.lang.String seek, JSON.Source source)voidsetArrayConverter(java.util.function.Function<java.util.List<?>,java.lang.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)java.lang.StringtoJSON(java.lang.Object object)Converts any object to JSON.
-
-
-
Method Detail
-
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
public void escapeString(java.lang.Appendable buffer, java.lang.String input) throws java.io.IOExceptionEscapes the characters of the given
inputstring into the given buffer.- Parameters:
buffer- the buffer to escape the string intoinput- the string to escape- Throws:
java.io.IOException- if appending to the buffer fails- See Also:
escapeUnicode(Appendable, char)
-
escapeUnicode
protected void escapeUnicode(java.lang.Appendable buffer, char c) throws java.io.IOExceptionPer 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:
java.io.IOException
-
toJSON
public java.lang.String toJSON(java.lang.Object object)
Converts any object to JSON.
- Parameters:
object- the object to convert- Returns:
- the JSON string representation of the object
- See Also:
append(Appendable, Object)
-
append
public void append(java.lang.Appendable buffer, java.lang.Object object)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
public void appendNull(java.lang.Appendable buffer)
-
appendJSON
public void appendJSON(java.lang.Appendable buffer, JSON.Convertor convertor, java.lang.Object object)
-
appendJSON
public void appendJSON(java.lang.Appendable buffer, JSON.Convertible converter)
-
appendJSON
public void appendJSON(java.lang.Appendable buffer, JSON.Generator generator)
-
appendMap
public void appendMap(java.lang.Appendable buffer, java.util.Map<?,?> map)
-
appendArray
public void appendArray(java.lang.Appendable buffer, java.util.Collection<?> collection)
-
appendArray
public void appendArray(java.lang.Appendable buffer, java.lang.Object array)
-
appendBoolean
public void appendBoolean(java.lang.Appendable buffer, java.lang.Boolean b)
-
appendNumber
public void appendNumber(java.lang.Appendable buffer, java.lang.Number number)
-
appendString
public void appendString(java.lang.Appendable buffer, java.lang.String string)
-
newMap
protected java.util.Map<java.lang.String,java.lang.Object> newMap()
Factory method that creates a Map when a JSON representation of
{...}is parsed.- Returns:
- a new Map representing the JSON object
-
newArray
@Deprecated protected java.lang.Object[] newArray(int size)
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
protected JSON 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
protected JSON contextFor(java.lang.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.- Parameters:
field- the field name- Returns:
- a JSON instance to parse the object field
-
convertTo
protected java.lang.Object convertTo(java.lang.Class<?> type, java.util.Map<java.lang.String,java.lang.Object> map)
-
addConvertor
public void addConvertor(java.lang.Class<?> forClass, JSON.Convertor convertor)Registers a
JSON.Convertorfor the given class.- Parameters:
forClass- the class the convertor applies toconvertor- the convertor for the class
-
addConvertorFor
public void addConvertorFor(java.lang.String name, JSON.Convertor convertor)Registers a
JSON.Convertorfor a named class.- Parameters:
name- the name of the class the convertor applies toconvertor- the convertor for the class
-
removeConvertor
public JSON.Convertor removeConvertor(java.lang.Class<?> forClass)
Unregisters a
JSON.Convertorfor a class.- Parameters:
forClass- the class the convertor applies to- Returns:
- the convertor for the class
-
removeConvertorFor
public JSON.Convertor removeConvertorFor(java.lang.String name)
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
protected JSON.Convertor getConvertor(java.lang.Class<?> forClass)
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
public JSON.Convertor getConvertorFor(java.lang.String name)
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
public java.util.function.Function<java.util.List<?>,java.lang.Object> getArrayConverter()
- Returns:
- the function to customize the Java representation of JSON arrays
- See Also:
setArrayConverter(Function)
-
setArrayConverter
public void setArrayConverter(java.util.function.Function<java.util.List<?>,java.lang.Object> arrayConverter)
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:
getArrayConverter()
-
parse
public java.lang.Object parse(JSON.Source source, boolean stripOuterComment)
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
public java.lang.Object fromJSON(java.lang.String string)
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
public java.lang.Object fromJSON(java.io.Reader reader)
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
public java.lang.Object parse(JSON.Source source)
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
protected java.lang.Object handleUnknown(JSON.Source source, char c)
-
parseObject
protected java.lang.Object parseObject(JSON.Source source)
-
parseArray
protected java.lang.Object parseArray(JSON.Source source)
-
parseString
protected java.lang.String parseString(JSON.Source source)
-
parseNumber
public java.lang.Number parseNumber(JSON.Source source)
-
seekTo
protected void seekTo(char seek, JSON.Source source)
-
seekTo
protected char seekTo(java.lang.String seek, JSON.Source source)
-
complete
protected static void complete(java.lang.String seek, JSON.Source source)
-
-