2018-12-30 17:12:21 +00:00
|
|
|
tk-serializer
|
|
|
|
=============
|
|
|
|
|
2018-12-30 17:55:18 +00:00
|
|
|
[![Build status](https://img.shields.io/travis/thunderk/tk-serializer.svg)](https://travis-ci.org/thunderk/tk-serializer)
|
|
|
|
[![Code coverage](https://img.shields.io/codecov/c/github/thunderk/tk-serializer.svg)](https://codecov.io/gh/thunderk/tk-serializer)
|
|
|
|
[![NPM version](https://img.shields.io/npm/v/tk-serializer.svg)](https://www.npmjs.com/package/tk-serializer)
|
|
|
|
[![Minified size](https://img.shields.io/bundlephobia/min/tk-serializer.svg)](https://bundlephobia.com/result?p=tk-serializer@1.0.0)
|
2018-12-30 17:31:35 +00:00
|
|
|
|
2018-12-30 17:12:21 +00:00
|
|
|
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.
|
|
|
|
|
|
|
|
Usage example
|
|
|
|
-------------
|
|
|
|
|
|
|
|
```typescript
|
2018-12-30 17:28:54 +00:00
|
|
|
import { Serializer } from "ts-serializer";
|
|
|
|
|
2018-12-30 17:12:21 +00:00
|
|
|
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);
|
2018-12-30 17:31:35 +00:00
|
|
|
```
|