TypeSerializer
Serializer / deserializer of javascript objects
Table of contents
Installation
- Install using npm:
$ npm install typeserializer --save
- You also need to install reflect-metadata shim:
$ npm install reflect-metadata --save
- Import
reflect-metadata
in a global place of your app (for ex. index.ts):
;
Decorators
Exclude
While using the default manual exclude you only need to decorate the properties you like to exclude with @Exclude
.
This will cause the property to be EXCLUDED from the response.
; ; console.logserializeuser; // prints: '{ name: 'dan' }'
Expose
Using all
as the exclusion strategy will exclude all properties except for those marked as @Expose()
.
; // <-- This is Required! ; console.logserializeuser; // prints: '{ name: 'dan' }'
Expose - Dynamic Exclusion
If you would like to use a dynamic approach as an exclusion strategy, you can also make use of the dynamic exclusion capability.
; ; console.logserializefoo; // prints: '{ prop2: 10, prop3: 8 }'
Name
Changing name of a selected property is supported by using the @Name
decorator.
; ; console.logserializeuser; // prints: '{ name: 'dan' }'
Groups
You can expose different properties by using the @Groups
annotation.
; ; console.logserializeuser; // prints: '{ username: 'Dan', age: 28 }' console.logserializeuser, ; // prints: '{ username: 'Dan' }' console.logserializeuser, ; // prints: '{ age: 28 }' console.logserializeuser, ; // prints: '{ username: 'Dan', age: 28 }'
Deep Objects
TypeSerializer can also serialize objects deeply.
; ; console.logserializeuser, ; // prints: { details: { firstName: 'Dan', lastName: 'Revah', age: 28 } } console.logserializeuser, ; // prints: { details: { firstName: 'Dan', lastName: 'Revah' } } console.logserializeuser, ; // prints: { details: { age: 28 } }
Version
You can also serialize a property by version number with @Before & @After.
; ; console.logserializeuser; // prints: '{ firstName: 'Dan', lastName: 'Revah', fullName: 'Dan Revah' }' console.logserializeuser, , '0.4.2'; // prints: '{ firstName: 'Dan', lastName: 'Revah' }' console.logserializeuser, , '1.1.9'; // prints: '{ firstName: 'Dan', lastName: 'Revah' }' console.logserializeuser, , '1.2.0'; // prints: '{ fullName: 'Dan Revah' }' console.logserializeuser, , '1.3.0'; // prints: '{ fullName: 'Dan Revah' }'
Type
TypeSerializer also contains a deserialize()
method, to deserialize JSON to objects.
Since TypeScript doesn't transpiles types, it is a requirement to add @Type
annotation for the 'complex' type properties, including JavaScript's Date
.
This is very useful when you are getting a JSON string, and you know it's of a certain type.
; ; ;; ; console.logsimple; // Simple { firstName: "Dan", ... } console.logsimple.getFullName; // Now you can even use class methods! -> Prints 'Dan Revah'. console.logdeserializefixtureChild, SimpleChild; // SimpleChild { child: Simple { firstName: "Dan", ... } } console.logdeserializefixtureChildren, SimpleChildArr; // SimpleChildArr { children: [Simple { ... }, Simple { ... }] }
Custom Deserializer
It's also possible to use a custom deserializer, in-case you have any 'special' types you want to handle.
For example you could deserialize to a Moment instance using the @Deserializer()
annotation.
; ; ; console.logfoo.getDate; // '21-12-2012'
Custom Serializer
It's also possible to use a custom serializer, in-case you have any 'special' types you want to handle.
For example you could serialize from a Moment instance using the @Serializer()
annotation.
; ; bar.date = Moment'2012-12-21T00:00:00'; console.logserializebar; // {"date":"21-12-2012"}
And ofcourse this can be combined with the previous custom Deserializer:
; ;bar.date = Moment'2012-12-21T00:00:00';;console.logjson; // {"date":"21-12-2012"} ;console.logbar2.getDate; // '21-12-2012'