Class JsonParser

All Implemented Interfaces:
JsonMetaProvider, ArrayRecordReadable, RecordReadable, TokenReadable
Direct Known Subclasses:
Json5Parser, JsonlParser

Parses any valid RFC 8259 JSON text into a POJO model.

This parser strictly enforces RFC 8259 JSON syntax. Double-quoted strings only; single-quoted strings, comments, trailing commas, and unquoted keys are rejected. For JSON5 syntax (single quotes, comments, etc.), use Json5Parser.

Media types

Handles Content-Type types: application/json, text/json, application/jcs+json

Description

This parser uses a state machine, which makes it very fast and efficient. It parses JSON in about 70% of the time that it takes the built-in Java DOM parsers to parse equivalent XML.

This parser handles all valid RFC 8259 JSON syntax.

Also handles negative, decimal, hexadecimal, octal, and double numbers, including exponential notation.

This parser handles the following input, and automatically returns the corresponding Java class.

  • JSON objects ("{...}") are converted to JsonMaps. Note: If a _type='xxx' attribute is specified on the object, then an attempt is made to convert the object to an instance of the specified Java bean class. See the MarshallingContext.Builder#typePropertyName(String) setting for more information about parsing beans from JSON.
  • JSON arrays ("[...]") are converted to JsonLists.
  • JSON string literals ("\"xyz\"") are converted to Strings.
  • JSON numbers ("123", including octal/hexadecimal/exponential notation) are converted to Integers, Longs, Floats, or Doubles depending on whether the number is decimal, and the size of the number.
  • JSON booleans ("false") are converted to Booleans.
  • JSON nulls ("null") are converted to null.

Input can be any of the following:

  • "{...}" - Converted to an JsonMap or an instance of a Java bean if a _type attribute is present.
  • "[...]" - Converted to an JsonList.
  • "123..." - Converted to a Number (either Integer, Long, Float, or Double).
  • "true"/"false" - Converted to a Boolean.
  • "null" - Returns null.
  • "\"xxx\"" - Converted to a String (double quotes only).

TIP: If you know you're parsing a JSON object or array, it can be easier to parse it using the JsonMap(CharSequence) or JsonList(CharSequence) constructors instead of using this class. The end result should be the same.

Example input (Map of name/age):

{"name":"Alice","age":30}

Complex (nested object + array):

{"name":"Alice","age":30,"address":{"street":"123 Main St","city":"Boston","state":"MA"},"tags":["a","b","c"]}

Notes:
  • This class is thread safe and reusable.
See Also: