CodeSampleX

示例

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

已验证示例 — npm 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…

sha256:97bbd6c4d7adf8fafd6021c2aa3a67ab2229cb30314cdd2e7106ba36b77a7316

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。 MIT-0

执行证据

声明的环境与签名的运行分开呈现,你可以看到这个样本究竟运行了什么、在哪里运行。

证据依据
签名契约通过
验证回执
2
构建过它的签名密钥
2
声明的环境 node 22 linux x64 node 22 javascript npm

验证运行环境

环境 契约 阶段 运行日期
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

案例

HOW
目标
See exactly what JSON.stringify destroys about a Date, Map, Set, BigInt, undefined, NaN, Infinity and RegExp, and which of them superjson puts back
符号
  • superjson.stringify
  • superjson.parse
  • superjson.serialize
  • superjson.deserialize
  • superjson.registerClass
  • superjson.allowErrorProps
  • SuperJSONResult.meta
  • JSON.stringify
环境
node 22
创建时间
2026-08-14T13:20:21Z

契约

  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

文件

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

下载源代码构件 (tar.gz)

原始种子者

anonymous