Search code examples
javajsonjacksonjvmjson5

Can the Jackson parser be used to parse JSON5?


We'd like to parse JSON5 documents in the JVM, but the JSON5-specific libraries available on GitHub all appear to have very low support. As parsers are often magnets for security vulnerabilities, we'd prefer not to use a parser that isn't already being widely used and scrutinised by the community.

This got us to wondering: Can the optional features of the Jackson JSON parser be used to parse JSON5?


Solution

  • tl;dr

    By enabling the following optional Jackson parser features:

    • ALLOW_UNQUOTED_FIELD_NAMES
    • ALLOW_TRAILING_COMMA
    • ALLOW_SINGLE_QUOTES
    • ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER
    • ALLOW_NON_NUMERIC_NUMBERS
    • ALLOW_JAVA_COMMENTS
    • ALLOW_LEADING_DECIMAL_POINT_FOR_NUMBERS

    it seems possible to support all of JSON5's headline features except for:

    • hexadecimal numbers
    • trailing decimal points on numbers
    • plus signs before numbers
    • extra whitespace characters

    Detail

    Jackson has a number of optional features which can be enabled on a parser to make it more lenient in the way it parses.

    Supported Features

    Comparing these parser options to the headline features of JSON5, we find that the following are supported:

    Object keys may be an ECMAScript 5.1 IdentifierName.

    ✅ Supported with the ALLOW_UNQUOTED_FIELD_NAMES feature

    Objects may have a single trailing comma.

    Arrays may have a single trailing comma.

    ✅ Both supported with the ALLOW_TRAILING_COMMA feature

    Strings may be single quoted.

    ✅ Supported with the ALLOW_SINGLE_QUOTES feature

    Strings may span multiple lines by escaping new line characters.

    Strings may include character escapes.

    ✅ Both of these appear to be supported by Jackson's ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER feature, though the meaning or purpose of escaping here may differ subtly. DYOR.

    Numbers may be IEEE 754 positive infinity, negative infinity, and NaN.

    ✅ Supported with the ALLOW_NON_NUMERIC_NUMBERS feature

    Single and multi-line comments are allowed.

    ✅ Supported with the ALLOW_JAVA_COMMENTS feature.

    Partially Supported Features

    The following JSON5 feature is partially supported by Jackson:

    Numbers may have a leading or trailing decimal point.

    ✅ Leading decimal points are supported with the ALLOW_LEADING_DECIMAL_POINT_FOR_NUMBERS feature.

    ⛔️ Jackson doesn't appear to have support for trailing decimal points.

    Unsupported Features

    There doesn't currently (mid-2021) appear to be any way to configure Jackson to permit the following JSON5 features:

    Numbers may be hexadecimal.

    Numbers may have a trailing decimal point.

    Numbers may begin with an explicit plus sign.

    Additional white space characters are allowed.