All serialization methods can accept a { disabledFeatures: number } option. This option influences how the serialization will process and emit a value.
import { serialize, Feature } from 'seroval';
const y = Object.create(null);
y.self = y;
y.example = 'Hello World';
function serializeWithTarget(value, disabledFeatures) {
const result = serialize(value, {
disabledFeatures,
});
console.log(result);
}
serializeWithTarget(y, Feature.ObjectAssign);
serializeWithTarget(y, 0);(function(h){return (h=(h=Object.create(null),h.example="Hello World",h),h.self=h,h)})()
(h=>(h=Object.assign(Object.create(null),{example:"Hello World"}),h.self=h,h))()disabledFeatures uses bit flags for faster checking, so if you need to disable multiple features, you can use the bitwise OR symbol (|).
Here's an ES2017 flag:
import { serialize, Feature } from 'seroval';
const ES2017FLAG =
Feature.AggregateError // ES2021
| Feature.BigIntTypedArray // ES2020;
serialize(myValue, {
disabledFeatures: ES2017FLAG,
})By default, all feature flags are enabled. The following are the feature flags and their behavior when disabled:
AggregateError- Compiles down to
Errorinstead.
- Compiles down to
ErrorPrototypeStack- Skipped when detected.
- Affects both
ErrorandAggregateError
ObjectAssign- Uses manual object assignments instead.
- Affects
Iterable,Error,AggregateErrorandObject.create(null)
BigIntTypedArray- Disables serialization of
BigInt64ArrayandBigUint64Array
- Disables serialization of
RegExp- Disables serialization (and deserialization) of
RegExp
- Disables serialization (and deserialization) of
- sync =
serialize,toJSON,crossSerialize,toCrossJSON - async =
serializeAsync,toJSONAsync,crossSerializeAsync,toCrossJSONAsync - streaming =
crossSerializeStream,toCrossJSONStream,Serializer
| Type | sync | async | streaming |
|---|---|---|---|
NaN |
✅ | ✅ | ✅ |
Infinity |
✅ | ✅ | ✅ |
-Infinity |
✅ | ✅ | ✅ |
-0 |
✅ | ✅ | ✅ |
number |
✅ | ✅ | ✅ |
string |
✅ | ✅ | ✅ |
boolean |
✅ | ✅ | ✅ |
null |
✅ | ✅ | ✅ |
undefined |
✅ | ✅ | ✅ |
bigint |
✅ | ✅ | ✅ |
Array |
✅ | ✅ | ✅ |
sparse (holey) Arrays |
✅ | ✅ | ✅ |
Object |
✅ | ✅ | ✅ |
RegExp |
❓1 | ❓1 | ❓1 |
Date |
✅ | ✅ | ✅ |
Map |
✅ | ✅ | ✅ |
Set |
✅ | ✅ | ✅ |
Object.create(null) |
✅ | ✅ | ✅ |
ArrayBuffer |
✅ | ✅ | ✅ |
DataView |
✅ | ✅ | ✅ |
Int8Array |
✅ | ✅ | ✅ |
Int16Array |
✅ | ✅ | ✅ |
Int32Array |
✅ | ✅ | ✅ |
Uint8Array |
✅ | ✅ | ✅ |
Uint16Array |
✅ | ✅ | ✅ |
Uint32Array |
✅ | ✅ | ✅ |
Uint8ClampedArray |
✅ | ✅ | ✅ |
Float32Array |
✅ | ✅ | ✅ |
Float64Array |
✅ | ✅ | ✅ |
BigInt64Array |
❓2 | ❓2 | ❓2 |
BigUint64Array |
❓2 | ❓2 | ❓2 |
Error |
✅3 | ✅3 | ✅3 |
AggregateError |
✅34 | ✅34 | ✅34 |
EvalError |
✅3 | ✅3 | ✅3 |
RangeError |
✅3 | ✅3 | ✅3 |
ReferenceError |
✅3 | ✅3 | ✅3 |
SyntaxError |
✅3 | ✅3 | ✅3 |
TypeError |
✅3 | ✅3 | ✅3 |
URIError |
✅3 | ✅3 | ✅3 |
Promise |
❌ | ✅ | ✅ |
Iterable |
✅ | ✅ | ✅ |
| Well-known symbols | ✅ | ✅ | ✅ |
AsyncIterable |
❌ | ✅ | ✅ |
| Built-in streaming primitive | ✅ | ✅ | ✅ |
| Cyclic references | ✅ | ✅ | ✅ |
| Isomorphic references | ✅ | ✅ | ✅ |
| Type | sync | async | streaming |
|---|---|---|---|
URL |
✅ | ✅ | ✅ |
URLSearchParams |
✅ | ✅ | ✅ |
Blob |
❌ | ✅ | ❌5 |
File |
❌ | ✅ | ❌5 |
Headers |
✅ | ✅ | ✅ |
FormData |
✅6 | ✅ | ✅6 |
ReadableStream |
❌ | ✅ | ✅ |
Request |
❌ | ✅ | ✅ |
Response |
❌ | ✅ | ✅ |
Event |
✅ | ✅ | ✅ |
CustomEvent |
✅ | ✅ | ✅ |
DOMException |
✅ | ✅ | ✅ |
ImageData |
✅ | ✅ | ✅ |
AbortSignal |
✅ | ✅ | ✅ |
Footnotes
-
Feature.RegExpmust be enabled, otherwise throws anSerovalUnsupportedTypeErrorandSerovalUnsupportedNodeError. ↩ ↩2 ↩3 -
Feature.BigIntTypedArraymust be enabled, otherwise throws anSerovalUnsupportedTypeError. ↩ ↩2 ↩3 ↩4 ↩5 ↩6 -
Feature.ErrorPrototypeStackmust be enabled if serializingError.prototype.stackis desired. ↩ ↩2 ↩3 ↩4 ↩5 ↩6 ↩7 ↩8 ↩9 ↩10 ↩11 ↩12 ↩13 ↩14 ↩15 ↩16 ↩17 ↩18 ↩19 ↩20 ↩21 ↩22 ↩23 ↩24 -
Feature.AggregateErrormust be enabled, otherwiseAggregateErroris serialized into anErrorinstance. ↩ ↩2 ↩3 -
Due to the nature of
BlobandFilebeing an async type (in that it returns aPromise-based serializable data) while having a sync constructor, it cannot be represented in a way that the type is consistent to its original declaration. ↩ ↩2 -
FormDatais partially supported if it doesn't contain anyBloborFileinstances. ↩ ↩2