more JSON spec questions
Hallvord R. M. Steen
hallvord at opera.com
Wed Aug 26 15:03:56 PDT 2009
the feedback below refers to the "Initial ECMA formated ES5 draft" and
consists of a few things I've noticed while working on a JSON test suite.
First, I think considerable work has gone into the JSON part of the spec
since I first looked at it - it's beginning to look really good. My test
suite is a work in progress, and of course both the tests and my questions
might just show that there's something I haven't entirely understood
rather than problems with the specification. Feedback is welcome.
Now, the questions..
1) Apparently the grammar still disallows literal tab inside JSONString.
Implementations that pass this test allow it:
- from earlier discussions I had the impression that the spec would
2) The grammar and an existing test in the json.org test suite disallows
final commas in array input (JSON.parse('[1,]') should apparently throw) -
IE8, Safari 4 and Firefox 3.5 all seem to happily accept it though, as
does the yet-to-go-public Opera implementation. I'd like some reassurance
from other browser vendors that you consider this a bug and intend to fix
it before I push for a fix here because this is the kind of thing that
might cause compat problems :-o (Guess this is more a question for the
implementers on the list than for the spec authors.)
3) Should JSON.parse() without arguments throw? It's not obvious from the
spec as far as I can tell, but it's what most browser implementations do.
4) (Editorial) - spec says "The abstract operation Str(key, holder) has
PropertyList and ReplacerFunction" - the algorithm does not actually use
PropertyList (very trivial issue, of course)
5) (Editorial) It's not apparent from the prose description of
JSON.stringify() that if the replacer argument is an array, it will only
be applied to objects. I expected to be able to do something like
JSON.stringify( [0,1,2],  ) => ''. Suggest a small clarification,
perhaps just saying "selecting object keys that will be stringified"
instead of "selecting the keys that will be stringified"
6) (Editorial) Per the algorithm, passing in a "replacer" array to
JSON.stringify() will cause it to also stringify dontEnum properties. An
example would be JSON.stringify( Number, ['MAX_VALUE'] ) including the
MAX_VALUE property where JSON.stringify(Number) would not. Perhaps clarify
this in the prose too, not just in the algorithm? Calling it a "whitelist"
seems it will only narrow down the number of elements that are included,
not expand it..
7) I also didn't understand why the "reviver" function was called once
with "key" being the empty string and "value" being the entire output
object until I read the algorithm carefully.
Quick word about the test suite: it's current home is
http://testsuites.opera.com/JSON/ where you can have a look at the README,
load runner.htm to run all tests (Warning: if you do that in IE8 something
makes it eat all your memory and make the PC entirely unusable!) or browse
through folders to individual tests. Hope it's useful to all you
implementers out there. Feedback welcome :)
Hallvord R. M. Steen, Core Tester, Opera Software
More information about the es-discuss