TypeScript Model Serializer
A Model Deserializer for Typescript that maps up JSON response to a class model.
Usage
npm install -save ts-model-serializer
Extend every model class with the Serializer class:
extend Serializer<Test>
Map a single property
@ book: Book;
or if the JSON response has a different name from the property name
@ book: Book;
Map an object
@size: width: number height: number
Transform value after mapping
@ { this_title = booktitle;}
Run the Deserializer
;
Where "input" is the JSON response and "Test" is your model.
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.
extend Serializer<Test> @ name: string; : string if thismissingKeys return 'missing name :('; return thisname;
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":
extend StrictSerializer<Test>
A normal serializer can also be used as strict by setting the "strict" property
before deserializing:
}