JSON parser grammar

Hallvord R. M. Steen hallvord at opera.com
Sat Jul 4 04:49:01 PDT 2009

>>> a) Allow strings, numbers, Booleans, and null in addition to objects  
>>> and
>>> arrays as top level JSON text.
>>> The ES5 spec. already has this although it isn't in the RFC.  I haven't
>>> heard any suggestions that we remove it.

>> How can you allow "strings" as top level JSON text?
> A piece of text is either a string literal or it is not -- i suspect  
> you're confusing JSON.parse("foo") where you are passing a string  
> containing the characters f,o and o with JSON.parse("\"foo\"") in which  
> the string contains the characters ",f,o,o and " -- eg. a  string  
> literal.

Indeed I was, particularly since IE8's implementation doesn't seem to understand this string-inside-string feature yet so when I tried this earlier I remained confused :-p. Thanks for clarifying.

Another question: The JSON grammar says

JSONNumber :: 
-opt  DecimalIntegerLiteral JSONFraction opt  ExponentPart opt 

JSONFraction :: 
. DecimalDigits 

This apparently makes numbers like "1." illegal? Should this really throw:

JSON.parse('[1.]') ?

And what about
JSON.parse('[1.e10]') ?

Both are of course allowed in normal JavaScript source text.

Hallvord R. M. Steen, Core Tester, Opera Software
http://www.opera.com http://my.opera.com/hallvors/

More information about the es-discuss mailing list