diff --git a/infra.bs b/infra.bs index 91f1360..ee17a52 100644 --- a/infra.bs +++ b/infra.bs @@ -14,7 +14,14 @@ urlPrefix: https://tc39.github.io/ecma262/; spec: ECMA-262; text: %JSONStringify%; url: #sec-json.stringify text: List; url: sec-list-and-record-specification-type text: The String Type; url: sec-ecmascript-language-types-string-type - type: abstract-op; text: Call; url: sec-call + text: realm; url: realm + type: abstract-op; + text: Call; url: sec-call + text: Get; url: sec-get-o-p + text: IsArray; url: sec-isarray + text: ToLength; url: sec-tolength + text: ToString; url: sec-tostring + text: Type; url: sec-ecmascript-data-types-and-values @@ -1074,18 +1081,16 @@ as 200/`OK`.

JSON

+

The conventions used in the algorithms in this section are those of the JavaScript +specification. [[!ECMA-262]] +

To parse JSON from bytes given bytes, run these steps:

  1. Let jsonText be the result of running UTF-8 decode on bytes. [[!ENCODING]] -

  2. -

    Return ? Call(%JSONParse%, undefined, « jsonText »). - [[!ECMA-262]] - -

    The conventions used in this step are those of the JavaScript specification. - +

  3. Return ? Call(%JSONParse%, undefined, « jsonText »).

To serialize JSON to bytes a given JavaScript value value, run these @@ -1093,16 +1098,82 @@ steps:

  1. -

    Let jsonString be the result of +

    Let jsonString be ? Call(%JSONStringify%, undefined, « value »). - [[!ECMA-262]] -

    The conventions used in this step are those of the JavaScript specification. - Also, since no additional arguments are passed to %JSONStringify%, the resulting string - will have no whitespace inserted. + +

    Since no additional arguments are passed to %JSONStringify%, the resulting + string will have no whitespace inserted.

  2. Return the result of running UTF-8 encode on jsonString. [[!ENCODING]]

+
+ +

The above two operations operate on JavaScript values directly; in particular, this means that +the involved objects or arrays are tied to a particular JavaScript realm. In +standards, it is often more convenient to parse JSON into realm-independent maps, +lists, strings, booleans, numbers, and nulls. + +

To parse JSON into maps and lists, given a string +jsonText: + +

    +
  1. Let |jsValue| be ? [$Call$](%JSONParse%, undefined, « |jsonText| »). + +

  2. Return the result of [=converting a JSON-derived JavaScript value to an Infra value=], given + |jsValue|. +

+ +

To convert a JSON-derived JavaScript value to an Infra value, +given a JavaScript value jsValue: + +

    +
  1. If [$Type$](|jsValue|) is Null, String, or Number, then return |jsValue|. + +

  2. +

    If [$IsArray$](|jsValue|) is true, then: + +

      +
    1. Let |result| be an empty [=list=]. + +

    2. Let |length| be ! [$ToLength$](! [$Get$](|jsValue|, "`length`")). + +

    3. +

      [=list/For each=] |index| of [=the range=] 0 to |length| − 1, inclusive: + +

        +
      1. Let |indexName| be ! [$ToString$](|index|). + +

      2. Let |jsValueAtIndex| be ! [$Get$](|jsValue|, |indexName|). + +

      3. Let |infraValueAtIndex| be the result of [=converting a JSON-derived JavaScript value to an Infra value=], + given |jsValueAtIndex|. + +

      4. [=list/Append=] |infraValueAtIndex| to |result|. +

      +
    4. + +
    5. Return |result|. +

    +
  3. + +
  4. Let |result| be an empty [=ordered map=]. + +

  5. +

    [=list/For each=] |key| of ! |jsValue|.\[[OwnPropertyKeys]](): + +

      +
    1. Let |jsValueAtKey| be ! [$Get$](|jsValue|, |key|). + +

    2. Let |infraValueAtKey| be the result of [=converting a JSON-derived JavaScript value to an Infra value=], + given |jsValueAtKey|. + +

    3. [=map/Set=] |result|[|key|] to |infraValueAtKey|. +

    +
  6. + +
  7. Return |result|. +

Forgiving base64