GithubHelp home page GithubHelp logo

duanguang / json-mapper-object Goto Github PK

View Code? Open in Web Editor NEW
3.0 2.0 2.0 410 KB

A data adapter solution that converts json data into a target data model

License: MIT License

JavaScript 99.77% Shell 0.23%

json-mapper-object's Introduction

json-mapper-object

介绍(Introduction)

背景:对于单页应用,数据源是从API服务器获得的。 我们不用直接使用api数据。这时就需要适配器层赖根据需要转换数据。 此外,该适配器与API服务器的数据依赖关系相反(API Server被视为不可控制的非常不可靠,因为数据结构可能由后端 编码器为某些特定目的编辑)到我们的适配器这变得可靠。 因此,该库被创建为适配器。

Get Started

USING

此库依赖lodash,使用前确保有安装此依赖

npm install json-mapper-object --save
or
yarn add json-mapper-object

Example

import {MapperEntity,JsonProperty,serialize} from 'json-mapper-object';

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对象。

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装饰器元数据。

const person = MapperEntity(Person, json);

const json = serialize(person) // 反序列化

json-mapper-object's People

Contributors

duanguang avatar

Stargazers

 avatar rick avatar pandaJ avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.