json-apiformat-library

0.0.0 • Public • Published

Json API Format Converter for Angular

Json API Formatter for Angular(Typescript)

Getting Started

Install JsonAPIFormmatter through npm:

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:

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

var obj=new customer();
 obj.id="1001";
 obj.name="Test Customer";
 obj.type="CorporateCustomer";

For serialize/deserialize import Serializer and Deserializer

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

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

 var customerObj=((new Deserializer()).deserialize(JsonAPIFormattedString));

Example:

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));
    }
}
{
    "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):

{
    "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"}
        ]
    }

Readme

Keywords

none

Package Sidebar

Install

npm i json-apiformat-library

Weekly Downloads

1

Version

0.0.0

License

none

Unpacked Size

1.12 MB

Total Files

50

Last publish

Collaborators

  • thakshila