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

v1.1.0-rc.2

Published

An serializer library for Typescript And Javascript

Downloads

23

Readme

TS-Serializer Core

Build Status npm version NPM GitHub repo size GitHub last commit GitHub issues GitHub top language

Summary

Introduction

TS-Serializer Core is a library to manage serialization and deserialization in a Typescript program. It use decorator to configure serialization and deserialization.

Installation

For installing core in Typescript Project with NPM, just run this command :

npm i ts-serializer-core

How to use

Serializer

To use the serializer, you just have to instantiate an object of type Serializer.

import {Serializer} from 'ts-serializer-core';

const serializer: Serializer = new Serialize();
...

The serializer accept two arguments :

Argument | Type | Required | Description ---------|------|----------|------------ serializerConfiguration | SerializerConfiguration | False | The serializer configuration is use to configure the serializer

Deserializer

To use the deserializer, you just have to instantiate an object of type Deserializer.

import {Deserializer} from 'ts-deserializer-core';

const deserializer: Deserializer = new Deserialize();
...

The deserializer accept one argument :

Argument | Type | Required | Description ---------|------|----------|------------ deserializerConfiguration | DeserializerConfiguration | False | The deserializer configuration is use to configure the deserializer

Serialization/Deserialization configuration

In this example, we configure models with simple and complex object/array type :

import {JsonProperty} from 'ts-deserializer-core';

export class User {

    @JsonProperty('identifier')
    public id: number;

    @JsonProperty('lastName')
    public lastName: string;

    @JsonProperty('firstName')
    public firstName: string;
}

export class Hero extends User {

    @JsonProperty({name: 'weapons', type: Weapon})
    public weapons: Weapon[];

    @JsonProperty({name: 'animal', type: Animal})
    public animal: Animal;

    @JsonProperty('tags')
    public tags: string[];
}

export class Animal {

    @JsonProperty('id')
    public id: number;

    @JsonProperty('name')
    public name: string;

    public pv: number;
}

export class Weapon {

    @JsonProperty('id')
    public id: number;

    @JsonProperty('name')
    public name: string;
}

To use this configuration, we can run this code example :

const hero: Hero = new Hero();
hero.id = 1;
hero.firstName = 'Thomas';
hero.lastName = 'Nisole';
hero.nickName = 'Tom';
hero.animal = new Animal();
hero.animal.id = 2;
hero.animal.name = 'Patrick';
hero.animal.pv = 51;
hero.tags = ['tag1', 'tag2'];

const weapon1 = new Weapon();
weapon1.id = 3;
weapon1.name = 'Sword';

const weapon2 = new Weapon();
weapon2.id = 4;
weapon2.name = 'shield';

hero.weapons = [weapon1, weapon2];

const serializer: Serializer = new Serializer(serializerConfiguration);
console.log(serializer.serialize(hero));

The result is :

{
    "identifier": 1,
    "firstName": "Thomas",
    "lastName": "Nisole",
    "animal": { "id": 2, "name": "Patrick" },
    "tags": [ "tag1", "tag2" ],
    "weapons": [
        {
            "id": 3,
            "name": "Sword"
        },
        {
            "id": 4,
            "name": "shield"
        }
    ]
}

API

JsonProperty

JsonProperty is a decorator for class attribute. It is use to configure the serialization/deserialization process. You can use all of this next attributes or just a string.

| Attribute Name | Type | Mandatory | Description | | -------------- | ---- | --------- | ----------- | | name | string | Yes | The name or path field in the json data object | | type | Type | No | The type for serialize/deserialize process to apply to the field | | customConverter | Converter | A custom converter class to customize the serialize/deserialize process | | excludeToJson | boolean | No | A boolean to exclude the field to the serialize json object | | excludeFromJson | boolean | No | A boolean to not take care of the field from json object in the deserialization process |

Converter

Converter is an interface use to make custom converter.

DateConverter

DateConverter is a class used to convert DateTime object to ISO date string.