On Dec 7, 2013, at 4:55 PM, John Cowan wrote:

> Allen Wirfs-Brock scripsit:
> 
>> Similarly, the JSON texts:
>>   {"1":  1, "2", 2}
>> and
>>   {"2":  2, "1": 1}
>> 
>> or the JSON texts:
>>   {"a": 1, "a": 2}
>> and
>>   {"a": 2, "a": 1}
>> 
>> have an ordering of the object members that must be preserved by the
>> parser in order for downstream semantics to be applied.
> 
> I cannot accept this statement without proof.  Where in the ECMAscript
> definition does it say this?

First, the console out from an experiment run from the developer console of 
Firefox 27

17:22:24.873 var jsonText1 = '{"a": 1, "a": 2}';
17:22:25.244 undefined                                     <----- ignore these, 
they are a console artifact
17:22:50.107 console.log(jsonText1);
17:22:50.124 undefined
17:22:50.125 "{"a": 1, "a": 2}"    <-----note that the console doesn't property 
escape embedded quotes
17:23:45.060 var jsonText2 = '{"a": 2, "a": 1}';
17:23:45.062 undefined
17:24:18.594 console.log(jsonText2);
17:24:18.649 undefined
17:24:18.649 "{"a": 2, "a": 1}"
17:25:31.540 var parsedText1 = JSON.parse(jsonText1);
17:25:31.577 undefined
17:26:36.429 console.log(parsedText1.a)
17:26:36.568 undefined
17:26:36.569 2
17:27:13.754 var parsedText2 = JSON.parse(jsonText2);
17:27:13.882 undefined
17:27:37.533 console.log(parsedText2.a)
17:27:37.660 undefined
17:27:37.661 1

Note that the value of the 'a' property on the JavaScript object produced by 
JSON.parse is either 1 or 2 depending upon the ordering of the member 
definitions with duplicate names.   I'll leave it to you to try using your 
favorite browser.  However, I'm confident that you will see the same result as 
this is what ECMA-262, 5th Edition requires.  I happen to be fairly familiar 
with that document, so I can explain how that is:

1) JSON.parse is specified in by the algorithms in section 15.12.2 
http://www.ecma-international.org/ecma-262/5.1/#sec-15.12.2 starting with the 
first algorithm in that section.
2) Step 2 of that algorithm requires validation the input string against the 
JSON grammar provided in 
http://www.ecma-international.org/ecma-262/5.1/#sec-15.12.1 
3) If the input text cannot be recognized by a parser for that grammar, an 
exception must be thrown at that point.
4) If the input text is recognized by the parser, then step 3 says to parse and 
evaluate the input text as with it was ECMAScript source code.  The result of 
that evaluation is what is normally returned from the function.  The ECMAScript 
parsing and evaluation rules can be used in this manner because a well-formed 
JSON text (that is verified in step 2) is a subset of an ECMAScript 
PrimaryExpression http://www.ecma-international.org/ecma-262/5.1/#sec-11.1 .
5)  The text of a JSON object definition will be parsed and evaluated as if it 
was an EMAScript ObjectLiteral as specified at 
http://www.ecma-international.org/ecma-262/5.1/#sec-11.1.5 The evaluation 
semantics are specified by the algorithms that follow the BNF in that section.
6) Note that the body of an ObjectLiteral is described by the 
PropertyNameAndValueList production which produces a comma separated list of 
PropertyAssignment productions. 
7) The PropertyAssignments of a PropertyNameAndValueList are evaluated in left 
to right order, as specified by 4th algorithm on this section. 
8) As each PropertyAssignment is evaluated, it performs a [[DefineOwnProperty]] 
operation up on the result object using the property name and value provided by 
the PropertyAssignment.
9) [[DefineOwnProperty]] is defined in 
http://www.ecma-international.org/ecma-262/5.1/#sec-8.12.9 . It is a fairly 
complex operation but the short story is that if a property of that name does 
not already exist one is created and assigned the associated value. If a 
property of that name does already exist, the existing value is overwritten 
with the current value. 

In other words, ECMA-262 explicitly specifies that when multiple occurrences of 
the same member name occurs in a JSON object, the value associated with the 
last (right-most) occurrence is used. Order matters.

A similar analysis applies to the first example.  

Allen

 











> 
> -- 
> John Cowan              [email protected]          http://www.ccil.org/~cowan
> C'est la` pourtant que se livre le sens du dire, de ce que, s'y conjuguant
> le nyania qui bruit des sexes en compagnie, il supplee a ce qu'entre eux,
> de rapport nyait pas.               --Jacques Lacan, "L'Etourdit"
> 

_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to