JSON parser grammar

Allen Wirfs-Brock Allen.Wirfs-Brock at microsoft.com
Wed Jun 3 11:31:46 PDT 2009


See below

>-----Original Message-----
>From: Oliver Hunt [mailto:oliver at apple.com]
...
>
>On Jun 2, 2009, at 11:09 PM, Rob Sayre wrote:
>
>> On 6/3/09 1:56 AM, Allen Wirfs-Brock wrote:
...
>> 1.) leading zeros are parsed as decimal numbers (octal seems like a
>> bug no matter what, per MarkM)
>IE8 and V8's JSON implementation, and json2.js at json.org all
>interpret 010, as octal (eg. 8), and 009 as 9
>

I'm not sure how you are testing IE8, but in my tests of IE8
	JSON.parse('010')
yields a syntax error (as currently specified by ES5) while
	JSON.parse('10')
Returns the number 10.

json2.js is probably producing the results you see on IE because internally it uses eval and IE supports octal literal with the semantics you observed. Are you sure, you are actually running the native JSON when you seem to see octal being accepted?  Native JSON is only enabled if your page is operating in "IE8 standards" mode.

>> 2.) trailing commas in objects and arrays are allowed ({"foo":
>> 42,"bar":42,})
>V8's JSON implementation also accepts [1,,,2]

IE8 syntax errors on both '[1,]' and '[1,,3]' as currently specified by ES5

>
>> 3.) tabs and linebreaks are allowed in JSON strings (but
>> JSON.stringify produces escape sequences, per spec)
>My testing shows that only '\' (excluding actual escape sequences) and
>'"' are prohibited -- all other values from 0-0xFFFF are allowed.

IE8 allows all control characters except or NUL,LF,CR to appear unescaped in JSON string literals
Violates/extends current ES5 spec.

As far as I can tell, this is an unintended extension in IE8 that is a result of reusing some parts of the JavaScript lexer for JSON.

Allen



More information about the es-discuss mailing list