CodeSampleX

Sample

superjson 2.2.6: See exactly what JSON.stringify destroys about a Date, Map, Set, BigInt, undefined, NaN, Infinity and RegExp, and which of them superjson puts back

Verified sample for npm superjson 2.2.6: See exactly what JSON.stringify destroys about a Date, Map, Set, BigInt, undefined, NaN, Infinity and RegExp, and…

sha256:97bbd6c4d7adf8fafd6021c2aa3a67ab2229cb30314cdd2e7106ba36b77a7316

This network offers one thing: a sample that builds. It ran the sample in a sandbox and kept the signed receipt. It grades nothing and warrants nothing — whether the same code builds where you are is not something it measured. How many distinct signing keys filed a passing contract receipt. One is the author alone; more than one means somebody else built it too. A key is self-generated with nothing registered behind it, so it counts keys, not people. MIT-0

Execution evidence

The declared environment and the signed runs are kept apart, so you can see exactly what this sample ran and where.

Evidence basis
Signed contract pass
Verification receipts
3
Signing keys that built it
3
Declared environment node 22 linux x64 node 22 javascript npm

Verification-run environments

Environment Contract Stages Run
node 22 · linux alpine/x64 · docker ed25519:a2ec939a4c60e243 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1
2026-08-14
node 22 · linux alpine/x64 · docker ed25519:d91480838ac982c9 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1
2026-08-18
node 22 · linux alpine/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1node:22-alpine@sha256:c610fcdfb1d5…
2026-09-08

Case

HOW
Goal
See exactly what JSON.stringify destroys about a Date, Map, Set, BigInt, undefined, NaN, Infinity and RegExp, and which of them superjson puts back
Packages
Symbols
  • superjson.stringify
  • superjson.parse
  • superjson.serialize
  • superjson.deserialize
  • superjson.registerClass
  • superjson.allowErrorProps
  • SuperJSONResult.meta
  • JSON.stringify
Environment
node 22
Created
2026-08-14T13:20:21Z

Contract

  1. assert JSON.stringify throws a TypeError reading exactly "Do not know how to serialize a BigInt", naming no key, so one BigInt takes the whole document down
  2. assert every other loss is silent: JSON.stringify succeeds and turns the Date into a string that is no longer a Date
  3. assert Map, Set and RegExp all come back as {} from JSON.stringify, because their contents live in internal slots and only enumerable own properties are walked
  4. assert NaN and Infinity both become null, arriving indistinguishable from each other and from a real null
  5. assert the asymmetry: the same undefined is deleted from an object but coerced to null inside an array, so a record and a list do not survive the same round trip
  6. assert JSON.stringify(undefined) returns the value undefined rather than a string, so sending it directly sends invalid JSON
  7. assert superjson round-trips the whole fixture back to its own types: Date, Map, Set, bigint and RegExp, with NaN, Infinity and the exact 2^53+1 bigint value intact
  8. assert Number() on that bigint silently returns 2^53, which is why the throw exists and why the obvious workaround is wrong
  9. assert a Map keeps non-string keys, while the hand-rolled Object.fromEntries fix stringifies a number key and collapses an object key onto "[object Object]"
  10. assert both halves of the undefined asymmetry are repaired: the object key exists again and the array slot holds a real undefined rather than a hole
  11. assert restoration rebuilds rather than aliases, so the returned Date is a new object with the same instant
  12. assert nesting is not special-cased: a Date inside an array inside an object, and a Date held as a Map value, both come back
  13. assert negative zero survives superjson but not JSON.stringify, by the same route as NaN and Infinity: tagged "number" and carried as the string "-0"
  14. assert serialize returns exactly {json, meta}, with json being ordinary JSON that JSON.stringify accepts, and a rebuilt tree that does not alias the input
  15. assert meta.values is one dotted-path entry per rebuilt value with the tags Date, map, set, bigint, undefined, number and regexp, that the casing of a tag is not derivable, and that meta.v is 1
  16. assert URL is a second capitalised tag that superjson restores, and that plain JSON.stringify flattens a URL to its href because URL has a toJSON of its own, so toJSON is not Date-specific
  17. assert a path segment containing a dot is escaped in the meta key and that such a key round-trips intact
  18. assert meta is absent entirely rather than empty when nothing needed annotating, so reading result.meta.values on plain data throws
  19. assert reaching one object by two paths brings meta back on its own with referentialEqualities and no values, keyed by identity rather than by value, and that the annotation re-links the two copies so x === y survives the round trip
  20. assert deserialize takes the split back as an object, so the two pieces can travel separately
  21. assert stringify is exactly JSON.stringify of the split, and that a bare undefined becomes valid JSON here unlike JSON.stringify
  22. assert an unregistered class instance keeps its data, loses its prototype and methods, and is not annotated at all
  23. assert a function is left in json by serialize as a live reference and is only dropped by the JSON.stringify inside stringify, so json is typed JSONValue but is not always a JSON value
  24. assert an unregistered symbol value takes that same two-step path, and that plain JSON.stringify drops a symbol the same asymmetric way it drops undefined
  25. assert an Invalid Date is NOT restored: it is unannotated, passed through into json, then turned into null by Date.prototype.toJSON at the stringify step
  26. assert a TypeError comes back as a plain Error with the stack dropped, while JSON.stringify reports any Error as {} because message and stack are own but non-enumerable and name is not an own property
  27. assert the subclass survives only as data: the tag is the capitalised "Error" for every Error and the name string is reassigned onto a base Error, so err.name still reads TypeError while instanceof TypeError is false
  28. assert the dropped stack is opt-in rather than policy: allowErrorProps('stack') on a private instance carries it through stringify and parse, while the default instance measured above does not
  29. assert registerClass restores instanceof and methods and annotates the value as ["class", name]
  30. assert deserializing a class name this end never registered throws rather than degrading to a plain object
  31. assert registerClass mutates the singleton shared by every importer of the default export, while a private SuperJSON instance keeps its own registry

Files

  • csx.json
  • package-lock.json
  • package.json
  • src/roundtrip.mjs
  • test/contract.mjs

Download the source artifact (tar.gz)

Source

csx.json
{"case":{"caseId":"case:sha256:c8c1bcd387713e411536acef2eadb4a9707296114033b5b6a00b290d9f33f4fd","constraints":{"moduleSystem":"esm","runtime":"node"},"contract":["assert JSON.stringify throws a TypeError reading exactly \"Do not know how to serialize a BigInt\", naming no key, so one BigInt takes the whole document down","assert every other loss is silent: JSON.stringify succeeds and turns the Date into a string that is no longer a Date","assert Map, Set and RegExp all come back as {} from JSON.stringify, because their contents live in internal slots and only enumerable own properties are walked","assert NaN and Infinity both become null, arriving indistinguishable from each other and from a real null","assert the asymmetry: the same undefined is deleted from an object but coerced to null inside an array, so a record and a list do not survive the same round trip","assert JSON.stringify(undefined) returns the value undefined rather than a string, so sending it directly sends invalid JSON","assert superjson round-trips the whole fixture back to its own types: Date, Map, Set, bigint and RegExp, with NaN, Infinity and the exact 2^53+1 bigint value intact","assert Number() on that bigint silently returns 2^53, which is why the throw exists and why the obvious workaround is wrong","assert a Map keeps non-string keys, while the hand-rolled Object.fromEntries fix stringifies a number key and collapses an object key onto \"[object Object]\"","assert both halves of the undefined asymmetry are repaired: the object key exists again and the array slot holds a real undefined rather than a hole","assert restoration rebuilds rather than aliases, so the returned Date is a new object with the same instant","assert nesting is not special-cased: a Date inside an array inside an object, and a Date held as a Map value, both come back","assert negative zero survives superjson but not JSON.stringify, by the same route as NaN and Infinity: tagged \"number\" and carried as the string \"-0\"","assert serialize returns exactly {json, meta}, with json being ordinary JSON that JSON.stringify accepts, and a rebuilt tree that does not alias the input","assert meta.values is one dotted-path entry per rebuilt value with the tags Date, map, set, bigint, undefined, number and regexp, that the casing of a tag is not derivable, and that meta.v is 1","assert URL is a second capitalised tag that superjson restores, and that plain JSON.stringify flattens a URL to its href because URL has a toJSON of its own, so toJSON is not Date-specific","assert a path segment containing a dot is escaped in the meta key and that such a key round-trips intact","assert meta is absent entirely rather than empty when nothing needed annotating, so reading result.meta.values on plain data throws","assert reaching one object by two paths brings meta back on its own with referentialEqualities and no values, keyed by identity rather than by value, and that the annotation re-links the two copies so x === y survives the round trip","assert deserialize takes the split back as an object, so the two pieces can travel separately","assert stringify is exactly JSON.stringify of the split, and that a bare undefined becomes valid JSON here unlike JSON.stringify","assert an unregistered class instance keeps its data, loses its prototype and methods, and is not annotated at all","assert a function is left in json by serialize as a live reference and is only dropped by the JSON.stringify inside stringify, so json is typed JSONValue but is not always a JSON value","assert an unregistered symbol value takes that same two-step path, and that plain JSON.stringify drops a symbol the same asymmetric way it drops undefined","assert an Invalid Date is NOT restored: it is unannotated, passed through into json, then turned into null by Date.prototype.toJSON at the stringify step","assert a TypeError comes back as a plain Error with the stack dropped, while JSON.stringify reports any Error as {} because message and stack are own but non-enumerable and name is not an own property","assert the subclass survives only as data: the tag is the capitalised \"Error\" for every Error and the name string is reassigned onto a base Error, so err.name still reads TypeError while instanceof TypeError is false","assert the dropped stack is opt-in rather than policy: allowErrorProps('stack') on a private instance carries it through stringify and parse, while the default instance measured above does not","assert registerClass restores instanceof and methods and annotates the value as [\"class\", name]","assert deserializing a class name this end never registered throws rather than degrading to a plain object","assert registerClass mutates the singleton shared by every importer of the default export, while a private SuperJSON instance keeps its own registry"],"goal":"See exactly what JSON.stringify destroys about a Date, Map, Set, BigInt, undefined, NaN, Infinity and RegExp, and which of them superjson puts back","kind":"HOW","packages":["pkg:npm/superjson@2.2.6"],"schemaVersion":1,"symbols":["superjson.stringify","superjson.parse","superjson.serialize","superjson.deserialize","superjson.registerClass","superjson.allowErrorProps","SuperJSONResult.meta","JSON.stringify"]},"contractCommand":["node","test/contract.mjs"],"environment":{"arch":"x64","ecosystem":"npm","executionContext":"node","language":"javascript","moduleSystem":"esm","os":"linux","packageManager":"npm","runtime":"node","runtimeVersion":"22","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/superjson@2.2.6"],"schemaVersion":1,"symbols":["superjson.stringify","superjson.parse","superjson.serialize","superjson.deserialize","superjson.registerClass","superjson.allowErrorProps","SuperJSONResult.meta","JSON.stringify"],"verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "csx-superjson-json-roundtrip",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "csx-superjson-json-roundtrip",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "superjson": "2.2.6"
      }
    },
    "node_modules/copy-anything": {
      "version": "4.0.5",
      "resolved": "https://registry.npmjs.org/copy-anything/-/copy-anything-4.0.5.tgz",
      "integrity": "sha512-7Vv6asjS4gMOuILabD3l739tsaxFQmC+a7pLZm02zyvs8p977bL3zEgq3yDk5rn9B0PbYgIv++jmHcuUab4RhA==",
      "license": "MIT",
      "dependencies": {
        "is-what": "^5.2.0"
      },
      "engines": {
        "node": ">=18"
      },
      "funding": {
        "url": "https://github.com/sponsors/mesqueeb"
      }
    },
    "node_modules/is-what": {
      "version": "5.5.0",
      "resolved": "https://registry.npmjs.org/is-what/-/is-what-5.5.0.tgz",
      "integrity": "sha512-oG7cgbmg5kLYae2N5IVd3jm2s+vldjxJzK1pcu9LfpGuQ93MQSzo0okvRna+7y5ifrD+20FE8FvjusyGaz14fw==",
      "license": "MIT",
      "engines": {
        "node": ">=18"
      },
      "funding": {
        "url": "https://github.com/sponsors/mesqueeb"
      }
    },
    "node_modules/superjson": {
      "version": "2.2.6",
      "resolved": "https://registry.npmjs.org/superjson/-/superjson-2.2.6.tgz",
      "integrity": "sha512-H+ue8Zo4vJmV2nRjpx86P35lzwDT3nItnIsocgumgr0hHMQ+ZGq5vrERg9kJBo5AWGmxZDhzDo+WVIJqkB0cGA==",
      "license": "MIT",
      "dependencies": {
        "copy-anything": "^4"
      },
      "engines": {
        "node": ">=16"
      }
    }
  }
}
package.json
{
  "name": "csx-superjson-json-roundtrip",
  "version": "1.0.0",
  "private": true,
  "type": "module",
  "license": "MIT-0",
  "dependencies": {
    "superjson": "2.2.6"
  }
}
src/roundtrip.mjs
import superjson, { SuperJSON } from "superjson";

/**
 * What JSON.stringify does to a value that is not already JSON, and what
 * superjson puts back.
 *
 * JSON.stringify has exactly one extension point, `toJSON`, and of the types
 * in the fixture below only Date implements it. That is not a rule about
 * built-ins — Node's URL has one too, measured in the contract — it is just
 * absent from Map, Set, RegExp and BigInt. Everything else falls into one of
 * four buckets, and only the last one tells you anything went wrong:
 *
 *   - enumerated as a plain object. Map, Set and RegExp keep their contents
 *     in internal slots rather than in enumerable own properties, so all
 *     three serialise to `{}`. The data is not truncated, it was never
 *     reached.
 *   - deleted. An `undefined`, a function or a symbol *value* makes
 *     JSON.stringify skip the key entirely, so the key stops existing.
 *   - coerced to null. The same three values in an *array* become null
 *     instead of vanishing, because an array cannot have a hole punched in it
 *     without changing every later index. NaN and Infinity become null
 *     everywhere, since JSON has no spelling for either.
 *   - thrown. A BigInt is the only one that raises, and it raises a TypeError
 *     from the stringifier itself, not from your code.
 *
 * The first three are the expensive ones. You get back a well-formed JSON
 * document, no error is reported anywhere, and the types are gone. The bug
 * surfaces later as `date.getTime is not a function`.
 *
 * superjson does not invent a wire format to fix this. It emits ordinary JSON
 * plus a sidecar of type annotations keyed by path — the `{ json, meta }`
 * split — so the payload stays readable by any JSON parser, and the receiving
 * end replays the annotations to rebuild the types.
 *
 * That split is two steps, and the contract measures the seam between them
 * because it is not where you would guess. `serialize` only rewrites the
 * values it recognises; anything it has no annotation for — a function, a
 * symbol, an Invalid Date — is copied into `json` untouched, still a live
 * reference. The dropping happens in the plain JSON.stringify that `stringify`
 * runs over that result. So `json` is typed JSONValue but is not always a JSON
 * value, and the losses above are still JSON.stringify's, one layer down.
 */

/**
 * One fixture holding every value the two serialisers disagree about. It is a
 * factory rather than a constant because half of these are mutable and the
 * contract compares object identity in places.
 *
 * `big` is 2^53 + 1, the smallest integer a double cannot represent. Reaching
 * for Number() to dodge the BigInt throw silently rounds it down to 2^53,
 * which is the reason the throw is there.
 *
 * `holes` carries the second half of the undefined story: the same value that
 * gets deleted from an object survives as null inside an array.
 */
export function makeFixture() {
  return {
    when: new Date("2024-03-01T12:00:00.000Z"),
    tags: new Map([
      ["release", 3],
      [7, "seven"],
    ]),
    seen: new Set(["a", "b"]),
    big: 9007199254740993n,
    missing: undefined,
    ratio: NaN,
    ceiling: Infinity,
    pattern: /ab+c/gi,
    holes: [1, undefined, 3],
  };
}

export function throughJson(value) {
  return JSON.parse(JSON.stringify(value));
}

export function throughSuperjson(value) {
  return superjson.parse(superjson.stringify(value));
}

/**
 * A class instance is the case superjson cannot solve by inspection. Nothing
 * in the serialised form records which constructor a plain-looking object came
 * from, so unless the class is registered by name on both ends the instance
 * comes back as a plain object: the data survives, the prototype does not, and
 * `format()` is gone.
 */
export class Money {
  constructor(cents) {
    this.cents = cents;
  }

  format() {
    return `$${(this.cents / 100).toFixed(2)}`;
  }
}

/**
 * `registerClass` on the default export mutates a module-level singleton that
 * every importer of superjson shares. That is what makes registration work
 * across files, and also what makes the order of these checks load-bearing:
 * once the contract registers Money, no later assertion can observe the
 * unregistered behaviour again. A private instance is the way to keep a
 * registry to yourself.
 */
export function privateInstance() {
  return new SuperJSON();
}
test/contract.mjs
import assert from "node:assert/strict";
import superjson from "superjson";

import {
  makeFixture,
  Money,
  privateInstance,
  throughJson,
  throughSuperjson,
} from "../src/roundtrip.mjs";

// ---------------------------------------------------------------------------
// What plain JSON.stringify destroys
// ---------------------------------------------------------------------------

const fixture = makeFixture();

// The BigInt is the only member that announces itself, and it does so by
// taking the whole document down: one 64-bit id anywhere in a response body
// and JSON.stringify throws instead of returning a partial string. The message
// comes from the stringifier, so it names no key and gives no path.
assert.throws(
  () => JSON.stringify(fixture),
  (err) => {
    assert.ok(err instanceof TypeError);
    assert.equal(err.message, "Do not know how to serialize a BigInt");
    return true;
  },
);

// Removing it is the only way to ask what JSON.stringify does to the rest, and
// the answer is that it succeeds. Every remaining loss below is silent.
const { big, ...jsonSafe } = fixture;
const flat = throughJson(jsonSafe);

// Date is the one type in this fixture with a toJSON, so it survives as data
// and dies as a type: what comes back is a string that will not answer
// getTime().
assert.equal(flat.when, "2024-03-01T12:00:00.000Z");
assert.equal(typeof flat.when, "string");
assert.equal(flat.when instanceof Date, false);

// Map, Set and RegExp keep everything in internal slots, and JSON.stringify
// only walks enumerable own properties. All three come back as an empty
// object — not a truncated one, an unvisited one.
assert.deepEqual(flat.tags, {});
assert.deepEqual(flat.seen, {});
assert.deepEqual(flat.pattern, {});

// NaN and Infinity have no JSON spelling, so both become null. A number field
// that was "not measured yet" and one that was "over the limit" arrive
// indistinguishable from each other and from a real null.
assert.equal(flat.ratio, null);
assert.equal(flat.ceiling, null);

// The asymmetry, which is the part that makes a partial fix look correct: the
// same undefined is DELETED from an object and COERCED TO NULL in an array.
// Code that recovers records by filling in missing keys is wrong for lists,
// and code that filters nulls out of lists is wrong for records.
assert.equal("missing" in flat, false);
assert.equal(Object.keys(flat).includes("missing"), false);
assert.deepEqual(flat.holes, [1, null, 3]);
assert.equal(flat.holes.length, 3);

// Stated once more as the single line worth remembering.
assert.equal(JSON.stringify({ v: undefined }), "{}");
assert.equal(JSON.stringify([undefined]), "[null]");

// A top-level undefined does not even produce a string. JSON.stringify returns
// the value undefined, so `res.send(JSON.stringify(x))` sends the four
// characters "undefined" rather than valid JSON.
assert.equal(JSON.stringify(undefined), undefined);

// ---------------------------------------------------------------------------
// What superjson restores
// ---------------------------------------------------------------------------

const back = throughSuperjson(makeFixture());

// Every type in the fixture comes back as its own type, including the BigInt
// that stopped JSON.stringify outright.
assert.ok(back.when instanceof Date);
assert.equal(back.when.getTime(), Date.UTC(2024, 2, 1, 12, 0, 0));
assert.ok(back.tags instanceof Map);
assert.ok(back.seen instanceof Set);
assert.equal(typeof back.big, "bigint");
assert.ok(back.pattern instanceof RegExp);

// Values, not just types. The BigInt is the reason to care: 2^53 + 1 is not
// representable as a double, so the "just use Number()" workaround for the
// throw above returns 2^53 and loses the last digit without complaining.
assert.equal(back.big, 9007199254740993n);
assert.equal(Number(9007199254740993n), 9007199254740992);

assert.ok(Number.isNaN(back.ratio));
assert.equal(back.ceiling, Infinity);
assert.equal(back.pattern.source, "ab+c");
assert.equal(back.pattern.flags, "gi");

// A Map keeps non-string keys. This is what separates superjson from the
// hand-rolled fix: Object.fromEntries stringifies every key, so a number key
// becomes "7" and an object key becomes the literal string "[object Object]",
// collapsing distinct entries onto one slot.
assert.deepEqual([...back.tags.entries()], [["release", 3], [7, "seven"]]);
assert.equal(back.tags.get(7), "seven");
assert.deepEqual(Object.fromEntries(new Map([[7, "seven"], [{}, "obj"]])), {
  7: "seven",
  "[object Object]": "obj",
});

assert.deepEqual([...back.seen], ["a", "b"]);

// Both halves of the undefined asymmetry are repaired: the key exists again
// and holds undefined, and the array slot is a real undefined rather than a
// null — it is present, so it is a set element and not a sparse hole.
assert.equal("missing" in back, true);
assert.equal(back.missing, undefined);
assert.deepEqual(back.holes, [1, undefined, 3]);
assert.equal(1 in back.holes, true);

// Restoration rebuilds, it does not alias. The Date is a new object with the
// same instant, so mutating what came off the wire cannot reach the original.
const source = makeFixture();
const restored = throughSuperjson(source);
assert.equal(restored.when === source.when, false);
assert.equal(restored.when.getTime(), source.when.getTime());

// Nesting is not special-cased: types inside arrays, and inside a Map's own
// values, are annotated by path and come back too.
const nested = throughSuperjson({ a: { b: [new Date(7)] } });
assert.ok(nested.a.b[0] instanceof Date);
assert.equal(throughSuperjson(new Map([["k", new Date(7)]])).get("k").getTime(), 7);

// Negative zero survives, which JSON cannot express at all. Same mechanism as
// NaN and Infinity, and the same tag: all three ride as strings annotated
// "number", so -0 crosses as the two characters "-0".
assert.equal(JSON.stringify({ z: -0 }), '{"z":0}');
assert.ok(Object.is(throughSuperjson({ z: -0 }).z, -0));
assert.deepEqual(superjson.serialize({ z: -0 }), {
  json: { z: "-0" },
  meta: { values: { z: ["number"] }, v: 1 },
});

// ---------------------------------------------------------------------------
// The { json, meta } split, which is how it does it
// ---------------------------------------------------------------------------

const result = superjson.serialize(makeFixture());

// `json` is ordinary JSON with the awkward values rewritten into strings, so
// it survives JSON.stringify without throwing. That is the design: the wire
// format is still JSON and any consumer can read it, typed or not.
assert.deepEqual(Object.keys(result).sort(), ["json", "meta"]);
assert.deepEqual(result.json, {
  when: "2024-03-01T12:00:00.000Z",
  tags: [["release", 3], [7, "seven"]],
  seen: ["a", "b"],
  big: "9007199254740993",
  missing: null,
  ratio: "NaN",
  ceiling: "Infinity",
  pattern: "/ab+c/gi",
  holes: [1, null, 3],
});
assert.doesNotThrow(() => JSON.stringify(result.json));

// `json` is a rebuilt tree rather than a view of the input, so writing to it
// cannot reach back into the object you serialised.
const shared = { id: 1, nested: { deep: 2 } };
assert.equal(superjson.serialize(shared).json.nested === shared.nested, false);

// `meta.values` is the sidecar: one entry per value that needed rebuilding,
// keyed by dotted path, valued as a tag array. A tag is whatever string the
// transformer was declared with, so the casing is not derivable and not worth
// guessing at: Date is capitalised where map, set, bigint, undefined, number
// and regexp are not, and Date is not the exception — URL just below and Error
// further down are capitalised too.
assert.deepEqual(result.meta.values, {
  when: ["Date"],
  tags: ["map"],
  seen: ["set"],
  big: ["bigint"],
  missing: ["undefined"],
  ratio: ["number"],
  ceiling: ["number"],
  pattern: ["regexp"],
  "holes.1": ["undefined"],
});
assert.equal(result.meta.v, 1);

// URL carries the second capitalised tag, and is the other type here with a
// toJSON of its own — so plain JSON.stringify degrades it the way it degrades
// a Date, to a string you could rebuild by hand, rather than to {} or null.
// Having a toJSON is not a Date-specific property.
const url = new URL("https://example.com/a?b=1");
assert.deepEqual(superjson.serialize({ u: url }).meta.values, { u: ["URL"] });
assert.ok(throughSuperjson({ u: url }).u instanceof URL);
assert.equal(JSON.stringify({ u: url }), '{"u":"https://example.com/a?b=1"}');

// A path segment containing a dot is escaped, so the dotted keys are not
// ambiguous and such a key round-trips intact.
assert.deepEqual(superjson.serialize({ "a.b": new Date(0) }).meta.values, {
  "a\\.b": ["Date"],
});
assert.deepEqual(Object.keys(throughSuperjson({ "a.b": new Date(0) })), ["a.b"]);

// When nothing needed annotating there is no `meta` key at all — not an empty
// object, absent. Reading `result.meta.values` on already-plain data throws.
const plain = superjson.serialize({ a: 1 });
assert.deepEqual(plain, { json: { a: 1 } });
assert.equal("meta" in plain, false);
assert.throws(() => plain.meta.values, TypeError);

// "Nothing needed annotating" is not the same condition as "the data was
// already JSON", which is the trap in reading the check above as a type test.
// Reaching one object by two paths brings `meta` back by itself, holding
// `referentialEqualities` and no `values` at all — so here `meta` exists,
// `meta.values` is undefined rather than absent, and code that branches on
// `meta` being present has misread a plain payload as a typed one. This is not
// the dedupe option, which is off: `json` still carries both copies. The
// annotation is what re-links them, so identity survives the wire.
const twice = { a: 1 };
const twiceSplit = superjson.serialize({ x: twice, y: twice });
assert.deepEqual(twiceSplit.json, { x: { a: 1 }, y: { a: 1 } });
assert.deepEqual(twiceSplit.meta, { referentialEqualities: { x: ["y"] }, v: 1 });
assert.equal(twiceSplit.meta.values, undefined);
const relinked = throughSuperjson({ x: twice, y: twice });
assert.equal(relinked.x === relinked.y, true);

// By identity, not by value: two objects that merely look alike are not linked
// and get no annotation.
assert.equal("meta" in superjson.serialize({ x: { a: 1 }, y: { a: 1 } }), false);

// deserialize is the other half, and takes the split back as an object, so the
// two pieces can travel separately.
const viaSplit = superjson.deserialize(superjson.serialize({ d: new Date(5) }));
assert.ok(viaSplit.d instanceof Date);
assert.equal(viaSplit.d.getTime(), 5);

// stringify is exactly JSON.stringify of that split, which is why a superjson
// payload is inspectable with any JSON tool.
assert.equal(superjson.stringify({ d: new Date(5) }), JSON.stringify(superjson.serialize({ d: new Date(5) })));

// A bare undefined does become valid JSON here, unlike JSON.stringify above.
assert.equal(superjson.stringify(undefined), '{"json":null,"meta":{"values":["undefined"],"v":1}}');
assert.equal(superjson.parse(superjson.stringify(undefined)), undefined);

// ---------------------------------------------------------------------------
// What superjson does NOT restore
//
// registerClass below mutates the singleton shared by every importer, so the
// unregistered checks have to come first — they cannot be observed again once
// the registration has happened.
// ---------------------------------------------------------------------------

const money = new Money(1250);
const looseMoney = throughSuperjson({ money }).money;

// An unregistered class instance keeps its data and loses everything that made
// it that class. It is not annotated at all: superjson has no way to tell this
// object apart from an object literal, so there is no meta entry to miss.
assert.equal(looseMoney instanceof Money, false);
assert.equal(Object.getPrototypeOf(looseMoney), Object.prototype);
assert.deepEqual(looseMoney, { cents: 1250 });
assert.equal(typeof looseMoney.format, "undefined");
assert.equal("meta" in superjson.serialize({ money }), false);

// A function is dropped by both, and superjson annotates nothing for it — but
// it is not serialize() that drops it. Measured, against the obvious guess:
// serialize leaves the function sitting in `json` by reference, and the
// JSON.stringify inside superjson.stringify is what removes it. `json` is
// typed JSONValue and is not always a JSON value, so a pipeline that takes the
// split and sends it through anything other than JSON.stringify still carries
// the function.
const fn = () => 42;
const withFn = superjson.serialize({ id: 1, run: fn });
assert.equal(withFn.json.run, fn);
assert.deepEqual(Object.keys(withFn.json), ["id", "run"]);
assert.equal("meta" in withFn, false);

// After stringify it is gone, and gone the same asymmetric way an undefined
// is: missing from an object, null in an array.
assert.equal(superjson.stringify({ id: 1, run: fn }), '{"json":{"id":1}}');
assert.equal(superjson.stringify([fn]), '{"json":[null]}');
assert.deepEqual(throughSuperjson({ id: 1, run: fn }), { id: 1 });
assert.deepEqual(JSON.parse(JSON.stringify({ id: 1, run: fn })), { id: 1 });

// An unregistered symbol value takes the same two-step path, and the step that
// drops it is visibly the plain one: JSON.stringify treats a symbol exactly the
// way it treats undefined and a function, deleted from an object and null in an
// array.
const sym = Symbol("tag");
assert.equal(superjson.serialize({ id: 1, s: sym }).json.s, sym);
assert.deepEqual(Object.keys(throughSuperjson({ id: 1, s: sym })), ["id"]);
assert.equal(JSON.stringify({ id: 1, s: sym }), '{"id":1}');
assert.equal(JSON.stringify([sym]), "[null]");

// Measured, and the opposite of what the fixture above suggests: an Invalid
// Date is NOT restored, and it is lost by the same two-step route as the
// function. superjson's Date test rejects a NaN timestamp, so the value is
// never annotated; serialize hands the Date object straight through into
// `json`, and then the JSON.stringify inside stringify calls
// Date.prototype.toJSON, which answers null for an invalid date. What arrives
// is null, not a Date. A date built from unvalidated user input lands here.
const invalid = new Date("not a date");
assert.ok(invalid instanceof Date);
assert.ok(Number.isNaN(invalid.getTime()));

const invalidSplit = superjson.serialize({ d: invalid });
assert.equal("meta" in invalidSplit, false);
assert.ok(invalidSplit.json.d instanceof Date);
assert.equal(superjson.stringify({ d: invalid }), '{"json":{"d":null}}');
assert.equal(throughSuperjson({ d: invalid }).d, null);

// An Error survives as an Error but not as its subclass: a TypeError comes
// back as a plain Error carrying the name as data, and without its stack.
// JSON.stringify, for contrast, reports an Error as {}, because message and
// stack are own properties but not enumerable ones and name is not an own
// property at all — so the walk finds nothing to write.
assert.deepEqual(JSON.parse(JSON.stringify({ e: new TypeError("boom") })), { e: {} });
assert.deepEqual(Object.keys(new TypeError("boom")), []);
assert.equal(Object.hasOwn(new TypeError("boom"), "name"), false);
const errBack = throughSuperjson({ e: new TypeError("boom") }).e;
assert.ok(errBack instanceof Error);
assert.equal(errBack instanceof TypeError, false);
assert.equal(errBack.constructor.name, "Error");
assert.equal(errBack.message, "boom");
assert.equal(errBack.stack, undefined);

// "Carrying the name as data" is literal: the tag is "Error" for every Error,
// and the subclass survives only as a `name` string that was copied out and
// assigned back onto a base Error. So `err.name` still reads "TypeError" while
// every instanceof narrowing against a subclass is now false — the pair that
// makes a catch block look right and behave wrong.
assert.equal(errBack.name, "TypeError");
assert.deepEqual(superjson.serialize({ e: new TypeError("boom") }), {
  json: { e: { name: "TypeError", message: "boom" } },
  meta: { values: { e: ["Error"] }, v: 1 },
});

// The missing stack is opt-in rather than policy, and the opt-in is per
// instance: allowErrorProps copies the named property into json and assigns it
// back on the way out. Done on a private instance, because the default export
// is the singleton the assertion above just measured without it.
const keepsStack = privateInstance();
keepsStack.allowErrorProps("stack");
const withStack = keepsStack.parse(keepsStack.stringify({ e: new TypeError("boom") })).e;
assert.equal(typeof withStack.stack, "string");
assert.match(withStack.stack, /^TypeError: boom/);

// Registering by name is the fix, and the name is what crosses the wire, not
// the class. This is the mutation the section header warns about.
superjson.registerClass(Money, "Money");
const typedMoney = throughSuperjson({ money }).money;
assert.ok(typedMoney instanceof Money);
assert.equal(typedMoney.format(), "$12.50");
assert.deepEqual(superjson.serialize({ money }).meta.values, {
  money: [["class", "Money"]],
});

// Both ends have to agree on that name. A payload naming a class this end has
// never registered throws rather than degrading to a plain object, so a
// deploy that ships the producer before the consumer fails loudly.
assert.throws(
  () =>
    superjson.deserialize({
      json: { money: { cents: 1 } },
      meta: { values: { money: [["class", "Coupon"]] }, v: 1 },
    }),
  (err) => {
    assert.ok(err instanceof Error);
    assert.match(err.message, /unknown class 'Coupon'/);
    return true;
  },
);

// The registry lives on the default export's singleton, so registering Money
// above changed it for every module in the process. An instance of your own
// keeps its own registry and never saw the registration.
assert.equal("meta" in privateInstance().serialize({ money }), false);

console.log("contract ok");

Origin Seeder

anonymous