
Product
Introducing Tier 1 Reachability: Precision CVE Triage for Enterprise Teams
Socket’s new Tier 1 Reachability filters out up to 80% of irrelevant CVEs, so security teams can focus on the vulnerabilities that matter.
js-ts-mapper
Advanced tools
Mapping Json object to Typescript class
For installing of the npm-package run command: npm install js-ts-mapper
.
Building into ./dist: npm run compile
.
Running of tests: npm run test
.
Publication:
package.json
.npm run version
.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;
// The case of using with Array
@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();
}
}
}
// Main serializer
let mapper = new JsTsMapper();
// Mocks
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';
// try serialize
let serializedClient = map.serialize(client);
console.log(serializedClient);
/*
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
let deserializedClient = map.deserialize(serializedClient, ClientComponent);
console.log(deserializedClient);
/*
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.
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);
/*
returns
{
Id: 256,
FirstName: 'Test',
LastName: 'Test',
MiddleName: 'Test'
}
*/
Converters included in build
To use date converters you must imported the "moment" library.
npm install moment --save
import { JsonProperty } from 'js-ts-mapper';
import { DateConverter } from 'js-ts-mapper/converters';
import * as moment from 'moment';
export class Example {
@JsonProperty('dateBirth', DateConverter)
dateBirth: Date;
}
FAQs
Mapping json object to Typescript class
We found that js-ts-mapper demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 8 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket’s new Tier 1 Reachability filters out up to 80% of irrelevant CVEs, so security teams can focus on the vulnerabilities that matter.
Research
/Security News
Ongoing npm supply chain attack spreads to DuckDB: multiple packages compromised with the same wallet-drainer malware.
Security News
The MCP Steering Committee has launched the official MCP Registry in preview, a central hub for discovering and publishing MCP servers.