Proposal proposal-json-parse-with-source

Stage 3 Draft / September 26, 2023

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 ] )

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 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).
  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 Completion(Evaluation of 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. Let snapshot be CreateJSONParseRecord(script, rootName, unfiltered).
    5. Return ? InternalizeJSONProperty(root, rootName, reviver, snapshot).
  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 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 Table 1.

Table 1: JSON Parse Record Fields
Field Name Value Meaning
[[ParseNode]] a Parse Node The context Parse Node.
[[Key]] a property name The property name with which [[Value]] is associated.
[[Value]] an ECMAScript language value The value produced by evaluation of [[ParseNode]].
[[Elements]] a List of JSON Parse Records JSON 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 Records JSON 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.

1.2.2 CreateJSONParseRecord ( parseNode, key, val )

The abstract operation CreateJSONParseRecord takes arguments parseNode (a Parse Node), key (a property name), and val (an ECMAScript language value) and returns a JSON Parse Record. It recursively combines a parseNode parsed from JSON text and the val produced by its evaluation. It performs the following steps when called:

  1. Let typedValNode be ShallowestContainedJSONValue of parseNode.
  2. Assert: typedValNode is not empty.
  3. Let elements be a new empty List.
  4. Let entries be a new empty List.
  5. If val is an 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 len be the number of elements in contentNodes.
      4. Let valLen be ! LengthOfArrayLike(val).
      5. Assert: valLen = len.
      6. Let I be 0.
      7. Repeat, while I < len,
        1. Let propName be ! ToString(𝔽(I)).
        2. Let elementParseRecord be CreateJSONParseRecord(contentNodes[I], propName, ! Get(val, propName)).
        3. Append elementParseRecord to elements.
        4. Set I to I + 1.
    3. Else,
      1. Assert: typedValNode is an ObjectLiteral Parse Node.
      2. Let propertyNodes be PropertyDefinitionNodes of typedValNode.
      3. NOTE: Because val was produced from JSON text and has not been modified, all of its property keys are Strings and will be exhaustively enumerated in source text order.
      4. Let keys be ! EnumerableOwnProperties(val, key).
      5. For each String P of keys, do
        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.
        2. Let propertyDefinition be empty.
        3. For each Parse Node propertyNode of propertyNodes, do
          1. Let propName be PropName of propertyNode.
          2. If SameValue(propName, P) is true, set propertyDefinition to propertyNode.
        4. Assert: propertyDefinition is PropertyDefinition : PropertyName : AssignmentExpression .
        5. Let propertyValueNode be the AssignmentExpression of propertyDefinition.
        6. Let entryParseRecord be CreateJSONParseRecord(propertyValueNode, P, ! Get(val, P)).
        7. Append entryParseRecord to entries.
  6. Else,
    1. Assert: typedValNode is not an ArrayLiteral Parse Node and not an ObjectLiteral Parse Node.
  7. Return the JSON Parse Record { [[ParseNode]]: typedValNode, [[Key]]: key, [[Value]]: val, [[Elements]]: elements, [[Entries]]: entries }.

1.2.3 InternalizeJSONProperty ( holder, name, reviver, parseRecord )

The abstract operation InternalizeJSONProperty takes arguments holder (an Object), name (a String), reviver (a function object), and parseRecord (either a JSON Parse Record or empty) and returns either a normal completion containing an ECMAScript language value or a throw 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 val be ? Get(holder, name).
  2. Let context be OrdinaryObjectCreate(%Object.prototype%).
  3. If parseRecord is a JSON Parse Record and SameValue(parseRecord.[[Value]], val) is true, then
    1. If val is not an Object, then
      1. Let parseNode be parseRecord.[[ParseNode]].
      2. Assert: parseNode is not an ArrayLiteral Parse Node and not an ObjectLiteral Parse Node.
      3. Let sourceText be the source text matched by parseNode.
      4. Perform ! CreateDataPropertyOrThrow(context, "source", CodePointsToString(sourceText)).
    2. Let elementRecords be parseRecord.[[Elements]].
    3. Let entryRecords be parseRecord.[[Entries]].
  4. Else,
    1. Let elementRecords be a new empty List.
    2. Let entryRecords be a new empty List.
  5. If val is an Object, then
    1. Let isArray be ? IsArray(val).
    2. If isArray is true, then
      1. Let elementRecordsLen be the number of elements in elementRecords.
      2. Let len be ? LengthOfArrayLike(val).
      3. Let I be 0.
      4. Repeat, while I < len,
        1. Let prop be ! ToString(𝔽(I)).
        2. If I < elementRecordsLen, let elementRecord be elementRecords[I]. Otherwise, let elementRecord be empty.
        3. Let newElement be ? InternalizeJSONProperty(val, prop, reviver, elementRecord).
        4. If newElement is undefined, then
          1. Perform ? val.[[Delete]](prop).
        5. Else,
          1. Perform ? CreateDataProperty(val, prop, newElement).
        6. Set I to I + 1.
    3. Else,
      1. Let keys be ? EnumerableOwnProperties(val, key).
      2. For each String P of keys, do
        1. Let entryRecord be the element of entryRecords whose [[Key]] field is P. If there is no such element, let entryRecord be empty.
        2. Let newElement be ? InternalizeJSONProperty(val, P, reviver, entryRecord).
        3. If newElement is undefined, then
          1. Perform ? val.[[Delete]](P).
        4. Else,
          1. Perform ? CreateDataProperty(val, P, newElement).
  6. 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.4 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, UnaryExpression ».
  4. Let unaryExpression be empty.
  5. Let queue be « this Parse Node ».
  6. Repeat, while queue is not empty,
    1. Let candidate be the first element of queue.
    2. Remove the first element from queue.
    3. Let queuedChildren be false.
    4. For each nonterminal type of types, do
      1. If candidate is an instance of type, then
        1. NOTE: In the JSON grammar, a number token may represent a negative value. In ECMAScript, negation is represented as a unary operation.
        2. If type is UnaryExpression, then
          1. Set unaryExpression to candidate.
        3. Else if type is NumericLiteral, then
          1. Assert: unaryExpression Contains candidate is true.
          2. Return unaryExpression.
        4. Else,
          1. 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.
  7. 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 (a JSON Serialization Record), key (a String), and holder (an Object) and returns either a normal completion containing either undefined or a String, or a throw completion. It performs the following steps when called:

  1. Let value be ? Get(holder, key).
  2. If value is an Object or value is a 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 value is an 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 value is a String, return QuoteJSONString(value).
  9. If value is a Number, then
    1. If value is finite, return ! ToString(value).
    2. Return "null".
  10. If value is a BigInt, throw a TypeError exception.
  11. If value is an 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. Let elements be 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. Let elements be 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. Let elements be 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. Let elements be 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. Let elements be ArrayLiteralContentNodes of ElementList.
  2. If Elision is present, append Elision to elements.
  3. Return the list-concatenation of elements and « SpreadElement ».

3 Static Semantics: PropertyDefinitionNodes

The syntax-directed operation PropertyDefinitionNodes 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 PropertyDefinitionNodes of PropertyDefinitionList and « PropertyDefinition ».

A Copyright & Software License

Copyright Notice

© 2023 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.