JSON parser grammar

Mark S. Miller erights at google.com
Tue Jun 2 19:26:18 PDT 2009


On Tue, Jun 2, 2009 at 7:06 PM, Oliver Hunt <oliver at apple.com> wrote:

> So i've been looking at the JSON object grammar and have been talking to
> brendan and i'm getting somewhat conflicting information.
>
> The grammars on json.org and in the ES5 spec both prohibit leading 0's on
> any number, but the various implementations disagree with this.
>
> json2.js (from json.org), ie8, and chrome all support the standard ES
> octal literal lexer -- eg. JSON.parse("[010]")[0] === 8
>
> SpiderMonkey allows a leading 0 but still interprets it as a decimal value
> -- eg. JSON.parse("[010]")[0] === 10
>
> It seems to me that the spec needs to be corrected to specify what the
> behaviour actually is, rather than what we wish it could be.
>


Since octal wasn't an official part of ES3, remains absent from official
ES5, and is now explicitly prohibited from ES5/strict, it is good that it is
not specified by JSON. I am surprised that json2.js accepts the syntax, and
even more surprised that it interprets it as octal. Although the rfc says

   A JSON parser transforms a JSON text into another representation.  A
   JSON parser MUST accept all texts that conform to the JSON grammar.
   A JSON parser MAY accept non-JSON forms or extensions.

I think the behavior you state of json2.js, ie8, and chrome should be
considered a bug. I hesitate to make the same statement about SpiderMonkey,
because their behavior falls within both the letter and spirit of the rfc,
while maintaining the subset relationship between JSON and EcmaScript.

I asked Crock and he clarified why json2.js has this bug. json2.js relies on
eval to parse the json. For safety it guards this eval with regular
expressions. These regular expressions are already too complicated to be
confident in their safety, so it wasn't worth adding complexity for a
non-safety issue.

As for how json2.js interprets these numbers -- according to eval's
interpretation on the underlying platform.

-- 
   Cheers,
   --MarkM
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20090602/d6fb3ba4/attachment.html>


More information about the es-discuss mailing list