JSONNumber - optional decimal

Oliver Hunt oliver at apple.com
Tue Jun 8 23:28:02 PDT 2010

On Jun 8, 2010, at 8:46 PM, Garrett Smith wrote:

> Today I looked for a good json regexp tester and finding nothing,
> decided to write one.
> The strategy that occurred to me was to first define a regex for the
> literal components (ES5 lumps literal value into the JSONValue
> alongside JSONObject and JSONArray). That way, I could reuse the
> literal components to define JSONObject and JSONArray.
> In the process, I noticed that the grammar defined for JSONNumber is
> different than that defiend by json2.js, which I had just looked at,
> and which has similar numeric parsing as seen in The GOod parts. That
> is, it allows DecimalLiteral, which includes this production:
>  DecimalIntegerLiteral .
> In contrast, JSONNumber does not allow that.
> JSONNumber ::
> -opt  DecimalIntegerLiteral JSONFractionopt  ExponentPartopt
> Am I misreading the spec, or is "1." not valid JSON?
> Next step was to see what the browsers do.
> Mozilla and IE accept it, while Webkit and Opera throws an error.
> JSON.parse("1.")
> Firefox 3.6, IE8, Besen r27:
> 1
> Safari, Opera
> SyntaxError
> IF anyone has a correct JSON parser, I would appreciate it. Also, are
> there any good test suites for JSON?

I spent quite a bit of time ensuring JSC's JSON parser exactly matched the spec grammar, and hence exhibits this behaviour so I believe the parser in JSC is "correct" -- if you're interested the code can be found at  (note that it's under an MIT style license) http://trac.webkit.org/browser/trunk/JavaScriptCore/runtime/LiteralParser.cpp
At the time I recall talking a bit with Hallvord Steen of Opera, and he was working on a testsuite of some sort but I can no longer recall the url.

That said I think allowing '1.' (etc) makes sense as it's fairly standard across multiple programming languages, and I am unaware of any specific reason for disallowing it.

In the long term I don't see changing the grammar to allow a trailing period as being harmful as it's a relaxation.  In the short term vendors that follow the spec may fail to parse content :-(


> Garrett
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss

More information about the es-discuss mailing list