Class JSON
- java.lang.Object
-
- org.eclipse.jetty.util.ajax.JSON
-
public class JSON extends java.lang.Object
JSON Parser and Generator.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 --> 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 interfaceJSON.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 thetoString(Object)
method will useJSON.Generator.addJSON(Appendable)
to generate the JSON. The classJSON.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 withregisterConvertor(Class, Convertor)
. These converters are looked up by class, interface and super class bygetConvertor(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 aJSON.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 Classes Modifier and Type Class 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 ofJSON.Generator
that holds a pre-generated string on JSON text.static interface
JSON.Output
JSON Output class for use byJSON.Convertible
.static class
JSON.ReaderSource
static interface
JSON.Source
static class
JSON.StringSource
-
Constructor Summary
Constructors Constructor Description JSON()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description void
addConvertor(java.lang.Class forClass, JSON.Convertor convertor)
Register aJSON.Convertor
for a class or interface.void
addConvertorFor(java.lang.String name, JSON.Convertor convertor)
Register aJSON.Convertor
for a named class or interface.void
append(java.lang.Appendable buffer, java.lang.Object object)
Append object as JSON to string buffer.void
append(java.lang.StringBuffer buffer, java.lang.Object object)
Deprecated.void
appendArray(java.lang.Appendable buffer, java.lang.Object array)
void
appendArray(java.lang.Appendable buffer, java.util.Collection collection)
void
appendArray(java.lang.StringBuffer buffer, java.lang.Object array)
Deprecated.void
appendArray(java.lang.StringBuffer buffer, java.util.Collection collection)
Deprecated.void
appendBoolean(java.lang.Appendable buffer, java.lang.Boolean b)
void
appendBoolean(java.lang.StringBuffer buffer, java.lang.Boolean b)
Deprecated.void
appendJSON(java.lang.Appendable buffer, JSON.Convertible converter)
void
appendJSON(java.lang.Appendable buffer, JSON.Convertor convertor, java.lang.Object object)
void
appendJSON(java.lang.Appendable buffer, JSON.Generator generator)
void
appendJSON(java.lang.StringBuffer buffer, JSON.Convertible converter)
Deprecated.void
appendJSON(java.lang.StringBuffer buffer, JSON.Convertor convertor, java.lang.Object object)
Deprecated.void
appendJSON(java.lang.StringBuffer buffer, JSON.Generator generator)
Deprecated.void
appendMap(java.lang.Appendable buffer, java.util.Map<?,?> map)
void
appendMap(java.lang.StringBuffer buffer, java.util.Map<?,?> map)
Deprecated.void
appendNull(java.lang.Appendable buffer)
void
appendNull(java.lang.StringBuffer buffer)
Deprecated.void
appendNumber(java.lang.Appendable buffer, java.lang.Number number)
void
appendNumber(java.lang.StringBuffer buffer, java.lang.Number number)
Deprecated.void
appendString(java.lang.Appendable buffer, java.lang.String string)
void
appendString(java.lang.StringBuffer buffer, java.lang.String string)
Deprecated.protected static void
complete(java.lang.String seek, JSON.Source source)
protected JSON
contextFor(java.lang.String field)
protected JSON
contextForArray()
protected java.lang.Object
convertTo(java.lang.Class type, java.util.Map map)
void
escapeString(java.lang.Appendable buffer, java.lang.String input)
protected void
escapeUnicode(java.lang.Appendable buffer, char c)
Per spec, unicode characters are by default NOT escaped.java.lang.Object
fromJSON(java.lang.String json)
Convert JSON to Objectjava.util.function.Function<java.util.List<?>,java.lang.Object>
getArrayConverter()
protected JSON.Convertor
getConvertor(java.lang.Class forClass)
Lookup a convertor for a class.JSON.Convertor
getConvertorFor(java.lang.String name)
Lookup a convertor for a named class.static JSON
getDefault()
int
getStringBufferSize()
protected java.lang.Object
handleUnknown(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()
static java.lang.Object
parse(java.io.InputStream in)
Deprecated.useparse(Reader)
static java.lang.Object
parse(java.io.InputStream in, boolean stripOuterComment)
Deprecated.static java.lang.Object
parse(java.io.Reader in)
static java.lang.Object
parse(java.io.Reader in, boolean stripOuterComment)
static java.lang.Object
parse(java.lang.String s)
static java.lang.Object
parse(java.lang.String s, boolean stripOuterComment)
java.lang.Object
parse(JSON.Source source)
java.lang.Object
parse(JSON.Source source, boolean stripOuterComment)
protected java.lang.Object
parseArray(JSON.Source source)
java.lang.Number
parseNumber(JSON.Source source)
protected java.lang.Object
parseObject(JSON.Source source)
protected java.lang.String
parseString(JSON.Source source)
static void
registerConvertor(java.lang.Class forClass, JSON.Convertor convertor)
Register aJSON.Convertor
for a class or interface.JSON.Convertor
removeConvertorFor(java.lang.String name)
Removes a registeredJSON.Convertor
for the given named class or interface.static void
reset()
Reset the default JSON behaviors to defaultprotected void
seekTo(char seek, JSON.Source source)
protected char
seekTo(java.lang.String seek, JSON.Source source)
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, aList<Object>
, to another Java data structure such as anObject[]
.static void
setDefault(JSON json)
Deprecated.void
setStringBufferSize(int stringBufferSize)
java.lang.String
toJSON(java.lang.Object object)
Convert Object to JSONprotected java.lang.String
toString(char[] buffer, int offset, int length)
static java.lang.String
toString(java.lang.Object object)
static java.lang.String
toString(java.lang.Object[] array)
static java.lang.String
toString(java.util.Map object)
-
-
-
Field Detail
-
DEFAULT
public static final JSON DEFAULT
-
-
Method Detail
-
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
public static void registerConvertor(java.lang.Class forClass, JSON.Convertor convertor)
Register aJSON.Convertor
for a class or interface.- Parameters:
forClass
- The class or interface that the convertor applies toconvertor
- the convertor
-
getDefault
public static JSON getDefault()
-
setDefault
@Deprecated public static void setDefault(JSON json)
Deprecated.
-
toString
public static java.lang.String toString(java.lang.Object object)
-
toString
public static java.lang.String toString(java.util.Map object)
-
toString
public static java.lang.String toString(java.lang.Object[] array)
-
parse
public static java.lang.Object parse(java.lang.String s)
- Parameters:
s
- String containing JSON object or array.- Returns:
- A Map, Object array or primitive array parsed from the JSON.
-
parse
public static java.lang.Object parse(java.lang.String s, boolean stripOuterComment)
- 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
public static java.lang.Object parse(java.io.Reader in) throws java.io.IOException
- Parameters:
in
- Reader containing JSON object or array.- Returns:
- A Map, Object array or primitive array parsed from the JSON.
- Throws:
java.io.IOException
- if unable to parse
-
parse
public static java.lang.Object parse(java.io.Reader in, boolean stripOuterComment) throws java.io.IOException
- 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:
java.io.IOException
- if unable to parse
-
parse
@Deprecated public static java.lang.Object parse(java.io.InputStream in) throws java.io.IOException
Deprecated.useparse(Reader)
- Parameters:
in
- Reader containing JSON object or array.- Returns:
- A Map, Object array or primitive array parsed from the JSON.
- Throws:
java.io.IOException
- if unable to parse
-
parse
@Deprecated public static java.lang.Object parse(java.io.InputStream in, boolean stripOuterComment) throws java.io.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:
java.io.IOException
- if unable to parse
-
escapeString
public void escapeString(java.lang.Appendable buffer, java.lang.String input) throws java.io.IOException
- Throws:
java.io.IOException
-
escapeUnicode
protected void escapeUnicode(java.lang.Appendable buffer, char c) throws java.io.IOException
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:
java.io.IOException
-
toJSON
public java.lang.String toJSON(java.lang.Object object)
Convert Object to JSON- Parameters:
object
- The object to convert- Returns:
- The JSON String
-
fromJSON
public java.lang.Object fromJSON(java.lang.String json)
Convert JSON to Object- Parameters:
json
- The json to convert- Returns:
- The object
-
append
@Deprecated public void append(java.lang.StringBuffer buffer, java.lang.Object object)
Deprecated.
-
append
public void append(java.lang.Appendable buffer, java.lang.Object object)
Append object as JSON to string buffer.- Parameters:
buffer
- the buffer to append toobject
- the object to append
-
appendNull
@Deprecated public void appendNull(java.lang.StringBuffer buffer)
Deprecated.
-
appendNull
public void appendNull(java.lang.Appendable buffer)
-
appendJSON
@Deprecated public void appendJSON(java.lang.StringBuffer buffer, JSON.Convertor convertor, java.lang.Object object)
Deprecated.
-
appendJSON
public void appendJSON(java.lang.Appendable buffer, JSON.Convertor convertor, java.lang.Object object)
-
appendJSON
@Deprecated public void appendJSON(java.lang.StringBuffer buffer, JSON.Convertible converter)
Deprecated.
-
appendJSON
public void appendJSON(java.lang.Appendable buffer, JSON.Convertible converter)
-
appendJSON
@Deprecated public void appendJSON(java.lang.StringBuffer buffer, JSON.Generator generator)
Deprecated.
-
appendJSON
public void appendJSON(java.lang.Appendable buffer, JSON.Generator generator)
-
appendMap
@Deprecated public void appendMap(java.lang.StringBuffer buffer, java.util.Map<?,?> map)
Deprecated.
-
appendMap
public void appendMap(java.lang.Appendable buffer, java.util.Map<?,?> map)
-
appendArray
@Deprecated public void appendArray(java.lang.StringBuffer buffer, java.util.Collection collection)
Deprecated.
-
appendArray
public void appendArray(java.lang.Appendable buffer, java.util.Collection collection)
-
appendArray
@Deprecated public void appendArray(java.lang.StringBuffer buffer, java.lang.Object array)
Deprecated.
-
appendArray
public void appendArray(java.lang.Appendable buffer, java.lang.Object array)
-
appendBoolean
@Deprecated public void appendBoolean(java.lang.StringBuffer buffer, java.lang.Boolean b)
Deprecated.
-
appendBoolean
public void appendBoolean(java.lang.Appendable buffer, java.lang.Boolean b)
-
appendNumber
@Deprecated public void appendNumber(java.lang.StringBuffer buffer, java.lang.Number number)
Deprecated.
-
appendNumber
public void appendNumber(java.lang.Appendable buffer, java.lang.Number number)
-
appendString
@Deprecated public void appendString(java.lang.StringBuffer buffer, java.lang.String string)
Deprecated.
-
appendString
public void appendString(java.lang.Appendable buffer, java.lang.String string)
-
toString
protected java.lang.String toString(char[] buffer, int offset, int length)
-
newMap
protected java.util.Map<java.lang.String,java.lang.Object> newMap()
-
newArray
@Deprecated protected java.lang.Object[] newArray(int size)
Deprecated.usesetArrayConverter(Function)
instead.- Parameters:
size
- the size of the array- Returns:
- a new array
-
contextForArray
protected JSON contextForArray()
-
contextFor
protected JSON contextFor(java.lang.String field)
-
convertTo
protected java.lang.Object convertTo(java.lang.Class type, java.util.Map map)
-
addConvertor
public void addConvertor(java.lang.Class forClass, JSON.Convertor convertor)
Register aJSON.Convertor
for a class or interface.- Parameters:
forClass
- The class or interface that the convertor applies toconvertor
- the convertor
-
getConvertor
protected JSON.Convertor getConvertor(java.lang.Class forClass)
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.Convertor
or null if none were found.
-
addConvertorFor
public void addConvertorFor(java.lang.String name, JSON.Convertor convertor)
Register aJSON.Convertor
for a named class or interface.- Parameters:
name
- name of a class or an interface that the convertor applies toconvertor
- the convertor
-
removeConvertorFor
public JSON.Convertor removeConvertorFor(java.lang.String name)
Removes a registeredJSON.Convertor
for the given named class or interface.- Parameters:
name
- name of a class or an interface for a registeredJSON.Convertor
- Returns:
- the
JSON.Convertor
that was removed, or null
-
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()
-
getConvertorFor
public JSON.Convertor getConvertorFor(java.lang.String name)
Lookup a convertor for a named class.- Parameters:
name
- name of the class- Returns:
- a
JSON.Convertor
or null if none were found.
-
parse
public java.lang.Object parse(JSON.Source source, boolean stripOuterComment)
-
parse
public java.lang.Object parse(JSON.Source source)
-
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)
-
-