Deviations between json2.js and the currently specified behaviour of JSON.stringify

# Oliver Hunt (16 years ago)

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 getter.

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.

# Allen Wirfs-Brock (16 years ago)

I must be dense this morning but I don't see how the two calls to [[Get]] occurs in the algorithm. The first call to [[Get]] for a property key in object holder occurs in step 1 of Str. How does the second call occur? The only place I see a possibility would be within a replacer function that reaccesses the property via its this value. What am I missing?

I agree with your analysis of ToString in JA. That means that ToString need to be applied to the key argument in step 1 of Str. Presumably, toJSON methods (Str step 2.b.i) also need to get the raw key as an argument rather than the ToString'd key.

Allen

From: es-discuss-bounces at mozilla.org [mailto:es-discuss-bounces at mozilla.org] On Behalf Of Oliver Hunt Sent: Thursday, June 04, 2009 8:48 PM To: es-discuss at mozilla.org Subject: Deviations between json2.js and the currently specified behaviour of JSON.stringify

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 getter.

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.

# Oliver Hunt (16 years ago)

On Jun 5, 2009, at 12:01 PM, Allen Wirfs-Brock wrote:

I must be dense this morning but I don’t see how the two calls to
[[Get]] occurs in the algorithm. The first call to [[Get]] for a
property key in object holder occurs in step 1 of Str. How does the
second call occur? The only place I see a possibility would be
within a replacer function that reaccesses the property via its this
value. What am I missing?

Indeed you are correct, i must have misread it at some point and
remembered that misreading.

I agree with your analysis of ToString in JA. That means that
ToString need to be applied to the key argument in step 1 of Str.
Presumably, toJSON methods (Str step 2.b.i) also need to get the raw
key as an argument rather than the ToString’d key.

Yes, i forgot about them.