SublimeLinter-json icon indicating copy to clipboard operation
SublimeLinter-json copied to clipboard

strict mode accepts NaN / Infinity / -Infinity (not valid JSON)

Open dpc00 opened this issue 2 months ago • 1 comments

In the default strict mode the linter accepts NaN, Infinity and -Infinity, which are not valid JSON, so files that every other JSON parser rejects show no errors.

Repro (Sublime Text 4215, SublimeLinter-json, strict: true which is the default). d.json:

{"n": NaN, "i": Infinity, "m": -Infinity}

SublimeLinter shows no error. JSON.parse('{"n": NaN}') in JavaScript, jq . and python -c "import json; json.loads('{\"n\": NaN}', parse_constant=...)" with a rejecting hook all treat it as a syntax error (RFC 8259 has no NaN or Infinity).

Cause: strict mode calls json.loads(code) with the default parse_constant, and the Python decoder accepts those three names as an extension.

Suggested fix: pass a parse_constant that raises ValueError with a line/column message, for example:

def _reject(name):
    raise ValueError('Unexpected constant {}: line 1 column 1'.format(name))
json.loads(code, parse_constant=_reject)

(the position would need to be found, for example by searching the source for the constant; the message format the strict_regex expects is <message>: line N column M.)

I did not look into duplicate keys: #19 is closed with an explanation.

The rest worked as expected: syntax errors, trailing commas (Python 3.14 message), invalid control characters, and lines containing accents/emoji get correct columns. In loose mode (.sublime-* files) comments and trailing commas are accepted and a real error is reported at the right column.

dpc00 avatar Sep 29 '26 13:09 dpc00

Similar to #19 this seems non-trivial to solve; same reason we need to find line/col errors and write meaningful error messages.

You can take a stab on this one and then on #19 too if you want to as it got closed for the "too complicated" reason. Should be backed up by tests obviously as it seems non-trivial.

Keep an eye on runtime performance as we typically run in the background.

kaste avatar Sep 29 '26 17:09 kaste