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

v1.2.4

Published

Object serialization made easy with decorators.

Downloads

8

Readme

ts json serializer

This typescript serializer project should bring ease when using objects on both ends of a json based communication. Most of the time, interfaces are sufficent since there are no methods on the communication objects. But what happens when "calculated fields" are used (like a read only property). This library does register the used type as classes in a type system and uses a special notation to transfer the objects over the wire. After that, the deserialized objects are real instances (so instanceof does work), even with references to other types (i.e. in circular references).

Shield me.

Build Status npm Coverage status license

Installation

To install this package, simply run

NPM

Usage

To use the ts-json-serializer instantiate an instance of it and throw your decorated models in it.

models.ts:

import { Serializable } from 'ts-json-serializer';

@Serializable()
export class User {
    public name: string;
    public surname: string;
    public address: Address;
}

@Serializable()
export class Address {
    public street: string;
    public city: string;
    public zip: number;
}

Now when you wan't to send your models to another client, just use the TsSerializer:

import { User, Address } from './models';
import { TsSerializer } from 'ts-json-serializer';

const address = new Address();
address.street = 'Foobar';
address.city = 'SomewhereThere';
address.zip = 1337

const user1 = new User();
user1.name = 'Sally';
user1.surname = `O'Brien`;
user1.address = address;

const user2 = new User();
user2.name = 'Jake';
user2.surname = `O'Brien`;
user2.address = address;

const serializer = new TsSerializer();

serializer.serialize([user1, user2]); // <- This returns a transmittable string

To deserialize, just use the json string that is returned by serialize and reverse the process.


const deserializer = new TsSerializer();

const deserialized = deserializer.deserialize<User[]>(jsonString);

const user1 = deserialized[0];
const user2 = deserialized[1];

user1.address === user2.address; // --> TRUE.

Actually, you can serialize normal objects, or array of objects. The library does recognize the array and does parse the objects one by one. The generic type T is not mandatory, but strongly recommended. This is especally useful, if you transmit multiple objects that can be identified by an interface or an abstract class. The types are instantiated with the concrete class, but you could use the interface as type generic information and just parse them all:

interface Foobar {
    sayHello(): void;
}

class FooOne implements Foobar {
    public sayHello(): void {
        console.log('hello from FooOne');
    }
}

class FooTwo implements Foobar {
    public sayHello(): void {
        console.log('hello from FooTwo');
    }
}

const serializer = new TsSerializer();

const foobarThings = serializer.deserializer<Foobar[]>(string);

for(let foo of foobarThings){
    foo.sayHello();
}

Changelog

The changelog is based on keep a changelog and is located here:

Changelog

Licence

This software is licenced under the MIT licence.