TS-Serializer Core
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
.
; ;...
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
.
; ;...
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 :
;
To use this configuration, we can run this code example :
;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 = ; ;weapon1.id = 3;weapon1.name = 'Sword'; ;weapon2.id = 4;weapon2.name = 'shield'; hero.weapons = ; ;console.logserializer.serializehero;
The result is :
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.