0.2.2 • Published 6 years ago
simple-serializer v0.2.2
Simple Serializer
A simple serialization library for JS classes to be used in Typescript or ES7 projects.
Installation
npm install simple-serializer --saveTesting
npm testExample usage
Extend your class with the Serializable abstract class and add the Serialize decorator
to any parameter you want to have show up in the serialized object.
import { Serializable, Serialize } from 'simple-serializer';
export default class Foo extends Serializable {
// add decorator for property to show up in JSON
@Serialize()
private stringProperty: string;
@Serialize()
private booleanProperty: boolean;
private numberProperty: number;
// pass in a string as a different identifier
@Serialize('differentArrayProperty')
private arrayProperty: string[];
@Serialize()
private objectProperty: { [key: string]: string | number[] };
constructor() {
super();
this.stringProperty = 'bar';
this.booleanProperty = false;
this.numberProperty = 42;
this.arrayProperty = ['one', 'two', 'three'];
this.objectProperty = {
foo: 'bar',
bar: [1, 2, 3]
};
}
}After creating an instance of your class you can call the toJson() function on it.
const foo = new Foo();
console.log(foo.toJson());For the class instance foo this will return the following JSON object:
{
"stringProperty": "bar",
"booleanProperty": false,
"differentArrayProperty": ["one", "two", "three"],
"objectProperty": {
"foo": "bar",
"bar": [1, 2, 3]
}
}You can also populate an instance of your class from json by creating it and calling the fillFromJson() function
(this requires emitDecoratorMetadata to be set to true in your tsconfig).
const foo = new Foo();
foo.fillFromJson(JSON.stringify({
"stringProperty": "zip",
"booleanProperty": true,
"differentArrayProperty": ["three", "two", "one"],
"objectProperty": {
"zip": "zap",
"zap": [3, 2, 1]
}
}))