npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ts-data-serializer

v1.3.0

Published

Mapping of JSON response to a custom model class

Downloads

5

Readme

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