diff --git a/README.md b/README.md index e79c565..1252e03 100644 --- a/README.md +++ b/README.md @@ -89,7 +89,7 @@ All input to `JSON.parse` that is currently rejected will continue to be, all in ### Modified values Reviver functions are intended to modify or remove values in the output, but those changes should have no effect on the source-derived arguments passed to them. Because reviver functions are invoked bottom-up, this means that values may not correlate with source text. -We consider this to be acceptable, but moot (see the following point). +We consider this to be acceptable, but mostly moot (see the following point). Where _not_ moot (such as when not-yet-visited array indexes or object entries are modified), source text is suppressed. ### Non-primitive values Per https://github.com/tc39/proposal-json-parse-with-source/issues/10#issuecomment-704441802 , source text exposure is limited to primitive values. diff --git a/spec.html b/spec.html index 15893e1..1fbc6db 100644 --- a/spec.html +++ b/spec.html @@ -48,7 +48,7 @@

JSON.isRawJSON ( _O_ )

JSON.parse ( _text_ [ , _reviver_ ] )

This function parses a JSON text (a JSON-formatted String) and produces an ECMAScript language value. The JSON format represents literals, arrays, and objects with a syntax similar to the syntax for ECMAScript literals, Array Initializers, and Object Initializers. After parsing, JSON objects are realized as ECMAScript objects. JSON arrays are realized as ECMAScript Array instances. JSON strings, numbers, booleans, and null are realized as ECMAScript Strings, Numbers, Booleans, and *null*.

-

The optional _reviver_ parameter is a function that takes two parameters, _key_ and _value_. It can filter and transform the results. It is called with each of the _key_/_value_ pairs produced by the parse,For each value produced by the parse, it is called with three arguments (the key, the value, and a context object containing [for primitive values] details of the corresponding Parse Node) and its return value is used instead of the original value. If it returns what it received, the structure is not modified. If it returns *undefined* then the property is deleted from the result.

+

The optional _reviver_ parameter is a function that takes two parameters, _key_ and _value_. It can filter and transform the results. It is called with each of the _key_/_value_ pairs produced by the parse,For each value produced by the parse, it is called with three arguments (the key, the value, and a context object containing [for unmodified primitive values] details of the corresponding Parse Node) and its return value is used instead of the original value. If it returns what it received, the structure is not modified. If it returns *undefined* then the property is deleted from the result.

1. Let _jsonString_ be ? ToString(_text_). 1. [id="step-json-parse-validate"] Parse StringToCodePoints(_jsonString_) as a JSON text as specified in ECMA-404. Throw a *SyntaxError* exception if it is not a valid JSON text as defined in that specification. @@ -64,7 +64,8 @@

JSON.parse ( _text_ [ , _reviver_ ] )

1. Let _root_ be OrdinaryObjectCreate(%Object.prototype%). 1. Let _rootName_ be the empty String. 1. Perform ! CreateDataPropertyOrThrow(_root_, _rootName_, _unfiltered_). - 1. Return ? InternalizeJSONProperty(_root_, _rootName_, _reviver_, _script_). + 1. Let _snapshot_ be CreateJSONParseRecord(_script_, _rootName_, _unfiltered_). + 1. Return ? InternalizeJSONProperty(_root_, _rootName_, _reviver_, _snapshot_). 1. Else, 1. Return _unfiltered_.
@@ -74,13 +75,108 @@

JSON.parse ( _text_ [ , _reviver_ ] )

However, because behaves differently during `JSON.parse`, the same source text can produce different results when evaluated as a |PrimaryExpression| rather than as JSON. Furthermore, the Early Error for duplicate *"__proto__"* properties in object literals, which likewise does not apply during `JSON.parse`, means that not all texts accepted by `JSON.parse` are valid as a |PrimaryExpression|, despite matching the grammar.

+ + +

JSON Parse Record

+

A JSON Parse Record is a Record value used to describe the initial state of a value parsed from JSON text.

+

JSON Parse Records have the fields listed in .

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Field NameValueMeaning
[[ParseNode]]a Parse NodeThe context Parse Node.
[[Key]]a property nameThe property name with which [[Value]] is associated.
[[Value]]an ECMAScript language valueThe value produced by evaluation of [[ParseNode]].
[[Elements]]a List of JSON Parse RecordsJSON Parse Records corresponding with the elements of a [[Value]] that is an Array, in order. If [[Value]] is not an Array, the List will be empty.
[[Entries]]a List of JSON Parse RecordsJSON Parse Records corresponding with the entries of a [[Value]] that is a non-Array Object, in source order. If [[Value]] is not a non-Array Object, the List will be empty.
+
+
+ + +

+ CreateJSONParseRecord ( + _parseNode_: a Parse Node, + _key_: a property name, + _val_: an ECMAScript language value, + ): a JSON Parse Record +

+
+
description
+
It recursively combines a _parseNode_ parsed from JSON text and the _val_ produced by its evaluation.
+
+ + 1. Let _typedValNode_ be ShallowestContainedJSONValue of _parseNode_. + 1. Assert: _typedValNode_ is not ~empty~. + 1. Let _elements_ be a new empty List. + 1. Let _entries_ be a new empty List. + 1. If _val_ is an Object, then + 1. Let _isArray_ be ! IsArray(_val_). + 1. If _isArray_ is *true*, then + 1. Assert: _typedValNode_ is an |ArrayLiteral| Parse Node. + 1. Let _contentNodes_ be ArrayLiteralContentNodes of _typedValNode_. + 1. Let _len_ be the number of elements in _contentNodes_. + 1. Let _valLen_ be ! LengthOfArrayLike(_val_). + 1. Assert: _valLen_ = _len_. + 1. Let _I_ be 0. + 1. Repeat, while _I_ < _len_, + 1. Let _propName_ be ! ToString(𝔽(_I_)). + 1. Let _elementParseRecord_ be CreateJSONParseRecord(_contentNodes_[_I_], _propName_, ! Get(_val_, _propName_)). + 1. Append _elementParseRecord_ to _elements_. + 1. Set _I_ to _I_ + 1. + 1. Else, + 1. Assert: _typedValNode_ is an |ObjectLiteral| Parse Node. + 1. Let _properties_ be PropertyDefinitionList of _typedValNode_. + 1. NOTE: Because _val_ was produced from JSON text and has not been modified, all of its properties are enumerable and all of its property keys are Strings. + 1. Let _keys_ be ! EnumerableOwnProperties(_val_, ~key~). + 1. For each String _P_ of _keys_, do + 1. Let _propertyDefinition_ be ~empty~. + 1. For each Parse Node _candidate_ of _properties_, do + 1. Let _propName_ be PropName of _candidate_. + 1. If SameValue(_propName_, _P_) is *true*, set _propertyDefinition_ to _candidate_. + 1. Assert: _propertyDefinition_ is not ~empty~. + 1. NOTE: In the case of JSON text specifying multiple name/value pairs with the same name for a single object (such as {"a":"lost","a":"kept"}), the value for the corresponding property of the resulting ECMAScript object is specified by the last pair with that name. + 1. Assert: _propertyDefinition_ is PropertyDefinition : PropertyName `:` AssignmentExpression. + 1. Let _propertyValueNode_ be the |AssignmentExpression| of _propertyDefinition_. + 1. Let _entryParseRecord_ be CreateJSONParseRecord(_propertyValueNode_, _P_, ! Get(_val_, _P_)). + 1. Append _entryParseRecord_ to _entries_. + 1. Else, + 1. Assert: _typedValNode_ is not an |ArrayLiteral| Parse Node and not an |ObjectLiteral| Parse Node. + 1. Return the JSON Parse Record { [[ParseNode]]: _typedValNode_, [[Key]]: _key_, [[Value]]: _val_, [[Elements]]: _elements_, [[Entries]]: _entries_ }. + +
+
+

InternalizeJSONProperty ( _holder_: an Object, _name_: a String, _reviver_: a function object, - _valNode_: a Parse Node, + _parseRecord_: either a JSON Parse Record or *undefined*, ): either a normal completion containing an ECMAScript language value or a throw completion

@@ -90,44 +186,39 @@

It performs the following steps when called:

- 1. Let _typedValNode_ be ShallowestContainedJSONValue of _valNode_. - 1. Assert: _typedValNode_ is not ~empty~. - 1. Let _context_ be OrdinaryObjectCreate(%Object.prototype%). - 1. If _typedValNode_ is not an |ArrayLiteral| Parse Node and not an |ObjectLiteral| Parse Node, then - 1. Let _sourceText_ be the source text matched by _typedValNode_. - 1. Perform ! CreateDataPropertyOrThrow(_context_, *"source"*, CodePointsToString(_sourceText_)). 1. Let _val_ be ? Get(_holder_, _name_). + 1. Let _context_ be OrdinaryObjectCreate(%Object.prototype%). + 1. If _parseRecord_ is a JSON Parse Record and SameValue(_val_, _parseRecord_.[[Value]]) is *true*, then + 1. If _val_ is not an Object, then + 1. Let _parseNode_ be _parseRecord_.[[ParseNode]]. + 1. Assert: _parseNode_ is not an |ArrayLiteral| Parse Node and not an |ObjectLiteral| Parse Node. + 1. Let _sourceText_ be the source text matched by _parseNode_. + 1. Perform ! CreateDataPropertyOrThrow(_context_, *"source"*, CodePointsToString(_sourceText_)). + 1. Let _elementRecords_ be _parseRecord_.[[Elements]]. + 1. Let _entryRecords_ be _parseRecord_.[[Entries]]. + 1. Else, + 1. Let _elementRecords_ be a new empty List. + 1. Let _entryRecords_ be a new empty List. 1. If _val_ is an Object, then 1. Let _isArray_ be ? IsArray(_val_). 1. If _isArray_ is *true*, then + 1. Let _elementRecordsLen_ be the number of elements in _elementRecords_. 1. Let _len_ be ? LengthOfArrayLike(_val_). - 1. Assert: _typedValNode_ is an |ArrayLiteral| Parse Node. - 1. Let _contentNodes_ be ArrayLiteralContentNodes of _typedValNode_. - 1. Let _contentNodesLen_ be the number of elements in _contentNodes_. - 1. Assert: _contentNodesLen_ = _len_. 1. Let _I_ be 0. 1. Repeat, while _I_ < _len_, 1. Let _prop_ be ! ToString(𝔽(_I_)). - 1. Let _newElement_ be ? InternalizeJSONProperty(_val_, _prop_, _reviver_, _contentNodes_[_I_]). + 1. If _I_ < _elementRecordsLen_, let _elementRecord_ be _elementRecords_[_I_]. Otherwise, let _elementRecord_ be *undefined*. + 1. Let _newElement_ be ? InternalizeJSONProperty(_val_, _prop_, _reviver_, _elementRecord_). 1. If _newElement_ is *undefined*, then 1. Perform ? _val_.[[Delete]](_prop_). 1. Else, 1. Perform ? CreateDataProperty(_val_, _prop_, _newElement_). 1. Set _I_ to _I_ + 1. 1. Else, - 1. Assert: _typedValNode_ is an |ObjectLiteral| Parse Node. - 1. Let _properties_ be PropertyDefinitionList of _typedValNode_. 1. Let _keys_ be ? EnumerableOwnProperties(_val_, ~key~). 1. For each String _P_ of _keys_, do - 1. Let _propertyDefinition_ be ~empty~. - 1. For each Parse Node _candidate_ of _properties_, do - 1. Let _propName_ be PropName of _candidate_. - 1. If SameValue(_propName_, _P_) is *true*, set _propertyDefinition_ to _candidate_. - 1. Assert: _propertyDefinition_ is not ~empty~. - 1. NOTE: In the case of JSON text specifying multiple name/value pairs with the same name for a single object (such as {"a":"lost","a":"kept"}), the value for the corresponding property of the resulting ECMAScript object is specified by the last pair with that name. - 1. Assert: _propertyDefinition_ is PropertyDefinition : PropertyName `:` AssignmentExpression. - 1. Let _propertyValueNode_ be the |AssignmentExpression| of _propertyDefinition_. - 1. Let _newElement_ be ? InternalizeJSONProperty(_val_, _P_, _reviver_, _propertyValueNode_). + 1. Let _entryRecord_ be the element of _entryRecords_ whose [[Key]] field is _P_. If there is no such element, let _entryRecord_ be *undefined*. + 1. Let _newElement_ be ? InternalizeJSONProperty(_val_, _P_, _reviver_, _entryRecord_). 1. If _newElement_ is *undefined*, then 1. Perform ? _val_.[[Delete]](_P_). 1. Else,