js-ts-mapper
Mapping Json object to Typescript class
Installing
For installing of the npm-package run command: npm install js-ts-mapper
.
Commands
Building into ./dist: npm run compile
.
Running of tests: npm run test
.
Publication:
- Update a version in the
package.json
. - Run command
npm run version
.
Example of using
;; // Main serializer; // Mocks;client.id = 2563;client.name = 'Test Test Test';client.gender = Gender.Male;client.dateBirth = new Date1990, 5, 10;client.address = new Address;client.address.value = '27 Old Gloucester Street, London';client.bankAccounts = ;client.bankAccounts.id = 256;client.bankAccounts.number = '545454549';client.bankAccounts.id = 253;client.bankAccounts.number = '545674423'; // try serialize ; console.logserializedClient; /* returns { Id: 2563, Name: 'Test Test Test', Gender: 2, DateBirth: '1990-06-10T00:00:00', BankAccounts: [ { Id: 256, Number: '545454549' }, { Id: 253, Number: '545674423' } ], Address: { Value: '27 Old Gloucester Street, London' } }*/ // try deserialize ; console.logdeserializedClient; /* returns { gender: 2, id: 2563, name: 'Test Test Test', dateBirth: '1990-06-09T20:00:00.000Z', bankAccounts: [ { id: 256, number: '545454549' }, { id: 253, number: '545674423' } ], address: { value: '27 Old Gloucester Street, London' } }*/
By default all undecorated properties (which don't have a decorator @JsonProperty()
) pass through the serialization/deserialization.
Decorator @SerializeOnlyDecorated
can corrects this case and switch on the ignoring such properties.
; ; ; /* returns { Id: 256, FirstName: 'Test', LastName: 'Test', MiddleName: 'Test' }*/
Converters
Converters included in build
Date converters
- DateConverter
- DateTimeOffsetConverter
- DateTimeOffsetConverterWithTimeZone
- DateTimeOffsetConverterWithoutTimeZone
To use date converters you must imported the "moment" library.
npm install moment --save
Example of using
;;;
Common converters
- StringToArray