Class AsyncJSON
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 JSON.Convertible,
or be mapped with a JSON.Convertor via AsyncJSON.Factory.putConvertor(String, Convertor).
JSON arrays are by default represented with a List<Object>, but the
Java representation can be customized via AsyncJSON.Factory.setArrayConverter(Function).
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceThe state of JSON parsing.static classThe factory that creates AsyncJSON instances.private static classprivate static classprivate static classprivate static enum -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate List<ByteBuffer> private final AsyncJSON.Factoryprivate final AsyncJSON.NumberBuilderprivate final AsyncJSON.FrameStackprivate final Utf8StringBuilderprivate static final Object -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription<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 ObjectconvertArray(List<?> array) private ObjectconvertObject(String fieldName, Map<String, Object> object) private ObjectconvertObject(Map<String, Object> object) private Objectend()private bytehexToByte(ByteBuffer buffer, byte currentByte) (package private) booleanisEmpty()private static booleanisWhitespace(byte ws) newArray(AsyncJSON.Context context) When a JSON[is encountered during parsing, this method is called to create a newListinstance.protected RuntimeExceptionnewInvalidJSON(ByteBuffer buffer, String message) 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(ByteBuffer buffer) Feeds the parser with the given buffer chunk.private booleanparseAny(ByteBuffer buffer) private booleanparseArray(ByteBuffer buffer) private booleanparseEscape(ByteBuffer buffer) private booleanparseEscapeCharacter(char escape) private booleanparseFalse(ByteBuffer buffer) private voidparseFalseCharacter(ByteBuffer buffer, int index) private booleanparseNull(ByteBuffer buffer) private voidparseNullCharacter(ByteBuffer buffer, int index) private booleanparseNumber(ByteBuffer buffer) private booleanparseObject(ByteBuffer buffer) private booleanparseObjectField(ByteBuffer buffer) private booleanparseObjectFieldName(ByteBuffer buffer) private booleanparseObjectFieldValue(ByteBuffer buffer) private booleanparseString(ByteBuffer buffer) private booleanparseTrue(ByteBuffer buffer) private voidparseTrueCharacter(ByteBuffer buffer, int index) private booleanparseUnicode(ByteBuffer buffer) private voidreset()private JSON.ConvertibletoConvertible(String className)
-
Field Details
-
UNSET
-
stack
-
numberBuilder
-
stringBuilder
-
factory
-
chunks
-
-
Constructor Details
-
AsyncJSON
-
-
Method Details
-
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:
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:
IllegalArgumentException- if the JSON is malformed
-
parse
Feeds the parser with the given buffer chunk.
- Parameters:
buffer- the buffer to parse- Returns:
- whether the JSON parsing was complete
- Throws:
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:
IllegalArgumentException- if the JSON is malformedIllegalStateException- if the no JSON was passed to theparse()methods
-
newObject
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
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
-
reset
private void reset() -
parseAny
-
parseNull
-
parseNullCharacter
-
parseTrue
-
parseTrueCharacter
-
parseFalse
-
parseFalseCharacter
-
parseNumber
-
parseString
-
parseEscape
-
parseEscapeCharacter
private boolean parseEscapeCharacter(char escape) -
parseUnicode
-
hexToByte
-
parseArray
-
parseObject
-
parseObjectField
-
parseObjectFieldName
-
parseObjectFieldValue
-
convertArray
-
convertObject
-
convertObject
-
toConvertible
-
newInvalidJSON
-
isWhitespace
private static boolean isWhitespace(byte ws)
-