@exodus/serialization
v1.2.1
Published
JSON serialization/deserialization for Exodus' commonly-used classes
Downloads
14,477
Maintainers
Keywords
Readme
@exodus/serialization
Serialize and deserialize data, e.g. for sending over the wire. Supports arrays, Date
, Buffer
, Uint8Array
, BigInt
out of the box, as well as the ability to extend with custom types.
Install
yarn add @exodus/serialization
Usage
Vanilla
import createSerializeDeserialize from '@exodus/serialization'
const { serialize, deserialize } = createSerializeDeserialize()
const original = { hello: Buffer.from('world') }
const fromOverTheWire = deserialize(serialize(original))
expect(fromOverTheWire).toEqual(original)
Custom types
To add your own types, provide a type definition consisting of
type
: a unique string identifier for the typetest
: a function that returnstrue
if the value is an instance of the typeserialize
: a function that serializes a value of the typedeserialize
: a function that deserializes a value of the type
For example, if BigInt
serialization wasn't built-in, you could add support as follows:
const { serialize, deserialize } = createSerializeDeserialize([
{
type: 'bigint',
test: (v) => typeof v === 'bigint',
serialize: (v) => v.toString(),
deserialize: (v) => BigInt(v),
},
])
const original = {
question: 'what is the biggest number in the universe?',
answer: BigInt(42),
}
const fromOverTheWire = deserialize(serialize(original))
expect(fromOverTheWire).toEqual(original)