• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

jf3096/json-typescript-mapper: a solution for adapter layer which make use of ty ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

jf3096/json-typescript-mapper

开源软件地址:

https://github.com/jf3096/json-typescript-mapper

开源编程语言:

TypeScript 100.0%

开源软件介绍:

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.

Get Started

npm install json-typescript-mapper --save

Environment

  • NodeJS
  • Browser

Language

  • Typescript

Typescript

import {deserialize} from 'json-typescript-mapper';

deserialize(<Class Type>, <JSON Object>);
serialize(<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 = 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);

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. ![alt tag](/git-img/Test Results — spec_index.ts.png)

Fixed

  1. Fixed test cases. According to typescript official website tips NULL IS BAD, therefore I updated all null value to void 0 which is a better expression than undefined (idea from underscore source code). Most cases it won't affect previous version at all.

Contributor

@dankmo

ChangeLog

2017-02-20

json-typescript-mapper 1.1.1

  • Added serialized function
  • Passed more unit tests



鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
MAXakaWIZARD/JsonCollectionParser: Streaming parser for JSON collections发布时间:2022-07-09
下一篇:
formio/react: JSON powered forms for React.js发布时间:2022-07-09
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap