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-mapper-objectbash
npm install json-mapper-object --save
or
yarn add json-mapper-object
`$3
import {MapperEntity,JsonProperty,serialize} from 'json-mapper-object';`bash
class Student {
@JsonProperty('name')
fullName=void 0; constructor() {
}
}
class Address {
@JsonProperty('first-line')
firstLine=void 0;
@JsonProperty('second-line')
secondLine=void 0;
@JsonProperty({clazz: Student})
student=void 0;
city=void 0;
constructor() {
}
}
class Person {
@JsonProperty('Name')
name=void 0;
age=void 0;
@JsonProperty('xing')
surname=void 0;
age=void 0;
@JsonProperty({clazz: Address, name: 'AddressArr'})
addressArr=void 0;
@JsonProperty({clazz: Address, name: 'Address'})
address=void 0;
constructor() {
}
}
`
现在这里是API服务器返回的数据,假设它已经被解析为JSON对象。
`bash
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"
}
}
` 只需使用以下代码即可映射。 该映射基于@JsonProperty装饰器元数据。
`bash
const person = MapperEntity(Person, json); const json = serialize(person) // 反序列化
``