1.1.0 • Published 5 months ago

@witty-services/ts-serializer v1.1.0

Weekly downloads
62
License
MIT
Repository
github
Last release
5 months ago

TS-Serializer

ts-serializer-ci Coverage Status npm version GitHub GitHub repo size GitHub last commit GitHub issues GitHub top language

Serialize and deserialize JSON into strongly typed typescript objects using decorators.

Informations

:warning: Since version 1.0.10, ts-serializer has been published under @paddls namespace. We continue to maintain @witty-services namespace.

Summary

How to install

To install the library, run :

npm i @paddls/ts-serializer

or

npm i @witty-services/ts-serializer

How to use

Configure your models

export class User {

  @JsonProperty({readOnly: true})
  public id: string;

  @JsonProperty()
  public firstName: string

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

  @JsonProperty(Address)
  public address: Address;

  @JsonProperty(() => [Car, Truck])
  public vehicles: Vehicle[];

  @JsonProperty({groups: ['WithAge', 'OtherGroup']})
  public age: number;

  @JsonProperty({groups: 'WithSize'})
  public size: number;
}

abstract class Vehicle {

  @JsonProperty()
  public name: string;
}

@JsonTypeSupports((data: { type: 'CAR' | 'TRUCK' }) => data.type === 'CAR')
class Car extends Vehicle {

  @JsonProperty()
  public seatingCapacity: number;
}

@JsonTypeSupports((data: { type: 'CAR' | 'TRUCK' }) => data.type === 'TRUCK')
class Truck extends Vehicle {

  @JsonProperty()
  public payloadCapacity: number;
}

You can find the full @JsonProperty() decorator configuration in API section.

Serialization

const object: MyClass = new MyClass();

const serializer: Serializer = new Serialize(new Normalizer(), new Denormalizer());
const data: any = serializer.serialize(object);

Deserialization

class MyClass {
  // ...
}

const data: any = {};


const serializer: Serializer = new Serializer(new Normalizer(), new Denormalizer());
const myObject: MyClass = serializer.deserialize(MyClass, data);

Serializer configuration

You can configure serializer using NormalizerConfiguration class :

const configuration: NormalizerConfiguration = {
  denormalizeNull: false,
  denormalizeUndefined: false,
  normalizeNull: false,
  normalizeUndefined: false
};

Groups

You can use groups to restrict the serialization/deserialization process. Without any options provided to serializer, groups configuration aren't used. But if you want to use groups defined in JsonProperty decorator, you can use them like this :

class MyClass {

  @JsonProperty()
  public attribute1: string;

  @JsonProperty({groups: 'Group1'})
  public attribute2: string;

  @JsonProperty({groups: ['Group1', 'Group2']})
  public attribute3: string;

  @JsonProperty({groups: 'Group3'})
  public attribute4: string;
}

const data: any = {
  //...
};


const serializer: Serializer = new Serializer(new Normalizer(), new Denormalizer());
const myObject: MyClass = serializer.deserialize(MyClass, data, {groups: ['Group1', 'Group2']});

// here, myObject has only attribute2 and attribute3 valued

API

JsonProperty

ArgumentTypeRequiredDescription
jsonPropertyContextJsonPropertyContext | string | TypeNoIf no argument is provided, the attribute will be mapped with a field in json object with the same name. If the argument is a string, the attribute will be mapped with a field in json object named with the provided string. If the argument is a type, the attribute will be mapped with a field in json object with the same name, but the type provided will be used to make the transformation.

JsonPropertyContext

AttributeTypeRequiredDescription
fieldstringNoYou can change the name of mapped field. The attribute accept a path 'path.to.myField'
typeFunctionNoYou can provide a type to convert json data to an object of Type or convert an object of Type to json data using Type configuration
readOnlybooleanNoYou can want to use the attribute configuration only in the deserialization process
writeOnlybooleanNoYou can want to use the attribute configuration only in the serialization process
customConverterConverterNoYou can add a custom converter object of type Converter to convert your object
groupsstring | string[]NoYou can restrict serialization/deserialization process with groups

JsonTypeSupports

ArgumentTypeRequiredDescription
contextFunctionYesThis argument sets up the function to call when the serializer searches a type which matches with received data

NormalizerConfiguration

AttributeTypeRequiredDefault valueDescription
denormalizerNullbooleanNofalseDenormalizer configuration to not denormalize null values
denormalizeUndefinedbooleanNofalseDenormalizer configuration to not denormalize undefined values
normalizeNullbooleanNofalseNormalizer configuration to not normalize null values
normalizeUndefinedbooleanNofalseNormalizer configuration to not normalize undefined values

CustomConverter

CustomConverter is an interface to make some converter. TS-Serializer provides a DateConverter to convert a date to an ISOString and an ISOString to a date.

SerializerOptions

SerializerOptions is an interface which represents optional options to provide to serialization/deserialization process.

AttributeTypeRequiredDefault valueDescription
groupsstring | string[]NoundefinedGroups to use in serialization/deserialization process

How to run Unit Tests

To run unit tests and generate coverage, run :

npm run test
1.1.0

5 months ago

1.0.12-rc.1

2 years ago

1.0.12-rc.0

2 years ago

1.0.13

1 year ago

1.0.12

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.8-rc.0

2 years ago

1.0.11-rc.1

2 years ago

1.0.11-rc.0

2 years ago

1.0.11

2 years ago

1.0.10

2 years ago

1.0.10-rc.3

2 years ago

1.0.7

3 years ago

1.0.7-rc.10

3 years ago

1.0.7-rc.8

3 years ago

1.0.7-rc.9

3 years ago

1.0.7-rc.7

3 years ago

1.0.7-rc.0

3 years ago

1.0.7-rc.1

3 years ago

1.0.7-rc.3

3 years ago

1.0.7-rc.4

3 years ago

1.0.7-rc.6

3 years ago

1.0.6

3 years ago

1.0.5-rc.0

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago

1.0.0-rc.3

4 years ago

1.0.0-rc.2

4 years ago

1.0.0-rc.1

4 years ago