Stage 2 Draft / July 28, 2022

JSON.parseImmutable

1 Structured Data

1.1 The JSON Object

1.1.1 JSON.parse ( text [ , reviver ] )

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.

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.

  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.

This function is the %JSONParse% intrinsic object.

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

1.1.1.1 ParseJSONText ( text )

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:

  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.

1.1.2 JSON.parseImmutable ( text [ , reviver ] )

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.

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.

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

This function is the %JSONParseImmutable% intrinsic object.

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

1.1.2.1 BuildImmutableProperty ( value, name, reviver )

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:

  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.

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.

Note

In the case where there are duplicate name Strings within an object, lexically preceding values for the same key shall be overwritten.

Note

There are two differences in the behaviour of the reviver parameter of the JSON.parse and JSON.parseImmutable functions:

  • JSON.parse passes the parent object as the this value, while JSON.parseImmutable passes undefined.
  • 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.

1.1.3 JSON.stringify ( value [ , replacer [ , space ] ] )

1.1.3.1 SerializeJSONProperty ( state, key, holder )

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

  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.

A Copyright & Software License

Copyright Notice

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

Software License

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.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

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

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.