Class AsyncJSON

java.lang.Object
org.eclipse.jetty.util.ajax.AsyncJSON

public class AsyncJSON extends 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 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).