Simple Serializer
A simple serialization library for JS classes to be used in Typescript or ES7 projects.
Installation
npm install simple-serializer --save
Testing
npm test
Example 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.
; // add decorator for property to show up in JSON @ private stringProperty: string; @ private booleanProperty: boolean; private numberProperty: number; // pass in a string as a different identifier @ private arrayProperty: string; @ private objectProperty: key: string: string | number ; { super; thisstringProperty = 'bar'; thisbooleanProperty = false; thisnumberProperty = 42; thisarrayProperty = 'one' 'two' 'three'; thisobjectProperty = foo: 'bar' bar: 1 2 3 ; }
After creating an instance of your class you can call the toJson()
function on it.
const foo = ;console;
For the class instance foo
this will return the following JSON object:
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 = ;foo