From 4295a8df596c21b2815b96483d168ab97e8de468 Mon Sep 17 00:00:00 2001 From: Domenic Denicola Date: Thu, 1 Oct 2020 16:58:36 -0400 Subject: [PATCH] =?UTF-8?q?Add=20Infra=20=E2=86=92=20JSON=20conversion=20a?= =?UTF-8?q?lgorithms?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Closes #336. --- infra.bs | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/infra.bs b/infra.bs index 1487c10..dfee465 100644 --- a/infra.bs +++ b/infra.bs @@ -18,9 +18,12 @@ urlPrefix: https://tc39.github.io/ecma262/#; spec: ECMA-262; text: realm; url: realm type: method; for: Array; text: sort(); url: sec-array.prototype.sort type: abstract-op; + text: ArrayCreate; url: sec-arraycreate text: Call; url: sec-call + text: CreateDataPropertyOrThrow; url: sec-createdatapropertyorthrow text: Get; url: sec-get-o-p text: IsArray; url: sec-isarray + text: OrdinaryObjectCreate; url: sec-ordinaryobjectcreate text: ToLength; url: sec-tolength text: ToString; url: sec-tostring text: Type; url: sec-ecmascript-data-types-and-values @@ -1522,6 +1525,82 @@ given a JavaScript value jsValue:
  • Return |result|. +

    To serialize an Infra value to a JSON string, +given a string, boolean, number, null, list, or string-keyed map +|value|: + +

      +
    1. Let |jsValue| be the result of + converting an Infra value to a JSON-compatible JavaScript value, given |value|. + +

    2. +

      Return ! [$Call$](%JSON.stringify%, undefined, « |jsValue| »). + +

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

    + +

    To serialize an Infra value to JSON bytes, +given a string, boolean, number, null, list, or string-keyed map +|value|: + +

      +
    1. Let |string| be the result of serializing an Infra value to a JSON string, given + |value|. + +

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

    + +

    To convert an Infra value to a JSON-compatible JavaScript value, +given |value|: + +

      +
    1. If |value| is a string, boolean, number, or null, then return |value|. + +

    2. +

      If |value| is a list, then: + +

        +
      1. Let |jsValue| be ! [$ArrayCreate$](0). + +

      2. Let |i| be 0. + +

      3. +

        For each |listItem| of |value|: + +

          +
        1. Let |listItemJSValue| be the result of + converting an Infra value to a JSON-compatible JavaScript value, given |listItem|. + +

        2. Perform ! [$CreateDataPropertyOrThrow$](|jsValue|, ! [$ToString$](|i|), + |listItemJSValue|). + +

        3. Set |i| to |i| + 1. +

        + +
      4. Return |jsValue|. +

      + +
    3. Assert: |value| is a map. + +

    4. Let |jsValue| be ! [$OrdinaryObjectCreate$](null). + +

    5. +

      For each |mapKey| → |mapValue| of |value|: + +

        +
      1. Assert: |mapKey| is a string. + +

      2. Let |mapValueJSValue| be the result of + converting an Infra value to a JSON-compatible JavaScript value, given |mapValue|. + +

      3. Perform ! [$CreateDataPropertyOrThrow$](|jsValue|, |mapKey|, |mapValueJSValue|). +

      + +
    6. Return |jsValue|. +

    + +

    Forgiving base64