Deviations between json2.js and the currently specified behaviour of JSON.stringify
david-sarah at jacaranda.org
Fri Jun 5 09:05:49 PDT 2009
Oliver Hunt wrote:
> The definition of the JSON.stringify function (per 15.12.3) defines str
> and JO such that [[Get]] will be called twice for every serialised
> property on an object, which may have side effects if the property is a
> My testing shows that json2.js accesses each property only once, which
> makes more sense in my opinion as having side effects occur twice for
> each serialised getter seems odd in of itself, and has the potential to
> be more efficient.
> Gecko currently matches the spec in this regard and calls getters twice.
> Another issue is the arguments passed to the abstract operation str when
> serialising an array when a replacer function is provided. The
> definition of the abstract operation JA says that str should be called
> with the arguments ToString(index) and value. While technically this is
> necessary in order to perform the [[Get]] operation with key on host in
> the str, it has the effect of implying that when replacer is called the
> key given passed as an argument, the key should be of type String.
> Currently no implementation (including json2.js) does this ToString
> conversion, so i think the specified algorithm needs to be corrected to
> remove the ToString from this point.
In that case the replacer would have to be able to handle keys provided
either as a number or as a string -- i.e. it would have to coerce the
key to a string itself in order to be consistent. Note that for an
array-like object that is not actually an array (according to
"Object.prototype.toString.apply(value) === '[object Array]'"),
json2.js will call the replacer with number properties coerced to
I think the draft spec is correct as it stands.
David-Sarah Hopwood ⚥ http://davidsarah.livejournal.com
More information about the es5-discuss