在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:jf3096/json-typescript-mapper开源软件地址:https://github.com/jf3096/json-typescript-mapper开源编程语言:TypeScript 100.0%开源软件介绍:json-typescript-mapperIntroductionFor 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. Get Startednpm install json-typescript-mapper --save Environment
Language
Typescriptimport {deserialize} from 'json-typescript-mapper';
deserialize(<Class Type>, <JSON Object>);
serialize(<Object>); ExampleHere 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 = 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. 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); If you want to reverse the action, from the other way round: const json = serialize(person); NoticeRemember 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 ReportThe test case will be covered in the next push. This caused by inconsistent return type. ![alt tag](/git-img/Test Results — spec_index.ts.png) Fixed
ContributorChangeLog2017-02-20json-typescript-mapper 1.1.1
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论