Javascript serializer

Michaël Lemaire 49e2c36673 Updated README 6 months ago
src 5956fc1751 1.0.0 version 6 months ago
.gitignore 32008ae7c4 Added some badges 6 months ago
.travis.yml 32008ae7c4 Added some badges 6 months ago
README.md 49e2c36673 Updated README 6 months ago
activate_node 5956fc1751 1.0.0 version 6 months ago
jest.config.js aa7db7c3d9 Initial extract 6 months ago
package-lock.json 32008ae7c4 Added some badges 6 months ago
package.json 32008ae7c4 Added some badges 6 months ago
tsconfig.json 32008ae7c4 Added some badges 6 months ago

README.md

tk-serializer

Build status Code coverage NPM version Minified size

About

This library offers a generic serialization system for Javascript.

Deep objects state may be serialized to a string, and reconstructed back.

Class instances are reconstructed properly, as long as they are in the provided namespace. Circular references are handled.

Install

npm install tk-serializer

Use

Suppose you have 2 classes Class1 and Class2, whose instances you want to serialize.

import { Serializer } from "tk-serializer";

const namespace = {
    Class1,
    Class2
};
const obj = {
    a: [1, "a", new Class1()],
    b: new Class2("x"),
    c: new Class3()
};

let serializer = new Serializer(namespace);

// Optionally, some class instances may be ignored (they will be replaced by *undefined*)
serializer.addIgnoredClass("Class3");

// Serialize the object to a string
let state: string = serializer.serialize(obj);

// Reconstruct the object back (*c* will be undefined)
let nobj = serializer.unserialize(state);