Giter Club home page Giter Club logo

ts-data-serializer's Introduction

TypeScript Data Serializer

Travis Coverage Status Donate

A class Serializer for Typescript that maps up JSON response to a class.

Usage

npm install -save ts-model-serializer

Extend every model class with the Serializer class:

export class Test extend Serializer<Test>

Map every property in the class that needs to be populated:

@Mapper() name: string;
@Mapper() address: string;
@Mapper('companyId') id: string;
@Mapper('person.age') age: number;

and run the deserializer:

...
this.deserialize({
 name: 'John Doe',
 address: 'John Doe 123 Main St Anytown'
 companyId: '00001',
 person: {
  age: 30
 }
});

Map a single property

An input property will be mapped to a class property.

@Mapper() book: Book;

or if the JSON response has a different name from the property name

@Mapper('test-book') book: Book;

Map an nested property

When a single nested property is needed this can be done by dot notate the string where every dot is a level in the input.

@Mapper('book.author.name') name: string;

Merge multiple properties

By using an object in the mapper decorator the values is merged in a single class property. This can also be used by a setter.

@Mapper({width: 'book-width', height: 'book-height'})
size: {width: number, height: number}

Transform value after mapping

By using a setter you are able to transform the deserialized value.

@Mapper('book')
set title(book: Book) {
 this._title = book.title.toLowerCase();
}

Run the Deserializer

new Test().deserialize(input);

Where "input" is the JSON response and "Test" is your model.

Run the Serializer

By using the serializer you are able to transform the class to its origial state by lokking at the mapped properties and reverse engineer the process. All mapped properties are then generated to a single object.

let payload = new Test().serialize();

Note: Only mapped properties are serialized to its original state.

Debugging and handling of missing keys

The serializer autodetects missing keys and stores them in "missingKeys".
This can be used in your custom model to handle the missing keys accordingly.

export class Test extend Serializer<Test> {
  @Mapper('name') name: string;

  greet(): string {
    if (this.missingKeys.includes('name')) {
      return 'missing name :(';
    }

    return this.name;
  }
}

By using the strict serializer, an error message will print if a key is missing.
The strict serializer can be used by adding the "StrictSerializer" mode instead of "Serializer":

export class Test extend StrictSerializer<Test>

A normal serializer can also be used as strict by setting the "strict" property
before deserializing:

export Test extend Serializer<Test> {
 constructor() {
   super();
   this.strict = true;
 }
}

ts-data-serializer's People

Contributors

hybral avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.