An env agnostic serializer and deserializer with recursion ability and types beyond JSON from the HTML standard itself.
- Supported Types
- not supported yet: Blob, File, FileList, ImageBitmap, ImageData, and ArrayBuffer, but typed arrays are supported without major issues, but u/int8, u/int16, and u/int32 are the only safely suppored (right now).
- not possible to implement: the
{transfer: []}
option can be passed but it's completely ignored.
- MDN Documentation
- Serializer
- Deserializer
Serialized values can be safely stringified as JSON too, and deserialization resurrect all values, even recursive, or more complex than what JSON allows.
Check the 100% test coverage to know even more.
// as default export
import structuredClone from '@ungap/structured-clone';
const cloned = structuredClone({any: 'serializable'});
// as independent serializer/deserializer
import {serialize, deserialize} from '@ungap/structured-clone';
// the result can be stringified as JSON without issues
// even if there is recursive data, bigint values,
// typed arrays, and so on
const serialized = serialize({any: 'serializable'});
// the result will be a replica of the original object
const deserialized = deserialize(serialized);