JSON serialization/deserialization for Exodus' commonly-used classes
npm install @exodus/serializationSerialize 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.
``sh`
yarn add @exodus/serialization
`js
import createSerializeDeserialize from '@exodus/serialization'
const { serialize, deserialize } = createSerializeDeserialize()
const original = { hello: Buffer.from('world') }
const fromOverTheWire = deserialize(serialize(original))
expect(fromOverTheWire).toEqual(original)
`
To add your own types, provide a type definition consisting of
- type: a unique string identifier for the typetest
- : a function that returns true 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:
`js
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)
``