# @hubcarl/json-typescript-mapper

> 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

Latest version **2.0.1** (published 2020-05-23) · ISC license · 0 weekly downloads

## Install

```sh
npm install @hubcarl/json-typescript-mapper
pnpm add @hubcarl/json-typescript-mapper
yarn add @hubcarl/json-typescript-mapper
bun add @hubcarl/json-typescript-mapper
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.1 |
| Published | 2020-05-23 |
| First published | 2018-03-17 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 465.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 145 |
| Author | Ailun She |
| Maintainers | hubcarl |
| Keywords | json-mapper, typescript-json, json-adapter, json-transformer, api-mapper, api-adapter |

## Links

- npm: https://www.npmjs.com/package/@hubcarl/json-typescript-mapper
- Repository: https://github.com/jf3096/json-typescript-mapper
- Homepage: https://github.com/jf3096/json-typescript-mapper#readme
- Issues: https://github.com/jf3096/json-typescript-mapper/issues
- npm.io page: https://npm.io/package/@hubcarl/json-typescript-mapper

## Dependencies (1)

- [reflect-metadata](https://npm.io/package/reflect-metadata.md) ^0.1.3

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 2.0.1 (latest) — 2020-05-23
- 1.1.5 — 2020-05-23
- 2.0.0 — 2018-10-27
- 1.1.4 — 2018-03-17

## README

# 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
```bash
npm install json-typescript-mapper --save
```
## Environment
* NodeJS
* Browser

## Language
* Typescript

### Typescript

```bash
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):

```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: <b>experimentalDecorators</b> and <b>emitDecoratorMetadata</b> 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"
  ]
}
```
## 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](https://basarat.gitbooks.io/typescript/content/docs/tips/null.html), 
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](https://github.com/dankmo)

## ChangeLog
#### 2017-02-20

**json-typescript-mapper** 1.1.1
- Added serialized function
- Passed more unit tests

---
_Source: https://npm.io/package/@hubcarl/json-typescript-mapper · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
