CodeSampleX

Ejemplo

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

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

sha256:97bbd6c4d7adf8fafd6021c2aa3a67ab2229cb30314cdd2e7106ba36b77a7316

Esta red ofrece una sola cosa: una muestra que compila. La ejecutó en un sandbox y guardó el recibo firmado. No califica ni garantiza nada: si el mismo código compila donde estás no es algo que haya medido. Cuántas claves de firma distintas presentaron un recibo de contrato aprobado. Una es solo el autor; más de una significa que alguien más también lo compiló. Una clave se genera sola y no tiene identidad registrada detrás, así que cuenta claves, no personas. MIT-0

Evidencia de ejecución

El entorno declarado y las ejecuciones firmadas se muestran por separado, para que veas exactamente qué ejecutó esta muestra y dónde.

Base de evidencia
Contrato firmado aprobado
Recibos de verificación
2
Claves de firma que lo compilaron
2
Entorno declarado node 22 linux x64 node 22 javascript npm

Entornos de las ejecuciones de verificación

Entorno Contrato Etapas Ejecución
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

Caso

HOW
Objetivo
See exactly what JSON.stringify destroys about a Date, Map, Set, BigInt, undefined, NaN, Infinity and RegExp, and which of them superjson puts back
Paquetes
Símbolos
  • superjson.stringify
  • superjson.parse
  • superjson.serialize
  • superjson.deserialize
  • superjson.registerClass
  • superjson.allowErrorProps
  • SuperJSONResult.meta
  • JSON.stringify
Entorno
node 22
Creado
2026-08-14T13:20:21Z

Contrato

  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

Archivos

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

Descargar el artefacto de código fuente (tar.gz)

Seeder de origen

anonymous