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).
| Modifier and Type | Class 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 |
| Modifier and Type | Field and Description |
|---|---|
| private List | |
| private final AsyncJSON. | |
| private final AsyncJSON. | |
| private final AsyncJSON. | |
| private final Utf8StringBuilder | |
| private static final Object |
| Modifier and Type | Method and Description |
|---|---|
| public < the type the result is cast to R> R | Returns: the result of the JSON parsingSignals to the parser that the parse data is complete, and returns
the object parsed from the JSON chunks passed to the |
| private Object | |
| private Object | |
| private Object | |
| private Object | |
| private byte | |
| pack-priv boolean | |
| private static boolean | |
| protected List | |
| protected RuntimeException | |
| protected Map | |
| public boolean | Returns: whether the JSON parsing was completethe bytes to parse bytes)Feeds the parser with the given bytes chunk. |
| public boolean | Returns: whether the JSON parsing was completethe 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 completethe buffer to parse buffer)Feeds the parser with the given buffer chunk. |
| private boolean | |
| private boolean | |
| private boolean | |
| private boolean | |
| private boolean | |
| private void | |
| private boolean | |
| private void | |
| private boolean | |
| private boolean | |
| private boolean | |
| private boolean | |
| private boolean | |
| private boolean | |
| private boolean | |
| private void | |
| private boolean | |
| private void | |
| private JSON. |
| chunks | back to summary |
|---|---|
| private List<ByteBuffer> chunks | |
| factory | back to summary |
|---|---|
| private final AsyncJSON. | |
| numberBuilder | back to summary |
|---|---|
| private final AsyncJSON. | |
| stack | back to summary |
|---|---|
| private final AsyncJSON. | |
| stringBuilder | back to summary |
|---|---|
| private final Utf8StringBuilder stringBuilder | |
| UNSET | back to summary |
|---|---|
| private static final Object UNSET | |
| AsyncJSON | back to summary |
|---|---|
| public AsyncJSON(AsyncJSON. | |
| complete | back 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
| |
| convertArray | back to summary |
|---|---|
| private Object convertArray(List<?> array) | |
| convertObject | back to summary |
|---|---|
| private Object convertObject(Map<String, Object> object) | |
| convertObject | back to summary |
|---|---|
| private Object convertObject(String fieldName, Map<String, Object> object) | |
| end | back to summary |
|---|---|
| private Object end() | |
| hexToByte | back to summary |
|---|---|
| private byte hexToByte(ByteBuffer buffer, byte currentByte) | |
| isEmpty | back to summary |
|---|---|
| pack-priv boolean isEmpty() | |
| isWhitespace | back to summary |
|---|---|
| private static boolean isWhitespace(byte ws) | |
| newArray | back to summary |
|---|---|
| protected List When a JSON Subclasses may override to return a custom | |
| newInvalidJSON | back to summary |
|---|---|
| protected RuntimeException newInvalidJSON(ByteBuffer buffer, String message) | |
| newObject | back to summary |
|---|---|
| protected Map When a JSON Subclasses may override to return a custom | |
| parse | back to summary |
|---|---|
| public boolean parse(byte[] bytes) Feeds the parser with the given bytes chunk.
| |
| parse | back to summary |
|---|---|
| public boolean parse(byte[] bytes, int offset, int length) Feeds the parser with the given bytes chunk.
| |
| parse | back to summary |
|---|---|
| public boolean parse(ByteBuffer buffer) Feeds the parser with the given buffer chunk.
| |
| parseAny | back to summary |
|---|---|
| private boolean parseAny(ByteBuffer buffer) | |
| parseArray | back to summary |
|---|---|
| private boolean parseArray(ByteBuffer buffer) | |
| parseEscape | back to summary |
|---|---|
| private boolean parseEscape(ByteBuffer buffer) | |
| parseEscapeCharacter | back to summary |
|---|---|
| private boolean parseEscapeCharacter(char escape) | |
| parseFalse | back to summary |
|---|---|
| private boolean parseFalse(ByteBuffer buffer) | |
| parseFalseCharacter | back to summary |
|---|---|
| private void parseFalseCharacter(ByteBuffer buffer, int index) | |
| parseNull | back to summary |
|---|---|
| private boolean parseNull(ByteBuffer buffer) | |
| parseNullCharacter | back to summary |
|---|---|
| private void parseNullCharacter(ByteBuffer buffer, int index) | |
| parseNumber | back to summary |
|---|---|
| private boolean parseNumber(ByteBuffer buffer) | |
| parseObject | back to summary |
|---|---|
| private boolean parseObject(ByteBuffer buffer) | |
| parseObjectField | back to summary |
|---|---|
| private boolean parseObjectField(ByteBuffer buffer) | |
| parseObjectFieldName | back to summary |
|---|---|
| private boolean parseObjectFieldName(ByteBuffer buffer) | |
| parseObjectFieldValue | back to summary |
|---|---|
| private boolean parseObjectFieldValue(ByteBuffer buffer) | |
| parseString | back to summary |
|---|---|
| private boolean parseString(ByteBuffer buffer) | |
| parseTrue | back to summary |
|---|---|
| private boolean parseTrue(ByteBuffer buffer) | |
| parseTrueCharacter | back to summary |
|---|---|
| private void parseTrueCharacter(ByteBuffer buffer, int index) | |
| parseUnicode | back to summary |
|---|---|
| private boolean parseUnicode(ByteBuffer buffer) | |
| reset | back to summary |
|---|---|
| private void reset() | |
| toConvertible | back to summary |
|---|---|
| private JSON. | |
The state of JSON parsing.
| Modifier and Type | Method and Description |
|---|---|
| public int |
| depth | back to summary |
|---|---|
public int depth()
| |
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.
| Modifier and Type | Class and Description |
|---|---|
| private static class |
| Modifier and Type | Field and Description |
|---|---|
| private Function | |
| private Index. | |
| private Map | |
| private boolean |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public boolean | |
| protected String | Returns: a cached string ornullthe buffer to lookup the string from buffer)Attempts to return a cached string from the buffer bytes. |
| public Function | |
| public JSON. | getConvertor(String
the class name associated with the className)ConvertorReturns the |
| public boolean | Returns: whether a parse failure should report the whole JSON string or just the last chunk
|
| public AsyncJSON | |
| public void | |
| public JSON. | removeConvertor(String
the class name associated with the className)ConvertorRemoves the |
| 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 |
| public void | setDetailedParseException(boolean
whether a parse failure should report the whole JSON string or just the last chunk detailedParseException)
|
| arrayConverter | back to summary |
|---|---|
| private Function<List<?>, Object> arrayConverter | |
| cache | back to summary |
|---|---|
| private Index. | |
| convertors | back to summary |
|---|---|
| private Map<String, JSON. | |
| detailedParseException | back to summary |
|---|---|
| private boolean detailedParseException | |
| Factory | back to summary |
|---|---|
| public Factory() | |
| cache | back to summary |
|---|---|
public boolean cache(String value)
| |
| cached | back 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,
| |
| getArrayConverter | back to summary |
|---|---|
public Function
| |
| getConvertor | back to summary |
|---|---|
| public JSON. Returns the | |
| isDetailedParseException | back to summary |
|---|---|
public boolean isDetailedParseException()
| |
| newAsyncJSON | back to summary |
|---|---|
public AsyncJSON newAsyncJSON()
| |
| putConvertor | back to summary |
|---|---|
| public void putConvertor(String className, JSON. Associates the given | |
| removeConvertor | back to summary |
|---|---|
| public JSON. Removes the | |
| setArrayConverter | back to summary |
|---|---|
| public void setArrayConverter(Function<List<?>, Object> arrayConverter) Sets the function to convert JSON arrays from their default Java
representation, a
| |
| setDetailedParseException | back to summary |
|---|---|
public void setDetailedParseException(boolean detailedParseException)
| |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| private boolean |
| encoded | back to summary |
|---|---|
| private final String encoded | |
| value | back to summary |
|---|---|
| private final String value | |
| CachedString | back to summary |
|---|---|
| private CachedString(String value) | |
| isCacheable | back to summary |
|---|---|
| private boolean isCacheable() | |
| Modifier and Type | Field and Description |
|---|---|
| private String | |
| private AsyncJSON. | |
| private Object |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| private void |
| name | back to summary |
|---|---|
| private String name | |
| state | back to summary |
|---|---|
| private AsyncJSON. | |
| value | back to summary |
|---|---|
| private Object value | |
| Frame | back to summary |
|---|---|
| private Frame() | |
| value | back to summary |
|---|---|
| private void value(Object value) | |
| Modifier and Type | Field and Description |
|---|---|
| private int | |
| private final List |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| private void | |
| public int | |
| private void | |
| private boolean | |
| private AsyncJSON. | |
| private void | |
| private void | |
| private void |
| cursor | back to summary |
|---|---|
| private int cursor | |
| stack | back to summary |
|---|---|
| private final List<AsyncJSON. | |
| FrameStack | back to summary |
|---|---|
| private FrameStack() | |
| clear | back to summary |
|---|---|
| private void clear() | |
| depth | back to summary |
|---|---|
| public int depth() Implements org.
| |
| grow | back to summary |
|---|---|
| private void grow(int grow) | |
| isEmpty | back to summary |
|---|---|
| private boolean isEmpty() | |
| peek | back to summary |
|---|---|
| private AsyncJSON. | |
| pop | back to summary |
|---|---|
| private void pop() | |
| push | back to summary |
|---|---|
| private void push(AsyncJSON. | |
| push | back to summary |
|---|---|
| private void push(String name, AsyncJSON. | |
| Modifier and Type | Field and Description |
|---|---|
| private StringBuilder | |
| private int | |
| private long |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| private boolean | |
| private void | |
| private boolean | |
| private void | |
| private Number |
| builder | back to summary |
|---|---|
| private StringBuilder builder | |
| integer | back to summary |
|---|---|
| private int integer | |
| value | back to summary |
|---|---|
| private long value | |
| NumberBuilder | back to summary |
|---|---|
| private NumberBuilder() | |
| appendAlpha | back to summary |
|---|---|
| private boolean appendAlpha(byte b) | |
| appendDigit | back to summary |
|---|---|
| private void appendDigit(byte b) | |
| appendSign | back to summary |
|---|---|
| private boolean appendSign(byte b) | |
| reset | back to summary |
|---|---|
| private void reset() | |
| value | back to summary |
|---|---|
| private Number value() | |
| Modifier and Type | Field and Description |
|---|---|
| public static final AsyncJSON. | |
| public static final AsyncJSON. | |
| public static final AsyncJSON. | |
| public static final AsyncJSON. | |
| public static final AsyncJSON. | |
| public static final AsyncJSON. | |
| public static final AsyncJSON. | |
| public static final AsyncJSON. | |
| public static final AsyncJSON. | |
| public static final AsyncJSON. | |
| public static final AsyncJSON. | |
| public static final AsyncJSON. | |
| public static final AsyncJSON. |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public static AsyncJSON. | |
| public static AsyncJSON. |
| ARRAY | back to summary |
|---|---|
| public static final AsyncJSON. | |
| COMPLETE | back to summary |
|---|---|
| public static final AsyncJSON. | |
| ESCAPE | back to summary |
|---|---|
| public static final AsyncJSON. | |
| FALSE | back to summary |
|---|---|
| public static final AsyncJSON. | |
| NULL | back to summary |
|---|---|
| public static final AsyncJSON. | |
| NUMBER | back to summary |
|---|---|
| public static final AsyncJSON. | |
| OBJECT | back to summary |
|---|---|
| public static final AsyncJSON. | |
| OBJECT_FIELD | back to summary |
|---|---|
| public static final AsyncJSON. | |
| OBJECT_FIELD_NAME | back to summary |
|---|---|
| public static final AsyncJSON. | |
| OBJECT_FIELD_VALUE | back to summary |
|---|---|
| public static final AsyncJSON. | |
| STRING | back to summary |
|---|---|
| public static final AsyncJSON. | |
| TRUE | back to summary |
|---|---|
| public static final AsyncJSON. | |
| UNICODE | back to summary |
|---|---|
| public static final AsyncJSON. | |
| State | back to summary |
|---|---|
| private State() | |
| valueOf | back to summary |
|---|---|
| public static AsyncJSON. | |
| values | back to summary |
|---|---|
| public static AsyncJSON. | |