Can the Jackson parser be used to parse JSON5?
Asked Answered
U

3

14

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?

Univalence answered 9/7, 2021 at 6:32 Comment(0)
U
32

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.

Univalence answered 9/7, 2021 at 6:32 Comment(1)
Thanks Graham! I was trying to use JSON5 too.Walkling
S
0

Graham Lea's answer was great.

Here I'll add a couple of improvements Jackson has made in recent years.

  • ALLOW_TRAILING_DECIMAL_POINT_FOR_NUMBERS(Since 2.14): Numbers may have a trailing decimal point.
  • ALLOW_LEADING_PLUS_SIGN_FOR_NUMBERS(Since 2.14): Numbers may begin with an explicit plus sign.
Sprat answered 13/3 at 7:26 Comment(0)
D
-1

For Javascript developers, you could use jsonc-parser which is developed by Microsoft. jsonc-parser could be use in browser and node.js environment.

Here's an example that demonstrates how to use the "jsonc-parser" npm package to parse a JSON5 string:

import * as jsonc from 'jsonc-parser';

const jsonString = `{
    // This is a JSON5 string!
    "name": 'John Doe',
    "age": 42,
    "isMarried": false,
    "pets": [
        {
            "name": 'Fluffy',
            "type": 'cat'
        }
    ]
}`;

const parsedJson = jsonc.parse(jsonString);

console.log(parsedJson);

Online Demo: JSON5 parser

Dichotomize answered 1/5, 2023 at 8:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.