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?
By enabling the following optional Jackson parser features:
ALLOW_UNQUOTED_FIELD_NAMESALLOW_TRAILING_COMMAALLOW_SINGLE_QUOTESALLOW_BACKSLASH_ESCAPING_ANY_CHARACTERALLOW_NON_NUMERIC_NUMBERSALLOW_JAVA_COMMENTSALLOW_LEADING_DECIMAL_POINT_FOR_NUMBERSit seems possible to support all of JSON5's headline features except for:
Jackson has a number of optional features which can be enabled on a parser to make it more lenient in the way it parses.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With