Class JSON
- java.lang.Object
-
- org.eclipse.jetty.util.ajax.JSON
-
public class JSON extends java.lang.ObjectJSON 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.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 thetoString(Object)method will useJSON.Generator.addJSON(Appendable)to generate the JSON. The classJSON.Literalmay be used to hold pre-generated JSON object.The interface
JSON.Convertormay 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.Convertorfor that class.If a JSON object has a "x-class" field then a direct lookup for a
JSON.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.ConvertorStatic JSON Convertor.static interfaceJSON.GeneratorJSON Generator.static classJSON.LiteralA Literal JSON generator A utility instance ofJSON.Generatorthat holds a pre-generated string on JSON text.static interfaceJSON.OutputJSON Output class for use byJSON.Convertible.static classJSON.ReaderSourcestatic interfaceJSON.Sourcestatic classJSON.StringSource
-
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)Register aJSON.Convertorfor a class or interface.voidaddConvertorFor(java.lang.String name, JSON.Convertor convertor)Register aJSON.Convertorfor a named class or interface.voidappend(java.lang.Appendable buffer, java.lang.Object object)Append object as JSON to string buffer.voidappend(java.lang.StringBuffer buffer, java.lang.Object object)Deprecated.voidappendArray(java.lang.Appendable buffer, java.lang.Object array)voidappendArray(java.lang.Appendable buffer, java.util.Collection collection)voidappendArray(java.lang.StringBuffer buffer, java.lang.Object array)Deprecated.voidappendArray(java.lang.StringBuffer buffer, java.util.Collection collection)Deprecated.voidappendBoolean(java.lang.Appendable buffer, java.lang.Boolean b)voidappendBoolean(java.lang.StringBuffer buffer, java.lang.Boolean b)Deprecated.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)voidappendJSON(java.lang.StringBuffer buffer, JSON.Convertible converter)Deprecated.voidappendJSON(java.lang.StringBuffer buffer, JSON.Convertor convertor, java.lang.Object object)Deprecated.voidappendJSON(java.lang.StringBuffer buffer, JSON.Generator generator)Deprecated.voidappendMap(java.lang.Appendable buffer, java.util.Map<?,?> map)voidappendMap(java.lang.StringBuffer buffer, java.util.Map<?,?> map)Deprecated.voidappendNull(java.lang.Appendable buffer)voidappendNull(java.lang.StringBuffer buffer)Deprecated.voidappendNumber(java.lang.Appendable buffer, java.lang.Number number)voidappendNumber(java.lang.StringBuffer buffer, java.lang.Number number)Deprecated.voidappendString(java.lang.Appendable buffer, java.lang.String string)voidappendString(java.lang.StringBuffer buffer, java.lang.String string)Deprecated.protected static voidcomplete(java.lang.String seek, JSON.Source source)protected JSONcontextFor(java.lang.String field)protected JSONcontextForArray()protected java.lang.ObjectconvertTo(java.lang.Class type, java.util.Map map)voidescapeString(java.lang.Appendable buffer, java.lang.String input)protected voidescapeUnicode(java.lang.Appendable buffer, char c)Per spec, unicode characters are by default NOT escaped.java.lang.ObjectfromJSON(java.lang.String json)Convert JSON to Objectjava.util.function.Function<java.util.List<?>,java.lang.Object>getArrayConverter()protected JSON.ConvertorgetConvertor(java.lang.Class forClass)Lookup a convertor for a class.JSON.ConvertorgetConvertorFor(java.lang.String name)Lookup a convertor for a named class.static JSONgetDefault()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()static java.lang.Objectparse(java.io.InputStream in)Deprecated.useparse(Reader)static java.lang.Objectparse(java.io.InputStream in, boolean stripOuterComment)Deprecated.static java.lang.Objectparse(java.io.Reader in)static java.lang.Objectparse(java.io.Reader in, boolean stripOuterComment)static java.lang.Objectparse(java.lang.String s)static java.lang.Objectparse(java.lang.String s, boolean stripOuterComment)java.lang.Objectparse(JSON.Source source)java.lang.Objectparse(JSON.Source source, boolean stripOuterComment)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)static voidregisterConvertor(java.lang.Class forClass, JSON.Convertor convertor)Register aJSON.Convertorfor a class or interface.JSON.ConvertorremoveConvertorFor(java.lang.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(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[].static voidsetDefault(JSON json)Deprecated.voidsetStringBufferSize(int stringBufferSize)java.lang.StringtoJSON(java.lang.Object object)Convert Object to JSONprotected java.lang.StringtoString(char[] buffer, int offset, int length)static java.lang.StringtoString(java.lang.Object object)static java.lang.StringtoString(java.lang.Object[] array)static java.lang.StringtoString(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.Convertorfor 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.IOExceptionDeprecated.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.IOExceptionDeprecated.- 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.IOExceptionPer 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.Convertorfor 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.Convertoror null if none were found.
-
addConvertorFor
public void addConvertorFor(java.lang.String name, JSON.Convertor convertor)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
public JSON.Convertor removeConvertorFor(java.lang.String name)
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
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.Convertoror 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)
-
-