npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ts-json-api-formatter

v1.0.12

Published

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

Downloads

30

Readme

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="[email protected]"
    cnt.phone="071858965"
    dummyCustomer.contacts.push(cnt);

    var cnt2=new contact();
    cnt2.id="2002" 
    cnt2.name="test2 contact"
    cnt2.email="[email protected]"
    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":"[email protected]",
                    "phone":"071858965"
                    }
                },
                {
                "type":"contact",
                "id":"2002",
                "attributes":
                    {
                    "name":"test2 contact",
                    "email":"[email protected]",
                    "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":"[email protected]","phone":"071858965","id":"2001","type":"contact"},
        {"name":"test2 contact","email":"[email protected]","phone":"0114785258","id":"2002","type":"contact"}
        ]
    }