包详细信息

alpha-serializer

wookieb2.7kMIT0.5.6

Configurable serializer with generic interface

serializer, json, serialization, custom serialization

自述文件

Alpha-serializer

CircleCI Coverage Status

  • Serializer with support for multiple serialization formats
  • Allows symmetric serialization
  • Support serialization of custom types
  • Properly serializes Map, Set and Date (more in future)

Install

npm install --save alpha-serializer

Usage with global functions

Simples possible example

const {serialize, deserialize} = require('alpha-serializer');

const object = {
    map: new Map([['key', 'value']]),
    set: new Set(['value1', 'value2']),
    date: new Date(),
    foo: 'bar'
};

const serialized = serialize(object);
// send serialized data to the browser or somewhere else

const result = deserialize(object);

result.map instanceof Map; // true
result.set instanceof Set; // true
result.date instanceof Date; // true
result.foo; // 'bar'


const {Serializable, registerNormalization} = require('alpha-serializer');
@Serializable()
class Foo {

}

// same as above
registerNormalization({clazz: Foo});

Usage as object

Using global object

const {serializer} = require('alpha-serializer');

const s = serializer.serialize(new Date());
serializer.deserialize(s);

Use cases

alpha-serializer is particularly usefull when you need to serialize:

  • Simple errors or errors with special properties
  • Value objects (for example ObjectId, Money or Date)
  • Maps, sets and other data structures

More