Class AsyncJSON
- java.lang.Object
-
- org.eclipse.jetty.util.ajax.AsyncJSON
-
public class AsyncJSON extends java.lang.ObjectA 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.Personmust either implementJSON.Convertible, or be mapped with aJSON.ConvertorviaAsyncJSON.Factory.putConvertor(String, Convertor).JSON arrays are by default represented with a
List<Object>, but the Java representation can be customized viaAsyncJSON.Factory.setArrayConverter(Function).
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceAsyncJSON.ContextThe state of JSON parsing.static classAsyncJSON.FactoryThe factory that creates AsyncJSON instances.private static classAsyncJSON.Frameprivate static classAsyncJSON.FrameStackprivate static classAsyncJSON.NumberBuilderprivate static classAsyncJSON.State
-
Field Summary
Fields Modifier and Type Field Description private java.util.List<java.nio.ByteBuffer>chunksprivate AsyncJSON.Factoryfactoryprivate AsyncJSON.NumberBuildernumberBuilderprivate AsyncJSON.FrameStackstackprivate Utf8StringBuilderstringBuilderprivate static java.lang.ObjectUNSET
-
Constructor Summary
Constructors Constructor Description AsyncJSON(AsyncJSON.Factory factory)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description <R> Rcomplete()Signals to the parser that the parse data is complete, and returns the object parsed from the JSON chunks passed to theparse()methods.private java.lang.ObjectconvertArray(java.util.List<?> array)private java.lang.ObjectconvertObject(java.lang.String fieldName, java.util.Map<java.lang.String,java.lang.Object> object)private java.lang.ObjectconvertObject(java.util.Map<java.lang.String,java.lang.Object> object)private java.lang.Objectend()private bytehexToByte(java.nio.ByteBuffer buffer, byte currentByte)(package private) booleanisEmpty()private static booleanisWhitespace(byte ws)protected java.util.List<java.lang.Object>newArray(AsyncJSON.Context context)When a JSON[is encountered during parsing, this method is called to create a newListinstance.protected java.lang.RuntimeExceptionnewInvalidJSON(java.nio.ByteBuffer buffer, java.lang.String message)protected java.util.Map<java.lang.String,java.lang.Object>newObject(AsyncJSON.Context context)When a JSON{is encountered during parsing, this method is called to create a newMapinstance.booleanparse(byte[] bytes)Feeds the parser with the given bytes chunk.booleanparse(byte[] bytes, int offset, int length)Feeds the parser with the given bytes chunk.booleanparse(java.nio.ByteBuffer buffer)Feeds the parser with the given buffer chunk.private booleanparseAny(java.nio.ByteBuffer buffer)private booleanparseArray(java.nio.ByteBuffer buffer)private booleanparseEscape(java.nio.ByteBuffer buffer)private booleanparseEscapeCharacter(char escape)private booleanparseFalse(java.nio.ByteBuffer buffer)private voidparseFalseCharacter(java.nio.ByteBuffer buffer, int index)private booleanparseNull(java.nio.ByteBuffer buffer)private voidparseNullCharacter(java.nio.ByteBuffer buffer, int index)private booleanparseNumber(java.nio.ByteBuffer buffer)private booleanparseObject(java.nio.ByteBuffer buffer)private booleanparseObjectField(java.nio.ByteBuffer buffer)private booleanparseObjectFieldName(java.nio.ByteBuffer buffer)private booleanparseObjectFieldValue(java.nio.ByteBuffer buffer)private booleanparseString(java.nio.ByteBuffer buffer)private booleanparseTrue(java.nio.ByteBuffer buffer)private voidparseTrueCharacter(java.nio.ByteBuffer buffer, int index)private booleanparseUnicode(java.nio.ByteBuffer buffer)private voidreset()private JSON.ConvertibletoConvertible(java.lang.String className)
-
-
-
Field Detail
-
UNSET
private static final java.lang.Object UNSET
-
stack
private final AsyncJSON.FrameStack stack
-
numberBuilder
private final AsyncJSON.NumberBuilder numberBuilder
-
stringBuilder
private final Utf8StringBuilder stringBuilder
-
factory
private final AsyncJSON.Factory factory
-
chunks
private java.util.List<java.nio.ByteBuffer> chunks
-
-
Constructor Detail
-
AsyncJSON
public AsyncJSON(AsyncJSON.Factory factory)
-
-
Method Detail
-
isEmpty
boolean isEmpty()
-
parse
public boolean parse(byte[] bytes)
Feeds the parser with the given bytes chunk.
- Parameters:
bytes- the bytes to parse- Returns:
- whether the JSON parsing was complete
- Throws:
java.lang.IllegalArgumentException- if the JSON is malformed
-
parse
public boolean parse(byte[] bytes, int offset, int length)Feeds the parser with the given bytes chunk.
- Parameters:
bytes- the bytes to parseoffset- the offset to start parsing fromlength- the number of bytes to parse- Returns:
- whether the JSON parsing was complete
- Throws:
java.lang.IllegalArgumentException- if the JSON is malformed
-
parse
public boolean parse(java.nio.ByteBuffer buffer)
Feeds the parser with the given buffer chunk.
- Parameters:
buffer- the buffer to parse- Returns:
- whether the JSON parsing was complete
- Throws:
java.lang.IllegalArgumentException- if the JSON is malformed
-
complete
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.- Type Parameters:
R- the type the result is cast to- Returns:
- the result of the JSON parsing
- Throws:
java.lang.IllegalArgumentException- if the JSON is malformedjava.lang.IllegalStateException- if the no JSON was passed to theparse()methods
-
newObject
protected java.util.Map<java.lang.String,java.lang.Object> newObject(AsyncJSON.Context context)
When a JSON
{is encountered during parsing, this method is called to create a newMapinstance.Subclasses may override to return a custom
Mapinstance.- Parameters:
context- the parsing context- Returns:
- a
Mapinstance
-
newArray
protected java.util.List<java.lang.Object> newArray(AsyncJSON.Context context)
When a JSON
[is encountered during parsing, this method is called to create a newListinstance.Subclasses may override to return a custom
Listinstance.- Parameters:
context- the parsing context- Returns:
- a
Listinstance
-
end
private java.lang.Object end()
-
reset
private void reset()
-
parseAny
private boolean parseAny(java.nio.ByteBuffer buffer)
-
parseNull
private boolean parseNull(java.nio.ByteBuffer buffer)
-
parseNullCharacter
private void parseNullCharacter(java.nio.ByteBuffer buffer, int index)
-
parseTrue
private boolean parseTrue(java.nio.ByteBuffer buffer)
-
parseTrueCharacter
private void parseTrueCharacter(java.nio.ByteBuffer buffer, int index)
-
parseFalse
private boolean parseFalse(java.nio.ByteBuffer buffer)
-
parseFalseCharacter
private void parseFalseCharacter(java.nio.ByteBuffer buffer, int index)
-
parseNumber
private boolean parseNumber(java.nio.ByteBuffer buffer)
-
parseString
private boolean parseString(java.nio.ByteBuffer buffer)
-
parseEscape
private boolean parseEscape(java.nio.ByteBuffer buffer)
-
parseEscapeCharacter
private boolean parseEscapeCharacter(char escape)
-
parseUnicode
private boolean parseUnicode(java.nio.ByteBuffer buffer)
-
hexToByte
private byte hexToByte(java.nio.ByteBuffer buffer, byte currentByte)
-
parseArray
private boolean parseArray(java.nio.ByteBuffer buffer)
-
parseObject
private boolean parseObject(java.nio.ByteBuffer buffer)
-
parseObjectField
private boolean parseObjectField(java.nio.ByteBuffer buffer)
-
parseObjectFieldName
private boolean parseObjectFieldName(java.nio.ByteBuffer buffer)
-
parseObjectFieldValue
private boolean parseObjectFieldValue(java.nio.ByteBuffer buffer)
-
convertArray
private java.lang.Object convertArray(java.util.List<?> array)
-
convertObject
private java.lang.Object convertObject(java.util.Map<java.lang.String,java.lang.Object> object)
-
convertObject
private java.lang.Object convertObject(java.lang.String fieldName, java.util.Map<java.lang.String,java.lang.Object> object)
-
toConvertible
private JSON.Convertible toConvertible(java.lang.String className)
-
newInvalidJSON
protected java.lang.RuntimeException newInvalidJSON(java.nio.ByteBuffer buffer, java.lang.String message)
-
isWhitespace
private static boolean isWhitespace(byte ws)
-
-