Class JsonParser

All Implemented Interfaces:
AnnotationProvider, JsonMetaProvider
Direct Known Subclasses:
Json5Parser, JsonParser.Strict

public class JsonParser extends ReaderParser implements JsonMetaProvider
Parses any valid JSON text into a POJO model.
Media types

Handles Content-Type types: application/json, text/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 JSON syntax. In addition, when strict mode is disable, the parser also handles the following:

  • Javascript comments (both /* and //) are ignored.
  • Both single and double quoted strings.
  • Automatically joins concatenated strings (e.g. "aaa" + 'bbb').
  • Unquoted attributes and values.

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 BeanContext.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 consisting of only whitespace or JSON comments 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.
  • "\"xxx\"" - Converted to a String.
  • "'xxx' + \"yyy\"" - Converted to a concatenated String.

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.

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