Supported types
The modes are:
- Sync:
serialize,toJSON,crossSerializeandtoCrossJSON. - Async:
serializeAsync,toJSONAsync,crossSerializeAsyncandtoCrossJSONAsync. - Streaming:
crossSerializeStream,toCrossJSONStreamandSerializer.
| Type | Sync | Async | Streaming |
|---|---|---|---|
string, boolean, null, undefined | ✅ | ✅ | ✅ |
number, including NaN, Infinity, -Infinity and -0 | ✅ | ✅ | ✅ |
bigint | ✅ | ✅ | ✅ |
Array, including sparse arrays | ✅ | ✅ | ✅ |
Object and Object.create(null) | ✅ | ✅ | ✅ |
| Frozen, sealed and non-extensible objects | ✅ | ✅ | ✅ |
Date | ✅ | ✅ | ✅ |
RegExp | ✅ 1 | ✅ 1 | ✅ 1 |
Map and Set | ✅ | ✅ | ✅ |
ArrayBuffer and DataView | ✅ | ✅ | ✅ |
Int8Array, Uint8Array, Uint8ClampedArray, Int16Array, Uint16Array, Int32Array, Uint32Array, Float32Array, Float64Array | ✅ | ✅ | ✅ |
BigInt64Array and BigUint64Array | ✅ 2 | ✅ 2 | ✅ 2 |
Error, EvalError, RangeError, ReferenceError, SyntaxError, TypeError and URIError | ✅ 3 | ✅ 3 | ✅ 3 |
AggregateError | ✅ 4 | ✅ 4 | ✅ 4 |
Temporal types | ✅ 5 | ✅ 5 | ✅ 5 |
Promise | ❌ 6 | ✅ | ✅ |
Iterables (Symbol.iterator) | ✅ | ✅ | ✅ |
Async iterables (Symbol.asyncIterator) | ❌ 7 | ✅ | ✅ |
createStream and createLiveStream | ❌ 8 | ✅ | ✅ |
| Well-known symbols | ✅ | ✅ | ✅ |
| Shared and cyclic references | ✅ | ✅ | ✅ |
| Isomorphic references | ✅ | ✅ | ✅ |
Web types such as URL, Request and FormData are supported through plugins.
Feature flags
Every function takes a disabledFeatures option. It turns features off, so the output runs in older runtimes, or so a receiver rejects types it does not want.
disabledFeatures is a bit mask. Combine flags with |:
import { Feature, serialize } from 'seroval';
serialize(value, {
disabledFeatures: Feature.AggregateError | Feature.BigIntTypedArray,
});| Flag | When disabled |
|---|---|
Feature.AggregateError | AggregateError is sent as an Error. |
Feature.ErrorPrototypeStack | The stack property of errors is not sent. |
Feature.ObjectAssign | Output assigns properties one by one instead of calling Object.assign. |
Feature.BigIntTypedArray | BigInt64Array and BigUint64Array throw. |
Feature.RegExp | RegExp throws when serialized, and is rejected when decoded. |
Feature.Temporal | Temporal values throw when serialized, and are rejected when decoded. |
Feature.ArrowFunction | Deprecated. It has no effect. |
All features are on by default. For example, with ObjectAssign disabled:
const value = Object.assign(Object.create(null), { x: 1 });
serialize(value);
// Object.assign(Object.create(null),{x:1})
serialize(value, { disabledFeatures: Feature.ObjectAssign });
// (h=>h=(h=Object.create(null),h.x=1,h))()Runtimes
seroval runs anywhere modern JavaScript runs: Node.js 20 or later, Deno, Bun, browsers and edge runtimes. It has no dependencies.
Package resolution
The seroval package has ESM and CommonJS builds, and a development build with full error messages. Bundlers pick the right one through the package's export conditions:
- The
developmentcondition selects the build with full error messages. - The default selects the production build, which reports errors by code. See Errors.
- The
browsercondition selects the same build as the default, so there is never a second copy of seroval's classes or reference registry.
Resolvers that ignore export conditions, such as older bundlers and some React Native setups, can use the main and module fields. Binary encoding uses Buffer when it exists, and portable browser APIs otherwise. Both produce the same output.
Footnotes
-
Requires
Feature.RegExp, which is on by default. When it is off, aRegExpthrowsSerovalUnsupportedTypeError. ↩ ↩2 ↩3 -
Requires
Feature.BigIntTypedArray, which is on by default. When it is off, these arrays throwSerovalUnsupportedTypeError. ↩ ↩2 ↩3 -
The
stackproperty is sent only whileFeature.ErrorPrototypeStackis on. ↩ ↩2 ↩3 -
Sent as an
ErrorwhenFeature.AggregateErroris off. ↩ ↩2 ↩3 -
Requires
Feature.Temporal, and a runtime that has theTemporalglobal on both sides. ↩ ↩2 ↩3 -
Sync functions cannot wait for promises, so a promise throws a
SerovalParserErrorwhose cause is aSerovalUnsupportedTypeError. ↩ -
Sync functions cannot wait for values, so an async iterable throws a
SerovalParserErrorwhose cause is aSerovalUnsupportedTypeError. ↩ -
Sync functions cannot wait for values, so these are sent as an empty stream that never ends. Reading it waits forever. ↩