The parse function parses a JSON text (a JSON-formatted String) and produces an ECMAScript 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.
2540 |
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, 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.
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.
Let completion be the result of parsing and evaluating StringToCodePoints(scriptString) as if it was the source text of an ECMAScript Script. The extended PropertyDefinitionEvaluation semantics defined in must not be used during the evaluation.
Let unfiltered be completion.[[Value]].
Assert: unfiltered is either a String, Number, Boolean, Null, or an Object that is defined by either an ArrayLiteral or an ObjectLiteral.
This function is the %JSONParse% intrinsic object.
2543 |
The "length" property of the parse function is 2.
2544 |
2545 |
2546 |
1.1.1.1 ParseJSONText ( text )
2547 |
The abstract operation ParseJSONText takes argument text (an ECMAScript language value). It parses a JSON text (a JSON-formatted String) and produces an ECMAScript value. It performs the following steps when called:
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.
Let completion be the result of parsing and evaluating StringToCodePoints(scriptString) as if it was the source text of an ECMAScript Script. The extended PropertyDefinitionEvaluation semantics defined in must not be used during the evaluation.
Let unfiltered be completion.[[Value]].
Assert: unfiltered is either a String, Number, Boolean, Null, or an Object that is defined by either an ArrayLiteral or an ObjectLiteral.
Return unfiltered.
2549 |
2550 |
2551 |
2552 |
2553 |
1.1.2 JSON.parseImmutable ( text [ , reviver ] )
2554 |
The parseImmutable function parses a JSON text (a JSON-formatted String) and produces an ECMAScript 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 records. JSON arrays are realized as ECMAScript tuples. JSON strings, numbers, booleans, and null are realized as ECMAScript Strings, Numbers, Booleans, and null.
2555 |
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, 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 abstract operation BuildImmutableProperty takes arguments value (an ECMAScript language value), name (a String), and reviver (a function object or Undefined). It performs the following steps when called:
Let immutable be ? Call(reviver, undefined, « name, immutable »).
If Type(immutable) is Object, throw a TypeError exception.
Return immutable.
2564 |
It is not permitted for a conforming implementation of JSON.parseImmutable to extend the JSON grammars. If an implementation wishes to support a modified or extended JSON interchange format it must do so by defining a different parse function.
2565 | Note
2566 |
In the case where there are duplicate name Strings within an object, lexically preceding values for the same key shall be overwritten.
2567 |
2568 |
2569 |
2570 | Note
2571 |
There are two differences in the behaviour of the reviver parameter of the JSON.parse and JSON.parseImmutable functions:
2572 |
2573 |
JSON.parse passes the parent object as the this value, while JSON.parseImmutable passes undefined.
2574 |
When reviver returns undefined for an array element, it introduced an hole. When reviver returns undefined for a tuple element, undefined is used as the resulting value.
2575 |
2576 |
2577 |
2578 |
2579 |
2580 |
1.1.3 JSON.stringify ( value [ , replacer [ , space ] ] )
All Software contained in this document ("Software") is protected by copyright and is being made available under the "BSD License", included below. This Software may be subject to third party rights (rights from parties other than Ecma International), including patent rights, and no licenses under such third party rights are granted under this license even if the third party concerned is a member of Ecma International. SEE THE ECMA CODE OF CONDUCT IN PATENT MATTERS AVAILABLE AT https://ecma-international.org/memento/codeofconduct.htm FOR INFORMATION REGARDING THE LICENSING OF PATENT CLAIMS THAT ARE REQUIRED TO IMPLEMENT ECMA INTERNATIONAL STANDARDS.
2597 |
2598 |
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
2599 |
2600 |
2601 |
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2602 |
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
2603 |
Neither the name of the authors nor Ecma International may be used to endorse or promote products derived from this software without specific prior written permission.
2604 |
2605 |
2606 |
THIS SOFTWARE IS PROVIDED BY THE ECMA INTERNATIONAL "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ECMA INTERNATIONAL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
The `parse` function parses a JSON text (a JSON-formatted String) and produces an ECMAScript 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*.
23 |
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, 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.
24 |
25 | 1. Let _jsonString_ be ? ToString(_text_).
26 | 1. 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.
27 | 1. Let _scriptString_ be the string-concatenation of *"("*, _jsonString_, and *");"*.
28 | 1. Let _completion_ be the result of parsing and evaluating StringToCodePoints(_scriptString_) as if it was the source text of an ECMAScript |Script|. The extended PropertyDefinitionEvaluation semantics defined in must not be used during the evaluation.
29 | 1. Let _unfiltered_ be _completion_.[[Value]].
30 | 1. Assert: _unfiltered_ is either a String, Number, Boolean, Null, or an Object that is defined by either an |ArrayLiteral| or an |ObjectLiteral|.
31 | 1. Let _unfiltered_ be ? ParseJSONText(_text_).
32 | 1. If IsCallable(_reviver_) is *true*, then
33 | 1. Let _root_ be OrdinaryObjectCreate(%Object.prototype%).
34 | 1. Let _rootName_ be the empty String.
35 | 1. Perform ! CreateDataPropertyOrThrow(_root_, _rootName_, _unfiltered_).
36 | 1. Return ? InternalizeJSONProperty(_root_, _rootName_, _reviver_).
37 | 1. Else,
38 | 1. Return _unfiltered_.
39 |
40 |
This function is the %JSONParse% intrinsic object.
41 |
The *"length"* property of the `parse` function is 2.
42 |
43 |
44 |
45 |
46 | ParseJSONText (
47 | _text_: an ECMAScript language value
48 | )
49 |
50 |
51 |
52 |
description
53 |
It parses a JSON text (a JSON-formatted String) and produces an ECMAScript value.
54 |
55 |
56 | 1. Let _jsonString_ be ? ToString(_text_).
57 | 1. 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.
58 | 1. Let _scriptString_ be the string-concatenation of *"("*, _jsonString_, and *");"*.
59 | 1. Let _completion_ be the result of parsing and evaluating StringToCodePoints(_scriptString_) as if it was the source text of an ECMAScript |Script|. The extended PropertyDefinitionEvaluation semantics defined in must not be used during the evaluation.
60 | 1. Let _unfiltered_ be _completion_.[[Value]].
61 | 1. Assert: _unfiltered_ is either a String, Number, Boolean, Null, or an Object that is defined by either an |ArrayLiteral| or an |ObjectLiteral|.
62 | 1. Return _unfiltered_.
63 |
64 |
65 |
66 |
67 |
68 |
JSON.parseImmutable ( _text_ [ , _reviver_ ] )
69 |
The `parseImmutable` function parses a JSON text (a JSON-formatted String) and produces an ECMAScript 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 records. JSON arrays are realized as ECMAScript tuples. JSON strings, numbers, booleans, and null are realized as ECMAScript Strings, Numbers, Booleans, and *null*.
70 |
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, 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.
This function is the %JSONParseImmutable% intrinsic object.
76 |
The *"length"* property of the `parseImmutable` function is 2.
77 |
78 |
79 |
80 |
81 | BuildImmutableProperty (
82 | _value_: an ECMAScript language value,
83 | _name_: a String,
84 | _reviver_: a function object or Undefined,
85 | )
86 |
87 |
88 |
89 |
90 | 1. If Type(_value_) is Object, then
91 | 1. Let _isArray_ be ? IsArray(_val_).
92 | 1. If _isArray_ is *true*, then
93 | 1. Let _items_ be a new empty List.
94 | 1. Let _I_ be 0.
95 | 1. Let _len_ be ? LengthOfArrayLike(_val_).
96 | 1. Repeat, while _I_ < _len_,
97 | 1. Let _childName_ be ! ToString(_I_).
98 | 1. Let _childValue_ be ? Get(_value_, ! _childName_).
99 | 1. Let _newElement_ be ? BuildImmutableProperty(_childValue_, _childName_, _reviver_).
100 | 1. Assert: Type(_newElement_) is not Object.
101 | 1. Append _newElement_ to _items_.
102 | 1. Set _I_ to _I_ + 1.
103 | 1. Let _immutable_ be a new Tuple value whose [[Sequence]] is _items_.
104 | 1. Else,
105 | 1. Let _props_ be ? EnumerableOwnPropertyNames(_obj_, ~key+value~).
106 | 1. Let _fields_ be a new empty List.
107 | 1. For each element _prop_ of _props_, do
108 | 1. Let _childName_ be ! GetV(_prop_, `"0"`).
109 | 1. Let _childValue_ be ! GetV(_prop_, `"1"`).
110 | 1. Let _newElement_ be ? BuildImmutableProperty(_childValue_, _childName_, _reviver_).
111 | 1. Assert: Type(_newElement_) is not Object.
112 | 1. If _newElement_ is not *undefined*, then
113 | 1. Let _field_ be the Record { [[Key]]: _childName_, [[Value]]: _newElement_ }.
114 | 1. Append _field_ to _fields_.
115 | 1. Let _immutable_ be ! CreateRecord(_fields_).
116 | 1. Else,
117 | 1. Let _immutable_ be _value_.
118 | 1. If IsCallable(_reviver_) is *true*, then
119 | 1. Let _immutable_ be ? Call(_reviver_, *undefined*, « _name_, _immutable_ »).
120 | 1. If Type(_immutable_) is Object, throw a *TypeError* exception.
121 | 1. Return _immutable_.
122 |
123 |
It is not permitted for a conforming implementation of `JSON.parseImmutable` to extend the JSON grammars. If an implementation wishes to support a modified or extended JSON interchange format it must do so by defining a different parse function.
124 |
125 |
In the case where there are duplicate name Strings within an object, lexically preceding values for the same key shall be overwritten.
126 |
127 |
128 |
129 |
130 |
There are two differences in the behaviour of the _reviver_ parameter of the *JSON.parse* and *JSON.parseImmutable* functions:
131 |
132 |
*JSON.parse* passes the parent object as the *this* value, while *JSON.parseImmutable* passes *undefined*.
133 |
When _reviver_ returns *undefined* for an array element, it introduced an hole. When _reviver_ returns *undefined* for a tuple element, *undefined* is used as the resulting value.
150 |
151 | 1. Let _value_ be ? Get(_holder_, _key_).
152 | 1. If Type(_value_) is Object or BigInt, then
153 | 1. Let _toJSON_ be ? GetV(_value_, *"toJSON"*).
154 | 1. If IsCallable(_toJSON_) is *true*, then
155 | 1. Set _value_ to ? Call(_toJSON_, _value_, « _key_ »).
156 | 1. If _state_.[[ReplacerFunction]] is not *undefined*, then
157 | 1. Set _value_ to ? Call(_state_.[[ReplacerFunction]], _holder_, « _key_, _value_ »).
158 | 1. If Type(_value_) is Object, then
159 | 1. If _value_ has a [[NumberData]] internal slot, then
160 | 1. Set _value_ to ? ToNumber(_value_).
161 | 1. Else if _value_ has a [[StringData]] internal slot, then
162 | 1. Set _value_ to ? ToString(_value_).
163 | 1. Else if _value_ has a [[BooleanData]] internal slot, then
164 | 1. Set _value_ to _value_.[[BooleanData]].
165 | 1. Else if _value_ has a [[BigIntData]] internal slot, then
166 | 1. Set _value_ to _value_.[[BigIntData]].
167 | 1. If _value_ is *null*, return *"null"*.
168 | 1. If _value_ is *true*, return *"true"*.
169 | 1. If _value_ is *false*, return *"false"*.
170 | 1. If Type(_value_) is Record or Type(_value_) is Tuple, set _value_ to ! ToObject(_value_).
171 | 1. If Type(_value_) is String, return QuoteJSONString(_value_).
172 | 1. If Type(_value_) is Number, then
173 | 1. If _value_ is finite, return ! ToString(_value_).
174 | 1. Return *"null"*.
175 | 1. If Type(_value_) is BigInt, throw a *TypeError* exception.
176 | 1. If Type(_value_) is Object and IsCallable(_value_) is *false*, then
177 | 1. Let _isArray_ be ? IsArray(_value_).
178 | 1. If ! IsTuple(_value_) is *true*, then
179 | 1. Let _isArrayLike_ be *true*.
180 | 1. Else,
181 | 1. Let _isArrayLike_ be ? IsArray(_value_).
182 | 1. If _isArray__isArrayLike_ is *true*, return ? SerializeJSONArray(_state_, _value_).
183 | 1. Return ? SerializeJSONObject(_state_, _value_).
184 | 1. Return *undefined*.
185 |
186 |
187 |
188 |
189 |
190 |
--------------------------------------------------------------------------------