Skip to content

JsonParser Features

Tatu Saloranta edited this page Mar 29, 2016 · 10 revisions

Jackson Streaming: JsonParser.Feature

Jackson Streaming API has a set of on/off features that change the way streaming parsing is done. Features can be directly enabled/disabled on JsonParser instances, but more commonly default values are changed on JsonFactory instances. For example:

JsonFactory f = new JsonFactory();
f.enable(JsonParser.Feature.ALLOW_COMMENTS);
f.disable(JsonParser.Feature.ALLOW_SINGLE_QUOTES);
JsonParser p = f.createParser(jsonSource);
p.enable(JsonParser.Feature.STRICT_DUPLICATE_DETECTION);

General Notes

Although use of these features is shared by all data format modules (and despite many format modules specifying additional format-specific features), not all features listed here apply to all data formats. Sometimes choice of addition of a feature for specific format module, and generic generator feature is arbitrary. The intent is for all features that are likely to be needed by more than one format should be included here, to reduce/remove overlapping features with same semantics.

Features

Settings can be divided in couple of loose categories, as follows.

Low-level I/O handling features

  • AUTO_CLOSE_SOURCE (default: true)

Support for non-standard data format constructs

  • ALLOW_COMMENTS (default: false) (for textual formats with concept of comments)
    • For textual formats that do not have official comments, but for which "de facto" conventions exist (like JSON), determines whether use of such unofficial comments is allowed or not
    • Supported for: JSON
    • For JSON: enabling the feature allows recognition and handling of "C comments" (/* ... */) and "C++ comments" (// ....)
    • YAML supports its own "hash comments" regardless of this setting
  • ALLOW_YAML_COMMENTS (default: false)
    • Supported for: JSON (YAML allows by default)
    • For JSON: enabling the feature allows recognition and handling of "hash comments" (# .... )
  • ALLOW_UNQUOTED_FIELD_NAMES (default: false)
    • Supported for: JSON
  • ALLOW_SINGLE_QUOTES (default: false)
    • Supported for: JSON
  • ALLOW_UNQUOTED_CONTROL_CHARS (default: false)
    • Supported for: JSON
  • ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER (default: false)
    • Supported for: JSON
  • ALLOW_NUMERIC_LEADING_ZEROS (default: false)
    • Supported for: JSON
  • ALLOW_NON_NUMERIC_NUMBERS (default: false)
    • Supported for: JSON
  • ALLOW_MISSING_VALUES (default: false) (since 2.8)
    • Allow translation of "missing" values (white-space between two commas, in JSON Array context) into null values, instead of an exception
    • Supported for: JSON

Additional input validation

  • STRICT_DUPLICATE_DETECTION (default: false) (added in 2.3)

  • IGNORE_UNDEFINED (default: false) (added in 2.6)

    • With formats that require Schema for parsing (like Avro, Protobuf, CSV), determines what happens if decoded encounters content that Schema has no definition for: if disabled, exception is thrown; if enabled, such content is quietly ignored.
    • Supported for: Avro, CSV
    • Note: setting has no effect on Avro since it has no mechanism for recognizing such "unknown" content -- there is no way for decoder to ignore such content and instead a decoding error is thrown (or data corruption occurs)
Clone this wiki locally