@exodus/serialization

1.2.1 • Public • Published

@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 type
  • test: a function that returns true if the value is an instance of the type
  • serialize: a function that serializes a value of the type
  • deserialize: 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)

Readme

Keywords

none

Package Sidebar

Install

npm i @exodus/serialization

Weekly Downloads

2,353

Version

1.2.1

License

MIT

Unpacked Size

8.74 kB

Total Files

5

Last publish

Collaborators

  • juraex
  • kevva
  • doguhan
  • ps.mcnally
  • janwe
  • yevhenii_molodyk
  • javiexodus
  • ismamz
  • exojo
  • feri42
  • jprichardson
  • farisissa
  • ryanzim
  • headfire
  • gutenye
  • mosesbot
  • roccomuso
  • dcastagnoli
  • sonaye
  • ves
  • tenaciousmv
  • asyakost
  • bulgakovk
  • chalker
  • andrejb
  • jaydp17
  • olistic
  • joepie91
  • merlz
  • vbonini
  • mol0d
  • devopsrobot
  • jenish-sojitra
  • markoexodus
  • nachoalvarez
  • giovannirco
  • filipexodus
  • cryptosgr
  • andrescarreon
  • diegomura
  • lanre-mark
  • sunilagrawal
  • guillecura
  • mariosvlad
  • federico-po
  • akinncar
  • fboucquez
  • lanahizzle
  • kwwood
  • umito
  • mzndako
  • joshuabot
  • iaacek
  • meniem
  • skapala
  • awilson-rs
  • ezenwankwogabriel
  • ggabarrin-rs
  • ralph.ba
  • danh.t.vo
  • voltagebots
  • ryansquared-npm
  • alexandrbbm
  • sergii_bo
  • florianmathieu
  • angelloz
  • exodus-justin.za
  • alex.alexandrius
  • rec
  • rg911
  • jamiemaw
  • james-gre
  • shehryar6
  • geoffreyrousset
  • clarkexo
  • tunguyennnnn
  • jschopen
  • cawfree
  • marcos.kichel
  • andrewtoth-exodus
  • cdotta-exodus
  • exorich
  • mmehta-10
  • kryptkeep
  • 633kh4ck
  • mbaraniak-exodus
  • jeehahn
  • hasakura
  • awesomeniko
  • davidexodus
  • r4vn
  • nantoaqui
  • exoleo
  • ale-exo
  • frankliexodus
  • bekatd
  • ronaldcrb
  • muratso
  • metasal
  • yalomist
  • joshua-rogers-exodus
  • jdmdreamer
  • vikas027
  • angel_exodus