public class AsyncJSON
extends java.lang.Object
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 Factory#putConvertor(String, Convertor).
| Modifier and Type | Class and Description |
|---|---|
static interface |
AsyncJSON.Context
The state of JSON parsing.
|
static class |
AsyncJSON.Factory
The factory that creates AsyncJSON instances.
|
| Constructor and Description |
|---|
AsyncJSON(AsyncJSON.Factory factory) |
| Modifier and Type | Method and Description |
|---|---|
<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. |
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 new List instance. |
protected java.lang.RuntimeException |
newInvalidJSON(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 new Map instance. |
boolean |
parse(byte[] bytes)
Feeds the parser with the given bytes chunk.
|
boolean |
parse(byte[] bytes,
int offset,
int length)
Feeds the parser with the given bytes chunk.
|
boolean |
parse(java.nio.ByteBuffer buffer)
Feeds the parser with the given buffer chunk.
|
public AsyncJSON(AsyncJSON.Factory factory)
public boolean parse(byte[] bytes)
Feeds the parser with the given bytes chunk.
bytes - the bytes to parsejava.lang.IllegalArgumentException - if the JSON is malformedpublic boolean parse(byte[] bytes,
int offset,
int length)
Feeds the parser with the given bytes chunk.
bytes - the bytes to parseoffset - the offset to start parsing fromlength - the number of bytes to parsejava.lang.IllegalArgumentException - if the JSON is malformedpublic boolean parse(java.nio.ByteBuffer buffer)
Feeds the parser with the given buffer chunk.
buffer - the buffer to parsejava.lang.IllegalArgumentException - if the JSON is malformedpublic <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.
R - the type the result is cast tojava.lang.IllegalArgumentException - if the JSON is malformedjava.lang.IllegalStateException - if the no JSON was passed to the parse() methodsprotected 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 new Map instance.
Subclasses may override to return a custom Map instance.
context - the parsing contextMap instanceprotected java.util.List<java.lang.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.
context - the parsing contextList instanceprotected java.lang.RuntimeException newInvalidJSON(java.nio.ByteBuffer buffer,
java.lang.String message)
Copyright © 1995–2021 Webtide. All rights reserved.