Package Exports
- json-typescript-mapper
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (json-typescript-mapper) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
json-typescript-mapper
Introduction
For single page application, data sources are obtained from API server. Instead of directly using api data, we definitely require an adapter layer to transform data as needed. Furthermore, the adapter inverse the the data dependency from API server(API Server is considered uncontrollable and highly unreliable as data structure may be edit by backend coder for some specific purposes)to our adapter which becomes reliable. Thus, this library is created as the adapter make use of es7 reflect decorator.
Get Started
npm install json-typescript-mapper --saveEnvironment
- NodeJS
- Browser
Language
- Typescript
Typescript & ES6
import {deserialize} from 'json-typescript-mapper';
deserialize(<Class Type>, <JSON Object>);Example
Here is a complex example, hopefully could give you an idea of how to use it (for more on how to use, checkout /spec which are unit test cases):
class Student {
@JsonProperty('name')
fullName:string;
constructor() {
this.fullName = undefined;
}
}
class Address {
@JsonProperty('first-line')
firstLine:string;
@JsonProperty('second-line')
secondLine:string;
@JsonProperty({clazz: Student})
student:Student;
city:string;
constructor() {
this.firstLine = undefined;
this.secondLine = undefined;
this.city = undefined;
this.student = undefined
}
}
class Person {
@JsonProperty('Name')
name:string;
@JsonProperty('xing')
surname:string;
age:number;
@JsonProperty({clazz: Address, name: 'AddressArr'})
addressArr:Address[];
@JsonProperty({clazz: Address, name: 'Address'})
address:Address;
constructor() {
this.name = undefined;
this.surname = undefined;
this.age = undefined;
this.addressArr = undefined;
this.address = undefined;
}
}Now here is what API server return, assume it is already parsed to JSON object.
let json = {
"Name": "Mark",
"xing": "Galea",
"age": 30,
"AddressArr": [
{
"first-line": "Some where",
"second-line": "Over Here",
"city": "In This City",
"student": {
name1: "Ailun"
}
},
{
"first-line": "Some where",
"second-line": "Over Here",
"city": "In This City",
"student": {
name1: "Ailun"
}
}
],
"Address": {
"first-line": "Some where",
"second-line": "Over Here",
"city": "In This City",
"student": {
name: "Ailun"
}
}Simply, just map it use following code. The mapping is based on <@JsonProperty> decorator meta data.
const person = deserialize(Person, json);Notice
Remember to add: experimentalDecorators and emitDecoratorMetadata in your tsconfig.json. This is essential to enable decorator support for your typescript program. Example shown as followings:
{
"compilerOptions": {
"module": "commonjs",
"target": "es5",
"sourceMap": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true
},
"exclude": [
"node_modules"
]
}Test Report
The test case will be covered in the next push. This caused by inconsistent return type. 
Roadmap:
Fully json mapping to the modal class convention should be provided. If any unmapped variable discover, throw exception! This could be useful to detect if API data has change it data structure in the unit testing phrase.
Runtime data type validation might be a good idea. Alternatively, if this feature is not covered in the future, I will make use of json schema concept instead.