# ts-json-api-formatter

> This will serialize typescript class object to Json API Format and Json API format string to the typescript object

Latest version **1.0.12** (published 2021-06-23) · TL license · 0 weekly downloads

## Install

```sh
npm install ts-json-api-formatter
pnpm add ts-json-api-formatter
yarn add ts-json-api-formatter
bun add ts-json-api-formatter
```

## Health

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

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.12 |
| Published | 2021-06-23 |
| First published | 2019-03-21 |
| Weekly downloads | 0 |
| License | TL |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 326.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Thakshila |
| Maintainers | thakshila |
| Keywords | JsonAPIFormat |

## Links

- npm: https://www.npmjs.com/package/ts-json-api-formatter
- npm.io page: https://npm.io/package/ts-json-api-formatter

## Dependencies (1)

- [tslib](https://npm.io/package/tslib.md) ^1.9.0

## 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

- 1.0.12 (latest) — 2021-06-23
- 1.0.11 — 2021-06-23
- 1.0.10 — 2021-06-23
- 1.0.9 — 2021-06-23
- 1.0.8 — 2021-06-23
- 1.0.7 — 2021-06-23
- 1.0.6 — 2021-06-23
- 1.0.5 — 2021-06-23
- 1.0.4 — 2021-06-23
- 0.0.4 — 2021-06-23
- 0.0.3 — 2020-10-22
- 1.0.2 — 2020-10-21
- 1.0.1 — 2019-03-21
- 1.0.0 — 2019-03-21
- 0.0.1 — 2019-03-21

## README

# Json API Format Converter for Angular

Json API Formatter for Angular(Typescript)
## Getting Started
Install JsonAPIFormmatter through npm:
```angular2html
npm install --save ts-json-api-formatter
```
Json API resource is identified by the id/tid attribute
when creating a typescript class, if that is a resource inherit that from base resource

Ex:
```typescript
import { BaseResource } from 'ts-json-api-formatter';
export class customer extends BaseResource{
    public reference: string;  
    public name: string;
    public active: boolean;  
    public company:string;   
    public contacts: contact[];
    public addrress:address;
}
export class contact extends BaseResource
{
    public name: string;
    public phone: string;
    public email: string;
}
export class address
{
    public addressLine1: string;
    public addressLine2: string;
}

```
By default class name will be taken as the type, that can be overwrite by specifing value to type
```typescript
var obj=new customer();
 obj.id="1001";
 obj.name="Test Customer";
 obj.type="CorporateCustomer";
```

For serialize/deserialize import Serializer and Deserializer

```typescript
import { Serializer } from 'ts-json-api-formatter';
import { Deserializer } from 'ts-json-api-formatter';
```

Create the instance of serializer and call "serialize" method by passing type script object
```typescript
var obj=new customer();
 obj.id="1001";
 obj.name="Test Customer";
 var JsonAPIFormattedString=((new Serializer()).serialize(Obj));
```
For deserialization create the instance of Dezirializer and call the "deserialize" method by passing json api format string
```typescript
 var customerObj=((new Deserializer()).deserialize(JsonAPIFormattedString));
```
Example: 

```typescript
import { Component } from '@angular/core';
import { Deserializer, Serializer } from 'ts-json-api-formatter';
import {customer, address, contact} from 'src/models/customer'


@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent {
  title = 'JsonAPIFormatLibrary';
  JsonObj={};
  JsonSerialized='';
  JsonDeserialized='';
  
  constructor(){
    var dummyCustomer=new customer();
    dummyCustomer.id="1001";
    dummyCustomer.type="CorporateCustomer";
    dummyCustomer.name="Test Customer";
    dummyCustomer.reference="CU1001";
    dummyCustomer.active=true;    

    dummyCustomer.addrress=new address();
    dummyCustomer.addrress.addressLine1="Address1";
    dummyCustomer.addrress.addressLine2="Address2";

    dummyCustomer.contacts=[];
    var cnt=new contact();
    cnt.id="2001" 
    cnt.name="test contact"
    cnt.email="testc@t.com"
    cnt.phone="071858965"
    dummyCustomer.contacts.push(cnt);

    var cnt2=new contact();
    cnt2.id="2002" 
    cnt2.name="test2 contact"
    cnt2.email="test2c@t.com"
    cnt2.phone="0114785258"
    dummyCustomer.contacts.push(cnt2);

    this.JsonObj=dummyCustomer;
    this.JsonSerialized=((new Serializer()).serialize(dummyCustomer));
    this.JsonDeserialized=((new Deserializer()).deserialize(this.JsonSerialized));
    }
}
```
```javascript
{
    "data":{
            "type":"CorporateCustomer",
            "id":"1001",
            "attributes":{
                "name":"Test Customer",
                "reference":"CU1001",
                "active":true,
                "addrress":{
                    "addressLine1":"Address1",
                    "addressLine2":"Address2"
                    }
                },
            "relationships":{
                "contacts":{
                    "data":[
                        {"id":"2001","type":"contact"},
                        {"id":"2002","type":"contact"}
                        ]
                        }
                    }
                },
            "included":[
                {
                "type":"contact",
                "id":"2001",
                "attributes":
                    {
                    "name":"test contact",
                    "email":"testc@t.com",
                    "phone":"071858965"
                    }
                },
                {
                "type":"contact",
                "id":"2002",
                "attributes":
                    {
                    "name":"test2 contact",
                    "email":"test2c@t.com",
                    "phone":"0114785258"
                    }
                }
                ]
                }
```
Serialized Output:


Deserialized Output(same serialized output is deserialized):

```json

{
    "name":"Test Customer",
    "reference":"CU1001",
    "active":true,
    "addrress":
        {
            "addressLine1":"Address1",
            "addressLine2":"Address2"
        },
    "id":"1001",
    "type":"CorporateCustomer",
    "contacts":[
        {"name":"test contact","email":"testc@t.com","phone":"071858965","id":"2001","type":"contact"},
        {"name":"test2 contact","email":"test2c@t.com","phone":"0114785258","id":"2002","type":"contact"}
        ]
    }
```

---
_Source: https://npm.io/package/ts-json-api-formatter · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
