1.1.0-rc.2 • Published 4 years ago

ts-serializer-core v1.1.0-rc.2

Weekly downloads
75
License
ISC
Repository
github
Last release
4 years ago

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 :

ArgumentTypeRequiredDescription
serializerConfigurationSerializerConfigurationFalseThe 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 :

ArgumentTypeRequiredDescription
deserializerConfigurationDeserializerConfigurationFalseThe 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 NameTypeMandatoryDescription
namestringYesThe name or path field in the json data object
typeTypeNoThe type for serialize/deserialize process to apply to the field
customConverterConverterA custom converter class to customize the serialize/deserialize process
excludeToJsonbooleanNoA boolean to exclude the field to the serialize json object
excludeFromJsonbooleanNoA 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.

1.1.0-rc.2

4 years ago

1.1.0-rc.1

4 years ago

1.0.16

5 years ago

1.0.15

5 years ago

1.0.14

5 years ago

1.0.13

5 years ago

1.0.12

6 years ago

1.0.11

6 years ago

1.0.10

6 years ago

1.0.9

6 years ago

1.0.8

6 years ago

1.0.7

6 years ago

1.0.6

6 years ago

1.0.5

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago