Unintuitive JSON Parsing
21–30 of 77 posts
Re: Unintuitive JSON Parsing
#22> The parser will not complain about leading zeros because JSON has no concept of leading zeros. Of course there is no logical reason why the parser shouldn't have this concept just because the spec doesn't require. IMO, beyond basic correctness, user friendly error messages are the main differentiator between excellent parsers and crappy parsers.
Reporting the last valid input and the start of the first invalid location (possibly repeating the first couple characters of invalid content, filtered for safety) is what I'd generally prefer in an error message.
The message in Firefox is: JSON.parse: expected ',' or ']' after array element at line 1 column 3 of the JSON data
In Chrome: Unexpected number in JSON at position 2
Re: Unintuitive JSON Parsing
#23Re: Unintuitive JSON Parsing
#24Why is concatenated json a thing? In what sense is: {0}{1} better than [{0},{1}]? Presumably, if a few bytes are a major concern, you aren't using JSON anyway.
Concatenated JSON is ambiguous, so you need to put some whitespace between any JSON texts where both aren't arrays or objects.
This is a widely used technique.
Re: Unintuitive JSON Parsing
#25So is it incorrect (Technically, at any rate) for a parser to support leading zeroes in its implementation?
It seems honoring this type of technical correctness matters a lot. For example, imagine if ECMA added a new feature (e.g. 0-prefixed octal literals) in 2020..
Another issue: security. Imagine a hacker figured out that you used a mix of JSON parsers on your application (e.g. V8 and jq), and they produced different output.
For a vaguely related example, consider that some URL parsers interpret N (U+FF2E - fullwidth latin N) as ".", meaning you can sneakily add a ".." to the URL with NN (see https://www.blackhat.com/docs/us-17/thursday/us-17-Tsai-A-Ne...)
Re: Unintuitive JSON Parsing
#26Why is concatenated json a thing? In what sense is: {0}{1} better than [{0},{1}]? Presumably, if a few bytes are a major concern, you aren't using JSON anyway.
Re: Unintuitive JSON Parsing
#27This is false. In JavaScript, a leading zero, unless accompanied by a lowercase oh ('o') does indicate the number is written in octal.
08 === 0o10; // true
Here, the left side is still base 10, while the right side is base 8.
Re: Unintuitive JSON Parsing
#28I think JSON would be vastly improved if it were to just allow comments. Maintaining configuration in JSON is unnecessarily painful due to this pointless feature gap.
I found this to be a rather interesting article, and it'd be a shame if the discussion around it centered on such a well-tread topic.
Re: Unintuitive JSON Parsing
#29Why is concatenated json a thing? In what sense is: {0}{1} better than [{0},{1}]? Presumably, if a few bytes are a major concern, you aren't using JSON anyway.
To elaborate:
{0}{1} is better than [{0},{1}] because, when streamed, {0} is still valid, while [{0}, is not.
Re: Unintuitive JSON Parsing
#30IMHO if it's not going to support octal anyway, it makes zero(!) sense to artificially limit/special-case things like this, because then it's much simpler and more consistent to have leading zeros behave like any other digit.
Of course, until recently JSON wasn't a strict subset of JS but that was an oversight rather than by design.