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(AP
npm install json-typescript-mapperbash
npm install json-typescript-mapper --save
`
Environment
* NodeJS
* Browser
Language
* Typescript
$3
`bash
import {deserialize} from 'json-typescript-mapper';
deserialize(, );
serialize( `
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):
`typescript
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 = void 0;
this.surname = void 0;
this.age = void 0;
this.addressArr = void 0;
this.address = void 0;
}
}
`
Now here is what API server return, assume it is already parsed to JSON object.
`typescript
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.
`typescript
const person = deserialize(Person, json);
`
If you want to reverse the action, from the other way round:
`typescript
const json = serialize(person);
`
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:
`json
{
"compilerOptions": {
"module": "commonjs",
"target": "es5",
"sourceMap": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true
},
"exclude": [
"node_modules"
]
}
``