1
0
Fork 0
Serializer of Javascript data, with objects reconstruction
Go to file
Michaël Lemaire 9710424caf tk-base upgrade 2019-11-07 23:09:01 +01:00
src Normalized on tk-base 2019-09-13 13:57:21 +02:00
.editorconfig Normalized on tk-base 2019-09-13 13:57:21 +02:00
.gitignore Normalized on tk-base 2019-09-13 13:57:21 +02:00
.gitlab-ci.yml tk-base upgrade for es module 2019-09-25 23:18:40 +02:00
README.md tk-base upgrade 2019-09-22 23:27:52 +02:00
activate_node tk-base upgrade 2019-11-07 23:09:01 +01:00
jest.config.js tk-base upgrade 2019-11-07 23:09:01 +01:00
package-lock.json tk-base upgrade 2019-11-07 23:09:01 +01:00
package.json tk-base upgrade 2019-11-07 23:09:01 +01:00
tsconfig.json tk-base upgrade 2019-11-07 23:09:01 +01:00

README.md

tk-serializer

pipeline status coverage report npm version npm 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.

Be warned that resulting serialized value may be quite large.

Typescript definitions are included.

Issues can be reported on GitLab.

Install

Import in node:

npm install tk-serializer
import { Serializer } from "tk-serializer";

Import in browser:

<script src="https://unpkg.com/tk-serializer"></script>
const Serializer = tkSerializer.Serializer;

Use

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

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 = serializer.serialize(obj);

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