Proposal proposal-json-parse-with-source

Stage 3 Draft / July 20, 2022

JSON.parse source text access

1 The JSON Object

The JSON object:

The JSON Data Interchange Format is defined in ECMA-404. The JSON interchange format used in this specification is exactly that described by ECMA-404. Conforming implementations of JSON.parse and JSON.stringify must support the exact interchange format described in the ECMA-404 specification without any deletions or extensions to the format.

1.1 JSON.isRawJSON ( O )

This function performs the following steps when called:

  1. If Type(O) is Object and O has an [[IsRawJSON]] internal slot, return true.
  2. Return false.

1.2 JSON.parse ( text [ , reviver ] )

The parse 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.

  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 script be ParseText(StringToCodePoints(scriptString), Script).
  5. NOTE: The early error rules defined in 13.2.5.1 have special handling for the above invocation of ParseText.
  6. Assert: script is a Parse Node.
  7. Let completion be the result of evaluating script.
  8. NOTE: The PropertyDefinitionEvaluation semantics defined in 13.2.5.5 have special handling for the above evaluation.
  9. Let unfiltered be completion.[[Value]].
  10. Assert: unfiltered is either a String, Number, Boolean, Null, or an Object that is defined by either an ArrayLiteral or an ObjectLiteral.
  11. 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, script).
  12. Else,
    1. Return unfiltered.

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

Note

Valid JSON text is a subset of the ECMAScript PrimaryExpression syntax. Step 2 verifies that jsonString conforms to that subset, and step 10 asserts that that parsing and evaluation returns a value of an appropriate type.

However, because 13.2.5.5 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.

1.2.1 InternalizeJSONProperty ( holder, name, reviver, valNode )

The abstract operation InternalizeJSONProperty takes arguments holder (an Object), name (a String), reviver (a function object), and valNode (a Parse Node) and returns either a normal completion containing an ECMAScript language value or an abrupt completion.

Note 1

This algorithm intentionally does not throw an exception if either [[Delete]] or CreateDataProperty return false.

It performs the following steps when called:

  1. Let typedValNode be ShallowestContainedJSONValue of valNode.
  2. Assert: typedValNode is not empty.
  3. Let context be OrdinaryObjectCreate(%Object.prototype%).
  4. 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.
    2. Perform ! CreateDataPropertyOrThrow(context, "source", CodePointsToString(sourceText)).
  5. Let val be ? Get(holder, name).
  6. If Type(val) is Object, then
    1. Let isArray be ? IsArray(val).
    2. If isArray is true, then
      1. Assert: typedValNode is an ArrayLiteral Parse Node.
      2. Let contentNodes be ArrayLiteralContentNodes of typedValNode.
      3. Let contentNodesLen be the number of elements in contentNodes.
      4. Let I be 0.
      5. Let len be ? LengthOfArrayLike(val).
      6. Assert: len = contentNodesLen.
      7. Repeat, while I < len,
        1. Let prop be ! ToString(𝔽(I)).
        2. Let newElement be ? InternalizeJSONProperty(val, prop, reviver, contentNodes[I]).
        3. If newElement is undefined, then
          1. Perform ? val.[[Delete]](prop).
        4. Else,
          1. Perform ? CreateDataProperty(val, prop, newElement).
        5. Set I to I + 1.
    3. Else,
      1. Assert: typedValNode is an ObjectLiteral Parse Node.
      2. Let properties be PropertyDefinitionList of typedValNode.
      3. Let keys be ? EnumerableOwnPropertyNames(val, key).
      4. For each String P of keys, do
        1. Let propertyDefinition be empty.
        2. For each Parse Node candidate of properties, do
          1. Let propName be PropName of candidate.
          2. If SameValue(propName, P) is true, set propertyDefinition to candidate.
        3. Assert: propertyDefinition is not empty.
        4. 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.
        5. Assert: propertyDefinition is PropertyDefinition : PropertyName : AssignmentExpression .
        6. Let propertyValueNode be the AssignmentExpression of propertyDefinition.
        7. Let newElement be ? InternalizeJSONProperty(val, P, reviver, propertyValueNode).
        8. If newElement is undefined, then
          1. Perform ? val.[[Delete]](P).
        9. Else,
          1. Perform ? CreateDataProperty(val, P, newElement).
  7. Return ? Call(reviver, holder, « name, val, context »).

It is not permitted for a conforming implementation of JSON.parse 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 2

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

1.2.2 Static Semantics: ShallowestContainedJSONValue

The syntax-directed operation ShallowestContainedJSONValue takes no arguments and returns a Parse Node or empty. It performs a breadth-first search of the parse tree rooted at the context node, and returns the first node that is an instance of a nonterminal corresponding to a JSON value, or empty if there is no such node.

  1. Let F be the active function object.
  2. Assert: F is a JSON.parse built-in function object (see JSON.parse).
  3. Let types be « NullLiteral, BooleanLiteral, NumericLiteral, StringLiteral, ArrayLiteral, ObjectLiteral ».
  4. Let queue be « this Parse Node ».
  5. Repeat, while queue is not empty,
    1. Let candidate be the first element of queue.
    2. Remove the first element from queue.
    3. Set queuedChildren to false.
    4. For each nonterminal type of types, do
      1. If candidate is an instance of type, return candidate.
      2. If queuedChildren is false and candidate is an instance of a nonterminal and candidate Contains type is true, then
        1. Let children be a List containing each child node of candidate, in order.
        2. Set queue to the list-concatenation of queue and children.
        3. Set queuedChildren to true.
  6. Return empty.
Editor's Note

It may make sense to generalize this operation and define Contains in terms of it rather than vice versa, but Contains is currently limited to a single target nonterminal and has specialized treatment for e.g. ClassTail descending into ClassBody exclusively to inspect computed names.

1.3 JSON.rawJSON ( text )

The rawJSON function returns an object representing raw JSON text of a string, number, boolean, or null value.

  1. Let jsonString be ? ToString(text).
  2. Throw a SyntaxError exception if jsonString is the empty String, or if either the first or last code unit of jsonString is any of 0x0009 (CHARACTER TABULATION), 0x000A (LINE FEED), 0x000D (CARRIAGE RETURN), or 0x0020 (SPACE).
  3. 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, or if its outermost value is an object or array as defined in that specification.
  4. Let internalSlotsList be « [[IsRawJSON]] ».
  5. Let obj be OrdinaryObjectCreate(null, internalSlotsList).
  6. Perform ! CreateDataPropertyOrThrow(obj, "rawJSON", jsonString).
  7. Perform ! SetIntegrityLevel(obj, frozen).
  8. Return obj.

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

The stringify function returns a String in UTF-16 encoded JSON format representing an ECMAScript language value, or undefined. It can take three parameters. The value parameter is an ECMAScript language value, which is usually an object or array, although it can also be a String, Boolean, Number or null. The optional replacer parameter is either a function that alters the way objects and arrays are stringified, or an array of Strings and Numbers that acts as an inclusion list for selecting the object properties that will be stringified. The optional space parameter is a String or Number that allows the result to have white space injected into it to improve human readability.

1.4.1 SerializeJSONProperty ( state, key, holder )

The abstract operation SerializeJSONProperty takes arguments state, key, and holder and returns either a normal completion containing either undefined or a String, or an abrupt completion. 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 an [[IsRawJSON]] internal slot, then
      1. Return ! Get(value, "rawJSON").
    2. If value has a [[NumberData]] internal slot, then
      1. Set value to ? ToNumber(value).
    3. Else if value has a [[StringData]] internal slot, then
      1. Set value to ? ToString(value).
    4. Else if value has a [[BooleanData]] internal slot, then
      1. Set value to value.[[BooleanData]].
    5. 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 String, return QuoteJSONString(value).
  9. If Type(value) is Number, then
    1. If value is finite, return ! ToString(value).
    2. Return "null".
  10. If Type(value) is BigInt, throw a TypeError exception.
  11. If Type(value) is Object and IsCallable(value) is false, then
    1. Let isArray be ? IsArray(value).
    2. If isArray is true, return ? SerializeJSONArray(state, value).
    3. Return ? SerializeJSONObject(state, value).
  12. Return undefined.

2 Static Semantics: ArrayLiteralContentNodes

The syntax-directed operation ArrayLiteralContentNodes takes no arguments and returns a List of Parse Nodes. It is defined piecewise over the following productions:

ArrayLiteral : [ Elisionopt ] [ ElementList ] [ ElementList , Elisionopt ]
  1. Set elements to a new empty List.
  2. If ElementList is present, set elements to the list-concatenation of elements and ArrayLiteralContentNodes of ElementList.
  3. If Elision is present, append Elision to elements.
  4. Return elements.
ElementList : Elisionopt AssignmentExpression
  1. Set elements to a new empty List.
  2. If Elision is present, append Elision to elements.
  3. Return the list-concatenation of elements and « AssignmentExpression ».
ElementList : Elisionopt SpreadElement
  1. Set elements to a new empty List.
  2. If Elision is present, append Elision to elements.
  3. Return the list-concatenation of elements and « SpreadElement ».
ElementList : ElementList , Elisionopt AssignmentExpression
  1. Set elements to ArrayLiteralContentNodes of ElementList.
  2. If Elision is present, append Elision to elements.
  3. Return the list-concatenation of elements and « AssignmentExpression ».
ElementList : ElementList , Elisionopt SpreadElement
  1. Set elements to ArrayLiteralContentNodes of ElementList.
  2. If Elision is present, append Elision to elements.
  3. Return the list-concatenation of elements and « SpreadElement ».

3 Static Semantics: PropertyDefinitionList

The syntax-directed operation PropertyDefinitionList takes no arguments and returns a List of Parse Nodes. It is defined piecewise over the following productions:

ObjectLiteral : { }
  1. Return a new empty List.
PropertyDefinitionList : PropertyDefinition
  1. Return « PropertyDefinition ».
PropertyDefinitionList : PropertyDefinitionList , PropertyDefinition
  1. Return the list-concatenation of PropertyDefinitionList of PropertyDefinitionList and « PropertyDefinition ».

A Copyright & Software License

Copyright Notice

© 2022 Richard Gibson

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.