Top Description Inners Fields Constructors Methods
org.eclipse.jetty.util.ajax

public Class JSON

extends Object
Class Inheritance
Imports
java.io.Externalizable, .IOException, .Reader, java.lang.reflect.Array, java.util.ArrayList, .Collection, .Collections, .HashMap, .Iterator, .List, .Map, .Objects, java.util.concurrent.ConcurrentHashMap, java.util.function.Function, java.util.stream.IntStream, org.eclipse.jetty.util.Loader, .TypeUtil, org.slf4j.Logger, .LoggerFactory

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 and Inner Type Summary

Modifier and TypeClass and Description
private class
public static interface
JSON.Convertible

JSON Convertible object.

public static interface
JSON.Convertor

JSON Convertor.

public static interface
JSON.Generator

JSON Generator.

public static class
JSON.Literal

A Literal JSON generator.

public static interface
JSON.Output

JSON Output class for use by Convertible.

public static class
JSON.ReaderSource

A Reader source for a JSON string.

public static interface
JSON.Source

A generic source for a JSON representation.

public static class
JSON.StringSource

An in-memory source for a JSON string.

Field Summary

Modifier and TypeField and Description
private Function<List<?>, Object>
private final Map<String, JSON.Convertor>
private int
pack-priv static final Logger

Constructor Summary

AccessConstructor and Description
public
JSON()

Method Summary

Modifier and TypeMethod and Description
public void
addConvertor(Class<?>
the class the convertor applies to
forClass
,
JSON.Convertor
the convertor for the class
convertor
)

Registers a Convertor for the given class.

public void
addConvertorFor(String
the name of the class the convertor applies to
name
,
JSON.Convertor
the convertor for the class
convertor
)

Registers a JSON.Convertor for a named class.

public void
append(Appendable
the buffer to append to
buffer
,
Object
the object to convert to JSON
object
)

Appends the given object as JSON to string buffer.

public void
appendArray(Appendable buffer, Collection<?> collection)

public void
appendArray(Appendable buffer, Object array)

public void
public void
appendJSON(Appendable buffer, JSON.Convertor convertor, Object object)

public void
public void
appendJSON(Appendable buffer, JSON.Generator generator)

public void
appendMap(Appendable buffer, Map<?, ?> map)

public void
public void
appendNumber(Appendable buffer, Number number)

public void
appendString(Appendable buffer, String string)

protected static void
complete(String seek, JSON.Source source)

protected JSON

Returns:

a JSON instance to parse the object field
contextFor
(String
the field name
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

Returns:

a JSON instance to parse array items
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.

protected Object
convertTo(Class<?> type, Map<String, Object> map)

private Object
public void
escapeString(Appendable
the buffer to escape the string into
buffer
,
String
the string to escape
input
)

Escapes the characters of the given input string into the given buffer.

protected void
escapeUnicode(Appendable buffer, char c)

Per JSON specification, unicode characters are by default NOT escaped.

public Object

Returns:

the object constructed from the JSON string representation
fromJSON
(String
the JSON string to parse
string
)

Parses the given JSON string into an object.

public Object

Returns:

the object constructed from the JSON string representation
fromJSON
(Reader
the Reader to read the JSON from
reader
)

Parses the JSON from the given Reader into an object.

public Function<List<?>, Object>
getArrayConverter()

Returns: the function to customize the Java representation of JSON arrays

protected JSON.Convertor

Returns:

a JSON.Convertor or null if none was found for the class
getConvertor
(Class<?>
the class to look up the convertor
forClass
)

Looks up a convertor for a class.

public JSON.Convertor

Returns:

a JSON.Convertor or null if none were found.
getConvertorFor
(String
name of the class to look up the convertor
name
)

Looks up a convertor for a class name.

public int
getStringBufferSize()

Returns: the initial stringBuffer size to use when creating JSON strings (default 1024)

protected Object
handleUnknown(JSON.Source source, char c)

protected Object[]

Returns:

a new array representing the JSON array
newArray
(int
the size of the array
size
)
Deprecated use setArrayConverter(Function) instead.

Factory method that creates an array when a JSON representation of [...] is parsed.

protected Map<String, Object>

Returns:

a new Map representing the JSON object
newMap
()

Factory method that creates a Map when a JSON representation of {...} is parsed.

public Object

Returns:

the object constructed from the JSON string representation
parse
(JSON.Source
the JSON source to parse
source
,
boolean
whether to strip outer comments
stripOuterComment
)

Parses the given JSON source into an object.

public Object

Returns:

the object constructed from the JSON string representation
parse
(JSON.Source
the JSON source to parse
source
)

Parses the given JSON source into an object.

protected Object
public Number
protected Object
protected String
private void
public JSON.Convertor

Returns:

the convertor for the class
removeConvertor
(Class<?>
the class the convertor applies to
forClass
)

Unregisters a Convertor for a class.

public JSON.Convertor

Returns:

the convertor for the class
removeConvertorFor
(String
the name of the class the convertor applies to
name
)

Unregisters a Convertor for a named class.

protected void
seekTo(char seek, JSON.Source source)

protected char
seekTo(String seek, JSON.Source source)

public void
setArrayConverter(Function<List<?>, Object>
the function to customize the Java representation of JSON arrays
arrayConverter
)

Sets the function to convert JSON arrays from their default Java representation, a List<Object>, to another Java data structure such as an Object[].

public void
setStringBufferSize(int
the initial stringBuffer size to use when creating JSON strings (default 1024)
stringBufferSize
)

public String

Returns:

the JSON string representation of the object
toJSON
(Object
the object to convert
object
)

Converts any object to JSON.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait