Serialize what JSON can't.

seroval turns JavaScript values into a string and back, including cyclic references, Date, Map, Set, BigInt and Promise, as code or as JSON.

npm install seroval
pnpm add seroval
yarn add seroval
bun add seroval
value
const user = {
  name: "Ada",
  born: new Date("1815-12-10"),
  notes: new Map([["G", "Bernoulli"]]),
};
user.self = user;
serialize(user)
string111 B
(h=>(h={name:"Ada",born:new Date("1815-12-10T00:00:00.000Z"),notes:new Map([["G","Bernoulli"]])},h.self=h,h))()
deserialize(string)
  • back.born instanceof Date
  • back.notes.get("G") === "Bernoulli"
  • back.self === back

JSON.stringify drops data. serialize keeps it.

Each row runs in your browser: JSON.stringify in the middle column, serialize in the right one.

ValueJSON.stringifyserialize
new Date(0)"1970-01-01T00:00:00.000Z"becomes a stringnew Date("1970-01-01T00:00:00.000Z")
new Map([["a", 1]]){}entries droppednew Map([["a",1]])
new Set([1, 2]){}items droppednew Set([1,2])
{ x: undefined }{}key dropped({x:void 0})
[NaN, -0, Infinity][null,0,null]numbers changed[0/0,-0,1/0]
/ab+c/gi{}pattern lost/ab+c/gi
9007199254740993nTypeErrorDo not know how to serialize a BigInt9007199254740993n
new Error("boom"){}message lostnew Error("boom")
[, , 1][null,null,1]holes filled[,,1]
x.self = xTypeErrorConverting circular structure to JSON(h=>(h={name:"x"},h.self=h,h))()

Stream values as they resolve.

The synchronous part ships first and each promise follows when it settles, shown with real crossSerializeStream chunks.

Sserver
Cbrowser
user
posts · 1.2 s
recommended · 2.4 s

Four output formats, same references.

Pick the format that fits the wire. Each one keeps shared and cyclic references.

One expression. Smallest output, needs eval.

Size
Needs eval
Round trip

More types, built in or by plugin.

Core handles the JavaScript built-ins, and seroval-plugins adds the Web API types.

Built in

DateMapSetBigIntRegExpErrorAggregateErrorPromiseArrayBufferUint8ArrayDataViewAsyncIterableTemporalNaN-0Infinityundefined

With seroval-plugins

URLURLSearchParamsHeadersFormDataBlobFileRequestResponseReadableStreamEventDOMException

Used by

  • SolidJS
  • TanStack Start

Questions

Is it safe to decode data from a client?

Yes, with the JSON functions. fromJSON validates the tree and runs no code from it. Never pass untrusted input to deserialize, which runs it. Security

Can the sender and receiver run different seroval versions?

Use the same version on both sides. The payload format can change in a minor release. SRV002

Where does it run?

Node.js 20 or later, Deno, Bun, browsers and edge runtimes. It has no dependencies. Compatibility

Can it send my own classes, or URL and Request?

Write a plugin for your classes. seroval-plugins covers URL, Headers, FormData, Request, Response, Blob, ReadableStream and more. Using plugins

Can it send functions?

Not their code. Register a function on both sides with createReference, and seroval sends its key. Isomorphic references

What does it cost?

Nothing. seroval is MIT licensed.

Send more than JSON.

npm install seroval
pnpm add seroval
yarn add seroval
bun add seroval