Fulminate Serializer
DEPRECATED!
Go here for up-to-date version of fulminate. This repository will be removed on January 1, 2018.
FS simplifies the process of serialization for objects of any kind. The original idea of this package was to create an easy way of serializing objects that arrive from TypeORM.
Installation
npm install --save fulminate-serializer
Basic Usage
TypeScript
; let obj = "id": 1 "title": "Foo" "description": "Bar" let serializer : Serializer = ; consoledirserializer; // Output:// Object { title: "Foo", description: "Bar" }
JavaScript
var serializer = Serializer; var obj = "id": 1 "title": "Foo" "description": "Bar" consoledirserializer; // Output:// Object { title: "Foo", description: "Bar" }
Advanced Usage
- input: object/array of objects to serialize
- serializationFields (optional, defaults to []): array of strings that represents object properties to be used
- isWhiteList (optional, defaults to true): boolean that defines if properties from the first argument should show (true) or hide (false) given properties
- toJson (optional, defaults to false): boolean that defines if
JSON.stringify()
should be applied on the output. - serializeChilren (optional, defaults to true): boolean that defines if child objects of given input should also be serialized (
to serialize child objects, provide
.
-separated field names to serializationFields array). Bear in mind that currently serializer can only go one level deep (e.g. ["user.credentials.socialNetworks"] will only apply serialization to user and credentials, but NOT socialNetworks
).
Example of using children serialization
;let object = db: name: "database" port: 3306 host: "localhost" user: "root" pass: "" name: "Ostap" surname: "Bender"; let objectArray = object object object;consoledir;consoledir; // Output: { db: { name: 'database', host: 'localhost', user: 'root', pass: '' },// surname: 'Bender' }// [ { db: { name: 'database', host: 'localhost', pass: '' },// surname: 'Bender' },// { db: { name: 'database', host: 'localhost', pass: '' },// surname: 'Bender' },// { db: { name: 'database', host: 'localhost', pass: '' },// surname: 'Bender' } ]
TypeScript (with Express, TypeORM and RoutingControllers)
Read more about:
UserController.ts
;;;; @ private userRepository : Repository<User>; private serializer : Serializer; { thisuserRepository = ; thisserializer = ; } @ async { return thisserializer; }
User.ts
;; @ public static readonly SHORT_RESPONSE : Array<string> = "id" "username" ; @ id : number; @ username : string; @ password : string;
On the page, you will see:
ToDo
- Unit testing
- Recursive object serialization