├── .gitattributes ├── .github └── workflows │ └── build.yml ├── .gitignore ├── .npmrc ├── LICENSE ├── README.md ├── biblio.json ├── index.html ├── package.json └── spec.emu /.gitattributes: -------------------------------------------------------------------------------- 1 | index.html -diff merge=ours 2 | spec.js -diff merge=ours 3 | spec.css -diff merge=ours 4 | -------------------------------------------------------------------------------- /.github/workflows/build.yml: -------------------------------------------------------------------------------- 1 | name: Deploy spec 2 | 3 | on: [push] 4 | 5 | jobs: 6 | build: 7 | runs-on: ubuntu-latest 8 | 9 | steps: 10 | - uses: actions/checkout@v2 11 | - uses: actions/setup-node@v1 12 | with: 13 | node-version: '12.x' 14 | - run: npm install 15 | - run: npm run build 16 | - name: commit changes 17 | uses: elstudio/actions-js-build/commit@v3 18 | with: 19 | commitMessage: "fixup: [spec] `npm run build`" 20 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | 6 | # Runtime data 7 | pids 8 | *.pid 9 | *.seed 10 | 11 | # Directory for instrumented libs generated by jscoverage/JSCover 12 | lib-cov 13 | 14 | # Coverage directory used by tools like istanbul 15 | coverage 16 | 17 | # nyc test coverage 18 | .nyc_output 19 | 20 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 21 | .grunt 22 | 23 | # node-waf configuration 24 | .lock-wscript 25 | 26 | # Compiled binary addons (http://nodejs.org/api/addons.html) 27 | build/Release 28 | 29 | # Dependency directories 30 | node_modules 31 | jspm_packages 32 | 33 | # Optional npm cache directory 34 | .npm 35 | 36 | # Optional REPL history 37 | .node_repl_history 38 | 39 | # Only apps should have lockfiles 40 | yarn.lock 41 | package-lock.json 42 | npm-shrinkwrap.json 43 | pnpm-lock.yaml 44 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 ECMA TC39 and contributors 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # JSON.parseImmutable Proposal 2 | 3 | ## Status 4 | 5 | Stage 2 6 | 7 | **Champions:** 8 | 9 | - Robin Ricard (Bloomberg) 10 | - Rick Button (Bloomberg) 11 | - Nicolò Ribaudo (Igalia) 12 | - Ashley Claymore (Bloomberg) 13 | 14 | ## Overview 15 | 16 | This proposal complements the [Records and Tuples Proposal][rec-tup-proposal]. 17 | It was originally part of that proposal but split off into a separate proposal to reduce the scope of the core Records and Tuples proposal. [#330](https://github.com/tc39/proposal-record-tuple/issues/330) 18 | 19 | The problem being explored is ergonomic and efficient creation of a deeply immutable data structure from a [JSON][json-mdn] string. 20 | 21 | ```javascript 22 | JSON.parse(data, (key, value) => { 23 | if (typeof value === 'object' && value !== null) { 24 | if (Array.isArray(value)) { 25 | return Tuple.from(value); 26 | } else { 27 | return Record(value); 28 | } 29 | } 30 | return value; 31 | }); 32 | ``` 33 | 34 | Could be replaced with: 35 | 36 | ```javascript 37 | JSON.parseImmutable(data); 38 | ``` 39 | 40 | 41 | [rec-tup-proposal]: https://github.com/tc39/proposal-record-tuple 42 | [json-mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON 43 | -------------------------------------------------------------------------------- /biblio.json: -------------------------------------------------------------------------------- 1 | { 2 | "location": "https://tc39.es/proposal-record-tuple/", 3 | "entries": [ 4 | { 5 | "type": "op", 6 | "aoid": "CreateRecord", 7 | "refId": "sec-createrecord", 8 | "kind": "abstract operation", 9 | "signature": { 10 | "parameters": [ 11 | { 12 | "name": "_entries_", 13 | "type": { 14 | "kind": "list", 15 | "elements": { 16 | "kind": "record", 17 | "fields": { 18 | "[[Key]]": { "kind": "opaque", "type": "a String" }, 19 | "[[Value]]": { 20 | "kind": "opaque", 21 | "type": "an ECMAScript language value" 22 | } 23 | } 24 | } 25 | } 26 | } 27 | ], 28 | "optionalParameters": [], 29 | "return": null 30 | }, 31 | "effects": [] 32 | }, 33 | { 34 | "type": "clause", 35 | "id": "sec-createrecord", 36 | "aoid": "CreateRecord", 37 | "title": "CreateRecord ( entries )", 38 | "titleHTML": "CreateRecord ( entries )", 39 | "number": "3.3.1" 40 | }, 41 | { 42 | "type": "clause", 43 | "id": "sec-tuple-value", 44 | "aoid": null, 45 | "title": "Tuple value", 46 | "titleHTML": "Tuple value", 47 | "number": "1.2.4" 48 | } 49 | ] 50 | } 51 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | JSON.parseImmutable
2519 |

Stage 2 Draft / July 28, 2022

JSON.parseImmutable

2528 | 2529 | 2530 | 2531 | 2532 |

1 Structured Data

2533 | 2534 | 2535 |

1.1 The JSON Object

2536 | 2537 | 2538 |

1.1.1 JSON.parse ( text [ , reviver ] )

2539 |

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.

2541 |
  1. Let jsonString be ? ToString(text).
  2. 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.
  3. Let scriptString be the string-concatenation of "(", jsonString, and ");".
  4. 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.
  5. Let unfiltered be completion.[[Value]].
  6. Assert: unfiltered is either a String, Number, Boolean, Null, or an Object that is defined by either an ArrayLiteral or an ObjectLiteral.
  7. Let unfiltered be ? ParseJSONText(text).
  8. If IsCallable(reviver) is true, then
    1. Let root be OrdinaryObjectCreate(%Object.prototype%).
    2. Let rootName be the empty String.
    3. Perform ! CreateDataPropertyOrThrow(root, rootName, unfiltered).
    4. Return ? InternalizeJSONProperty(root, rootName, reviver).
  9. Else,
    1. Return unfiltered.
2542 |

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:

2548 |
  1. Let jsonString be ? ToString(text).
  2. 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.
  3. Let scriptString be the string-concatenation of "(", jsonString, and ");".
  4. 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.
  5. Let unfiltered be completion.[[Value]].
  6. Assert: unfiltered is either a String, Number, Boolean, Null, or an Object that is defined by either an ArrayLiteral or an ObjectLiteral.
  7. 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.

2556 |
  1. Let unfiltered be ? ParseJSONText(text).
  2. Return ? BuildImmutableProperty(unfiltered, "", reviver).
2557 |

This function is the %JSONParseImmutable% intrinsic object.

2558 |

The "length" property of the parseImmutable function is 2.

2559 | 2560 | 2561 |

1.1.2.1 BuildImmutableProperty ( value, name, reviver )

2562 |

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:

2563 |
  1. If Type(value) is Object, then
    1. Let isArray be ? IsArray(val).
    2. If isArray is true, then
      1. Let items be a new empty List.
      2. Let I be 0.
      3. Let len be ? LengthOfArrayLike(val).
      4. Repeat, while I < len,
        1. Let childName be ! ToString(I).
        2. Let childValue be ? Get(value, ! childName).
        3. Let newElement be ? BuildImmutableProperty(childValue, childName, reviver).
        4. Assert: Type(newElement) is not Object.
        5. Append newElement to items.
        6. Set I to I + 1.
      5. Let immutable be a new Tuple value whose [[Sequence]] is items.
    3. Else,
      1. Let props be ? EnumerableOwnPropertyNames(obj, key+value).
      2. Let fields be a new empty List.
      3. For each element prop of props, do
        1. Let childName be ! GetV(prop, "0").
        2. Let childValue be ! GetV(prop, "1").
        3. Let newElement be ? BuildImmutableProperty(childValue, childName, reviver).
        4. Assert: Type(newElement) is not Object.
        5. If newElement is not undefined, then
          1. Let field be the Record { [[Key]]: childName, [[Value]]: newElement }.
          2. Append field to fields.
      4. Let immutable be ! CreateRecord(fields).
  2. Else,
    1. Let immutable be value.
  3. If IsCallable(reviver) is true, then
    1. Let immutable be ? Call(reviver, undefined, « name, immutable »).
    2. If Type(immutable) is Object, throw a TypeError exception.
  4. 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 ] ] )

2581 | 2582 | 2583 |

1.1.3.1 SerializeJSONProperty ( state, key, holder )

2584 |

The abstract operation SerializeJSONProperty takes arguments state, key, and holder. It performs the following steps when called:

2585 |
  1. Let value be ? Get(holder, key).
  2. If Type(value) is Object or BigInt, then
    1. Let toJSON be ? GetV(value, "toJSON").
    2. If IsCallable(toJSON) is true, then
      1. Set value to ? Call(toJSON, value, « key »).
  3. If state.[[ReplacerFunction]] is not undefined, then
    1. Set value to ? Call(state.[[ReplacerFunction]], holder, « key, value »).
  4. If Type(value) is Object, then
    1. If value has a [[NumberData]] internal slot, then
      1. Set value to ? ToNumber(value).
    2. Else if value has a [[StringData]] internal slot, then
      1. Set value to ? ToString(value).
    3. Else if value has a [[BooleanData]] internal slot, then
      1. Set value to value.[[BooleanData]].
    4. Else if value has a [[BigIntData]] internal slot, then
      1. Set value to value.[[BigIntData]].
  5. If value is null, return "null".
  6. If value is true, return "true".
  7. If value is false, return "false".
  8. If Type(value) is Record or Type(value) is Tuple, set value to ! ToObject(value).
  9. If Type(value) is String, return QuoteJSONString(value).
  10. If Type(value) is Number, then
    1. If value is finite, return ! ToString(value).
    2. Return "null".
  11. If Type(value) is BigInt, throw a TypeError exception.
  12. If Type(value) is Object and IsCallable(value) is false, then
    1. Let isArray be ? IsArray(value).
    2. If ! IsTuple(value) is true, then
      1. Let isArrayLike be true.
    3. Else,
      1. Let isArrayLike be ? IsArray(value).
    4. If isArrayisArrayLike is true, return ? SerializeJSONArray(state, value).
    5. Return ? SerializeJSONObject(state, value).
  13. Return undefined.
2586 |
2587 |
2588 |
2589 |
2590 |

A Copyright & Software License

2591 | 2592 |

Copyright Notice

2593 |

© 2022 Robin Ricard, Rick Button, Ashley Claymore, Nicolò Ribaudo

2594 | 2595 |

Software License

2596 |

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 |
  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  2. 2602 |
  3. 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.
  4. 2603 |
  5. 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.
  6. 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.

2607 | 2608 |
2609 |
-------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "private": true, 3 | "name": "template-for-proposals", 4 | "description": "A repository template for ECMAScript proposals.", 5 | "scripts": { 6 | "start": "npm run build-loose -- --watch", 7 | "build": "npm run build-loose -- --strict", 8 | "build-loose": "ecmarkup --load-biblio @tc39/ecma262-biblio --verbose spec.emu index.html --lint-spec" 9 | }, 10 | "homepage": "https://github.com/tc39/template-for-proposals#readme", 11 | "repository": { 12 | "type": "git", 13 | "url": "git+https://github.com/tc39/template-for-proposals.git" 14 | }, 15 | "license": "MIT", 16 | "devDependencies": { 17 | "@tc39/ecma262-biblio": "2.1.2340", 18 | "ecmarkup": "^12.1.0" 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /spec.emu: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 |
  7 | title: JSON.parseImmutable
  8 | stage: 2
  9 | contributors: Robin Ricard, Rick Button, Ashley Claymore, Nicolò Ribaudo
 10 | 
11 | 12 | 13 | 14 | 15 |

Structured Data

16 | 17 | 18 |

The JSON Object

19 | 20 | 21 |

JSON.parse ( _text_ [ , _reviver_ ] )

22 |

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.

71 | 72 | 1. Let _unfiltered_ be ? ParseJSONText(_text_). 73 | 1. Return ? BuildImmutableProperty(_unfiltered_, *""*, _reviver_). 74 | 75 |

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.
  • 134 |
135 |
136 |
137 | 138 | 139 |

JSON.stringify ( _value_ [ , _replacer_ [ , _space_ ] ] )

140 | 141 | 142 |

143 | SerializeJSONProperty ( 144 | _state_: unknown, 145 | _key_: unknown, 146 | _holder_: unknown, 147 | ) 148 |

149 |
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 | --------------------------------------------------------------------------------