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

public Class AsyncJSON

extends Object
Class Inheritance
Imports
java.nio.ByteBuffer, java.util.ArrayList, .Arrays, .HashMap, .List, .Map, .Objects, java.util.concurrent.ConcurrentHashMap, java.util.function.Function, org.eclipse.jetty.util.BufferUtil, .Index, .Loader, .TypeUtil, .Utf8StringBuilder, org.eclipse.jetty.util.ajax.JSON.Convertible, .JSON.Convertor

A non-blocking JSON parser that can parse partial JSON strings.

Usage:

AsyncJSON parser = new AsyncJSON.Factory().newAsyncJSON();

// Feed the parser with partial JSON string content.
parser.parse(chunk1);
parser.parse(chunk2);

// Tell the parser that the JSON string content
// is terminated and get the JSON object back.
Map<String, Object> object = parser.complete();

After the call to complete() the parser can be reused to parse another JSON string.

Custom objects can be created by specifying a "class" or "x-class" field:

String json = """
{
  "x-class": "com.acme.Person",
  "firstName": "John",
  "lastName": "Doe",
  "age": 42
}
"""

parser.parse(json);
com.acme.Person person = parser.complete();

Class com.acme.Person must either implement Convertible, or be mapped with a Convertor via Factory#putConvertor(String, Convertor).

JSON arrays are by default represented with a List<Object>, but the Java representation can be customized via Factory#setArrayConverter(Function).

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static interface
AsyncJSON.Context

The state of JSON parsing.

public static class
AsyncJSON.Factory

The factory that creates AsyncJSON instances.

private static class
private static class
private static class
private static enum

Field Summary

Modifier and TypeField and Description
private List<ByteBuffer>
private final AsyncJSON.Factory
private final AsyncJSON.NumberBuilder
private final AsyncJSON.FrameStack
private final Utf8StringBuilder
private static final Object

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public <
the type the result is cast to
R
>
R

Returns:

the result of the JSON parsing
complete
()

Signals to the parser that the parse data is complete, and returns the object parsed from the JSON chunks passed to the parse() methods.

private Object
convertArray(List<?> array)

private Object
private Object
convertObject(String fieldName, Map<String, Object> object)

private Object
end()

private byte
hexToByte(ByteBuffer buffer, byte currentByte)

pack-priv boolean
private static boolean
isWhitespace(byte ws)

protected List<Object>

Returns:

a List instance
newArray
(AsyncJSON.Context
the parsing context
context
)

When a JSON [ is encountered during parsing, this method is called to create a new List instance.

protected RuntimeException
newInvalidJSON(ByteBuffer buffer, String message)

protected Map<String, Object>

Returns:

a Map instance
newObject
(AsyncJSON.Context
the parsing context
context
)

When a JSON { is encountered during parsing, this method is called to create a new Map instance.

public boolean

Returns:

whether the JSON parsing was complete
parse
(byte[]
the bytes to parse
bytes
)

Feeds the parser with the given bytes chunk.

public boolean

Returns:

whether the JSON parsing was complete
parse
(byte[]
the bytes to parse
bytes
,
int
the offset to start parsing from
offset
,
int
the number of bytes to parse
length
)

Feeds the parser with the given bytes chunk.

public boolean

Returns:

whether the JSON parsing was complete
parse
(ByteBuffer
the buffer to parse
buffer
)

Feeds the parser with the given buffer chunk.

private boolean
private boolean
private boolean
private boolean
parseEscapeCharacter(char escape)

private boolean
private void
parseFalseCharacter(ByteBuffer buffer, int index)

private boolean
private void
parseNullCharacter(ByteBuffer buffer, int index)

private boolean
private boolean
private boolean
private boolean
private boolean
private boolean
private boolean
private void
parseTrueCharacter(ByteBuffer buffer, int index)

private boolean
private void
private JSON.Convertible
toConvertible(String className)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

chunksback to summary
private List<ByteBuffer> chunks
factoryback to summary
private final AsyncJSON.Factory factory
numberBuilderback to summary
private final AsyncJSON.NumberBuilder numberBuilder
stackback to summary
private final AsyncJSON.FrameStack stack
stringBuilderback to summary
private final Utf8StringBuilder stringBuilder
UNSETback to summary
private static final Object UNSET

Constructor Detail

AsyncJSONback to summary
public AsyncJSON(AsyncJSON.Factory factory)

Method Detail

completeback to summary
public <R> R complete()

Signals to the parser that the parse data is complete, and returns the object parsed from the JSON chunks passed to the parse() methods.

Parameters
<R>
the type the result is cast to
Returns:R

the result of the JSON parsing

Exceptions
IllegalArgumentException:
if the JSON is malformed
IllegalStateException:
if the no JSON was passed to the parse() methods
convertArrayback to summary
private Object convertArray(List<?> array)
convertObjectback to summary
private Object convertObject(Map<String, Object> object)
convertObjectback to summary
private Object convertObject(String fieldName, Map<String, Object> object)
endback to summary
private Object end()
hexToByteback to summary
private byte hexToByte(ByteBuffer buffer, byte currentByte)
isEmptyback to summary
pack-priv boolean isEmpty()
isWhitespaceback to summary
private static boolean isWhitespace(byte ws)
newArrayback to summary
protected List<Object> newArray(AsyncJSON.Context context)

When a JSON [ is encountered during parsing, this method is called to create a new List instance.

Subclasses may override to return a custom List instance.

Parameters
context:AsyncJSON.Context

the parsing context

Returns:List<Object>

a List instance

newInvalidJSONback to summary
protected RuntimeException newInvalidJSON(ByteBuffer buffer, String message)
newObjectback to summary
protected Map<String, Object> newObject(AsyncJSON.Context context)

When a JSON { is encountered during parsing, this method is called to create a new Map instance.

Subclasses may override to return a custom Map instance.

Parameters
context:AsyncJSON.Context

the parsing context

Returns:Map<String, Object>

a Map instance

parseback to summary
public boolean parse(byte[] bytes)

Feeds the parser with the given bytes chunk.

Parameters
bytes:byte[]

the bytes to parse

Returns:boolean

whether the JSON parsing was complete

Exceptions
IllegalArgumentException:
if the JSON is malformed
parseback to summary
public boolean parse(byte[] bytes, int offset, int length)

Feeds the parser with the given bytes chunk.

Parameters
bytes:byte[]

the bytes to parse

offset:int

the offset to start parsing from

length:int

the number of bytes to parse

Returns:boolean

whether the JSON parsing was complete

Exceptions
IllegalArgumentException:
if the JSON is malformed
parseback to summary
public boolean parse(ByteBuffer buffer)

Feeds the parser with the given buffer chunk.

Parameters
buffer:ByteBuffer

the buffer to parse

Returns:boolean

whether the JSON parsing was complete

Exceptions
IllegalArgumentException:
if the JSON is malformed
parseAnyback to summary
private boolean parseAny(ByteBuffer buffer)
parseArrayback to summary
private boolean parseArray(ByteBuffer buffer)
parseEscapeback to summary
private boolean parseEscape(ByteBuffer buffer)
parseEscapeCharacterback to summary
private boolean parseEscapeCharacter(char escape)
parseFalseback to summary
private boolean parseFalse(ByteBuffer buffer)
parseFalseCharacterback to summary
private void parseFalseCharacter(ByteBuffer buffer, int index)
parseNullback to summary
private boolean parseNull(ByteBuffer buffer)
parseNullCharacterback to summary
private void parseNullCharacter(ByteBuffer buffer, int index)
parseNumberback to summary
private boolean parseNumber(ByteBuffer buffer)
parseObjectback to summary
private boolean parseObject(ByteBuffer buffer)
parseObjectFieldback to summary
private boolean parseObjectField(ByteBuffer buffer)
parseObjectFieldNameback to summary
private boolean parseObjectFieldName(ByteBuffer buffer)
parseObjectFieldValueback to summary
private boolean parseObjectFieldValue(ByteBuffer buffer)
parseStringback to summary
private boolean parseString(ByteBuffer buffer)
parseTrueback to summary
private boolean parseTrue(ByteBuffer buffer)
parseTrueCharacterback to summary
private void parseTrueCharacter(ByteBuffer buffer, int index)
parseUnicodeback to summary
private boolean parseUnicode(ByteBuffer buffer)
resetback to summary
private void reset()
toConvertibleback to summary
private JSON.Convertible toConvertible(String className)
org.eclipse.jetty.util.ajax back to summary

public Interface AsyncJSON.Context

Known Direct Implementers
org.eclipse.jetty.util.ajax.AsyncJSON.FrameStack

The state of JSON parsing.

Method Summary

Modifier and TypeMethod and Description
public int
depth()

Returns: the depth in the JSON structure

Method Detail

depthback to summary
public int depth()
Returns:int

the depth in the JSON structure

org.eclipse.jetty.util.ajax back to summary

public Class AsyncJSON.Factory

extends Object
Class Inheritance

The factory that creates AsyncJSON instances.

The factory can be configured with custom Convertors, and with cached strings that will not be allocated if they can be looked up from the cache.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class

Field Summary

Modifier and TypeField and Description
private Function<List<?>, Object>
private Index.Mutable<AsyncJSON.Factory.CachedString>
private Map<String, JSON.Convertor>
private boolean

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public boolean
cache(String
the string to cache
value
)

Returns: whether the value can be cached

protected String

Returns:

a cached string or null
cached
(ByteBuffer
the buffer to lookup the string from
buffer
)

Attempts to return a cached string from the buffer bytes.

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

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

public JSON.Convertor

Returns:

the Convertor associated with the class name, or null
getConvertor
(String
the class name associated with the Convertor
className
)

Returns the Convertor associated with the given class name, if any.

public boolean
isDetailedParseException()

Returns: whether a parse failure should report the whole JSON string or just the last chunk

public AsyncJSON
newAsyncJSON()

Returns: a new parser instance

public void
putConvertor(String
the domain class name such as com.acme.Person
className
,
JSON.Convertor
the Convertor that converts Map to domain objects
convertor
)

Associates the given Convertor to the given class name.

public JSON.Convertor

Returns:

the Convertor associated with the class name, or null
removeConvertor
(String
the class name associated with the Convertor
className
)

Removes the Convertor associated with the given class name.

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
setDetailedParseException(boolean
whether a parse failure should report the whole JSON string or just the last chunk
detailedParseException
)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

arrayConverterback to summary
private Function<List<?>, Object> arrayConverter
cacheback to summary
private Index.Mutable<AsyncJSON.Factory.CachedString> cache
convertorsback to summary
private Map<String, JSON.Convertor> convertors
detailedParseExceptionback to summary
private boolean detailedParseException

Constructor Detail

Factoryback to summary
public Factory()

Method Detail

cacheback to summary
public boolean cache(String value)
Parameters
value:String

the string to cache

Returns:boolean

whether the value can be cached

cachedback to summary
protected String cached(ByteBuffer buffer)

Attempts to return a cached string from the buffer bytes.

In case of a cache hit, the string is returned and the buffer position updated.

In case of cache miss, null is returned and the buffer position is left unaltered.

Parameters
buffer:ByteBuffer

the buffer to lookup the string from

Returns:String

a cached string or null

getArrayConverterback to summary
public Function<List<?>, Object> getArrayConverter()
Returns:Function<List<?>, Object>

the function to customize the Java representation of JSON arrays

See Also
setArrayConverter(Function)
getConvertorback to summary
public JSON.Convertor getConvertor(String className)

Returns the Convertor associated with the given class name, if any.

Parameters
className:String

the class name associated with the Convertor

Returns:JSON.Convertor

the Convertor associated with the class name, or null

isDetailedParseExceptionback to summary
public boolean isDetailedParseException()
Returns:boolean

whether a parse failure should report the whole JSON string or just the last chunk

newAsyncJSONback to summary
public AsyncJSON newAsyncJSON()
Returns:AsyncJSON

a new parser instance

putConvertorback to summary
public void putConvertor(String className, JSON.Convertor convertor)

Associates the given Convertor to the given class name.

Parameters
className:String

the domain class name such as com.acme.Person

convertor:JSON.Convertor

the Convertor that converts Map to domain objects

removeConvertorback to summary
public JSON.Convertor removeConvertor(String className)

Removes the Convertor associated with the given class name.

Parameters
className:String

the class name associated with the Convertor

Returns:JSON.Convertor

the Convertor associated with the class name, or null

setArrayConverterback to summary
public void setArrayConverter(Function<List<?>, Object> 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[].

Parameters
arrayConverter:Function<List<?>, Object>

the function to customize the Java representation of JSON arrays

See Also
getArrayConverter()
setDetailedParseExceptionback to summary
public void setDetailedParseException(boolean detailedParseException)
Parameters
detailedParseException:boolean

whether a parse failure should report the whole JSON string or just the last chunk

org.eclipse.jetty.util.ajax back to summary

private Class AsyncJSON.Factory.CachedString

extends Object
Class Inheritance

Field Summary

Modifier and TypeField and Description
private final String
private final String

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
private boolean
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

encodedback to summary
private final String encoded
valueback to summary
private final String value

Constructor Detail

CachedStringback to summary
private CachedString(String value)

Method Detail

isCacheableback to summary
private boolean isCacheable()
org.eclipse.jetty.util.ajax back to summary

private Class AsyncJSON.Frame

extends Object
Class Inheritance

Field Summary

Modifier and TypeField and Description
private String
private AsyncJSON.State
private Object

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
private void
value(Object value)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

nameback to summary
private String name
stateback to summary
private AsyncJSON.State state
valueback to summary
private Object value

Constructor Detail

Frameback to summary
private Frame()

Method Detail

valueback to summary
private void value(Object value)
org.eclipse.jetty.util.ajax back to summary

private Class AsyncJSON.FrameStack

extends Object
implements Context
Class Inheritance
All Implemented Interfaces
org.eclipse.jetty.util.ajax.AsyncJSON.Context

Field Summary

Modifier and TypeField and Description
private int
private final List<AsyncJSON.Frame>

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
private void
public int
private void
grow(int grow)

private boolean
private AsyncJSON.Frame
peek()

private void
pop()

private void
push(AsyncJSON.State state, Object value)

private void
push(String name, AsyncJSON.State state, Object value)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

cursorback to summary
private int cursor
stackback to summary
private final List<AsyncJSON.Frame> stack

Constructor Detail

FrameStackback to summary
private FrameStack()

Method Detail

clearback to summary
private void clear()
depthback to summary
public int depth()

Implements org.eclipse.jetty.util.ajax.AsyncJSON.Context.depth.

Returns:int

Doc from org.eclipse.jetty.util.ajax.AsyncJSON.Context.depth.

the depth in the JSON structure

Annotations
@Override
growback to summary
private void grow(int grow)
isEmptyback to summary
private boolean isEmpty()
peekback to summary
private AsyncJSON.Frame peek()
popback to summary
private void pop()
pushback to summary
private void push(AsyncJSON.State state, Object value)
pushback to summary
private void push(String name, AsyncJSON.State state, Object value)
org.eclipse.jetty.util.ajax back to summary

private Class AsyncJSON.NumberBuilder

extends Object
Class Inheritance

Field Summary

Modifier and TypeField and Description
private StringBuilder
private int
private long

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
private boolean
appendAlpha(byte b)

private void
appendDigit(byte b)

private boolean
appendSign(byte b)

private void
private Number
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

builderback to summary
private StringBuilder builder
integerback to summary
private int integer
valueback to summary
private long value

Constructor Detail

NumberBuilderback to summary
private NumberBuilder()

Method Detail

appendAlphaback to summary
private boolean appendAlpha(byte b)
appendDigitback to summary
private void appendDigit(byte b)
appendSignback to summary
private boolean appendSign(byte b)
resetback to summary
private void reset()
valueback to summary
private Number value()
org.eclipse.jetty.util.ajax back to summary

private final Enum AsyncJSON.State

extends Enum<AsyncJSON.State>
Class Inheritance

Field Summary

Modifier and TypeField and Description
public static final AsyncJSON.State
public static final AsyncJSON.State
public static final AsyncJSON.State
public static final AsyncJSON.State
public static final AsyncJSON.State
public static final AsyncJSON.State
public static final AsyncJSON.State
public static final AsyncJSON.State
public static final AsyncJSON.State
public static final AsyncJSON.State
public static final AsyncJSON.State
public static final AsyncJSON.State
public static final AsyncJSON.State

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static AsyncJSON.State
public static AsyncJSON.State[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

ARRAYback to summary
public static final AsyncJSON.State ARRAY
COMPLETEback to summary
public static final AsyncJSON.State COMPLETE
ESCAPEback to summary
public static final AsyncJSON.State ESCAPE
FALSEback to summary
public static final AsyncJSON.State FALSE
NULLback to summary
public static final AsyncJSON.State NULL
NUMBERback to summary
public static final AsyncJSON.State NUMBER
OBJECTback to summary
public static final AsyncJSON.State OBJECT
OBJECT_FIELDback to summary
public static final AsyncJSON.State OBJECT_FIELD
OBJECT_FIELD_NAMEback to summary
public static final AsyncJSON.State OBJECT_FIELD_NAME
OBJECT_FIELD_VALUEback to summary
public static final AsyncJSON.State OBJECT_FIELD_VALUE
STRINGback to summary
public static final AsyncJSON.State STRING
TRUEback to summary
public static final AsyncJSON.State TRUE
UNICODEback to summary
public static final AsyncJSON.State UNICODE

Constructor Detail

Stateback to summary
private State()

Method Detail

valueOfback to summary
public static AsyncJSON.State valueOf(String name)
valuesback to summary
public static AsyncJSON.State[] values()