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).

  • Field Details

  • Constructor Details

  • 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 parse
      offset - the offset to start parsing from
      length - the number of bytes to parse
      Returns:
      whether the JSON parsing was complete
      Throws:
      IllegalArgumentException - if the JSON is malformed
    • parse

      public boolean parse(ByteBuffer buffer)

      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 malformed
      IllegalStateException - if the no JSON was passed to the parse() methods
    • newObject

      protected Map<String,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.

      Parameters:
      context - the parsing context
      Returns:
      a Map instance
    • newArray

      protected List<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.

      Parameters:
      context - the parsing context
      Returns:
      a List instance
    • end

      private Object end()
    • reset

      private void reset()
    • parseAny

      private boolean parseAny(ByteBuffer buffer)
    • parseNull

      private boolean parseNull(ByteBuffer buffer)
    • parseNullCharacter

      private void parseNullCharacter(ByteBuffer buffer, int index)
    • parseTrue

      private boolean parseTrue(ByteBuffer buffer)
    • parseTrueCharacter

      private void parseTrueCharacter(ByteBuffer buffer, int index)
    • parseFalse

      private boolean parseFalse(ByteBuffer buffer)
    • parseFalseCharacter

      private void parseFalseCharacter(ByteBuffer buffer, int index)
    • parseNumber

      private boolean parseNumber(ByteBuffer buffer)
    • parseString

      private boolean parseString(ByteBuffer buffer)
    • parseEscape

      private boolean parseEscape(ByteBuffer buffer)
    • parseEscapeCharacter

      private boolean parseEscapeCharacter(char escape)
    • parseUnicode

      private boolean parseUnicode(ByteBuffer buffer)
    • hexToByte

      private byte hexToByte(ByteBuffer buffer, byte currentByte)
    • parseArray

      private boolean parseArray(ByteBuffer buffer)
    • parseObject

      private boolean parseObject(ByteBuffer buffer)
    • parseObjectField

      private boolean parseObjectField(ByteBuffer buffer)
    • parseObjectFieldName

      private boolean parseObjectFieldName(ByteBuffer buffer)
    • parseObjectFieldValue

      private boolean parseObjectFieldValue(ByteBuffer buffer)
    • convertObject

      private Object convertObject(Map<String,Object> object)
    • convertObject

      private Object convertObject(String fieldName, Map<String,Object> object)
    • toConvertible

      private JSON.Convertible toConvertible(String className)
    • newInvalidJSON

      protected RuntimeException newInvalidJSON(ByteBuffer buffer, String message)
    • isWhitespace

      private static boolean isWhitespace(byte ws)