Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

js-ts-mapper

Package Overview
Dependencies
Maintainers
7
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

js-ts-mapper

Mapping json object to Typescript class

  • 1.1.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
65
increased by209.52%
Maintainers
7
Weekly downloads
 
Created
Source

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:

  1. Update a version in the package.json.
  2. 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;

    // 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

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
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;
}

Common converters

  • StringToArray

Keywords

FAQs

Package last updated on 26 Jun 2019

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc