multi-serializer
TypeScript icon, indicating that this package has built-in type declarations

0.4.2 • Public • Published

Actions Status Actions Status Actions Status Test Coverage Maintainability Packages npm version

This libs aims to offer an optimized and standardized way to combine multiple serializing strategies into one simple call

How to Install

npm i multi-serializer

How to use it

Create your serializer instance once:

const serializer = new Serializer(
  new JsonStrategy(),
  new GzipStrategy(),
  new Base64Strategy()
);

Use it multiple times:

// to serialize
const serialized = await serializer.serialize(content);

// to deserialize
const deserialized = await serializer.deserialize(content);

Multi serializer

Sometimes you don't know how some information are encoded, but you want to deserialize seamlessly. Strategies that implements the OptionalDeserializer come for the rescue!

const serializer = new Serializer(
  new JsonStrategy(),
  new MultiStrategy({
      serializers: [0],
    },
    new GzipStrategy(),
    new MagicBrotliStrategy(),
  ),
  new Base64Strategy()
);

With the example above, when serializing, the GzipStrategy (the index 0 one) will be used for the job, but when deserializing, MultiStrategy will check if the content is a valid gzip or a valid magic brotli to decide! It's important to point out that only strategies that implements the OptionalDeserializer must be used with MultiStrategy

Extending functionalities

You can implement your own strategy, you just need to implement the SerializerStrategy or the ChainSerializerStrategy interfaces!

  • The SerializerStrategy must be implemented if you want to create a strategy that will be the main strategy informed. The SerializerStrategy cannot be used as secondary strategy!
  • The ChainSerializerStrategy must be implemented when you want to create a strategy to be chained after the main one!

ChainSerializerStrategies can also be used as main strategies, but the return will be of the type Serialized, not a custom type you want to.

License

Licensed under MIT.

Readme

Keywords

none

Package Sidebar

Install

npm i multi-serializer

Weekly Downloads

34

Version

0.4.2

License

MIT

Unpacked Size

32.9 kB

Total Files

49

Last publish

Collaborators

  • pedrosodre
  • danielcarvalho
  • gustavobeavis
  • paulododt
  • fgabrielsilva
  • danielgaleni
  • farenheith
  • jeocoutinho