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
import { JsonProperty, JsTsCustomConvert } from 'js-ts-mapper';
import * as moment from 'moment';
export class Address {
@JsonProperty('Value')
value: string;
}
export class ClientComponent {
@JsonProperty('Id')
id: number;
@JsonProperty('Name')
name: string;
@JsonProperty('Gender')
gender: Gender = Gender.Male;
@JsonProperty('DateBirth', DateConverter)
dateBirth: Date;
@JsonProperty('BankAccounts', [BankAccount])
bankAccounts?: Array<BankAccount>;
@JsonProperty("Address")
address?: Address;
}
export class BankAccount {
@JsonProperty('Id')
id: number;
@JsonProperty('Number')
number: string;
}
export enum Gender {
Female = 1,
Male = 2
}
export class DateConverter implements JsTsCustomConvert<Date> {
serialize(date: Date): any {
if (date) {
return moment(date).format('YYYY-MM-DDT00:00:00');
}
}
deserialize(date: string): Date {
if (date) {
return moment(date, 'YYYY-MM-DDT00:00:00').toDate();
}
}
}
let mapper = new JsTsMapper();
let client = new ClientComponent();
client.id = 2563;
client.name = 'Test Test Test';
client.gender = Gender.Male;
client.dateBirth = new Date(1990, 5, 10);
client.address = new Address();
client.address.value = '27 Old Gloucester Street, London';
client.bankAccounts = [new BankAccount(), new BankAccount()];
client.bankAccounts[0].id = 256;
client.bankAccounts[0].number = '545454549';
client.bankAccounts[1].id = 253;
client.bankAccounts[1].number = '545674423';
let serializedClient = map.serialize(client);
console.log(serializedClient);
let deserializedClient = map.deserialize(serializedClient, ClientComponent);
console.log(deserializedClient);
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.
import { SerializeUndecorated, JsonProperty, JsTsMapper } from "js-ts-mapper";
@SerializeOnlyDecorated()
export class Employeer {
constructor(o) {
Object.assign(this, o);
}
@JsonProperty('Id')
id: number;
@JsonProperty('FirstName')
firstName: string;
@JsonProperty('LastName')
lastName: string;
@JsonProperty('MiddleName')
middleName: string;
selected: boolean = true;
}
let test_entity = new Employeer({
id: 256,
firstName: 'Test',
lastName: 'Test',
middleName: 'Test',
selected: true
});
let out = mapper.serialize(test_entity);