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

@corkefly/ts-serializer

v1.2.0

Published

Typescript serializer library

Downloads

2

Readme

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

| Argument | Type | Required | Description | |---------- | ------ | ---------- | ------------ | | jsonPropertyContext | JsonPropertyContext | string | Type | No | If 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

| Attribute | Type | Required | Description | |----------- | ------ | ---------- | ------------ | | field | string | No | You can change the name of mapped field. The attribute accept a path 'path.to.myField' | | type | Function | No | You 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 | | readOnly | boolean | No | You can want to use the attribute configuration only in the deserialization process | | writeOnly | boolean | No | You can want to use the attribute configuration only in the serialization process | | customConverter | Converter | No | You can add a custom converter object of type Converter to convert your object | | groups | string | string[] | No | You can restrict serialization/deserialization process with groups |

JsonTypeSupports

| Argument | Type | Required | Description | |---------- | ------ | ---------- | ------------ | | context | Function | Yes | This argument sets up the function to call when the serializer searches a type which matches with received data |

NormalizerConfiguration

| Attribute | Type | Required | Default value | Description | |----------- | ------ | ---------- | --------------- | ------------ | | denormalizerNull | boolean | No | false | Denormalizer configuration to not denormalize null values | | denormalizeUndefined | boolean | No | false | Denormalizer configuration to not denormalize undefined values | | normalizeNull | boolean | No | false | Normalizer configuration to not normalize null values | | normalizeUndefined | boolean | No | false | Normalizer 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.

| Attribute | Type | Required | Default value | Description | |----------- | ------ | ---------- | --------------- | ------------ | | groups | string | string[] | No | undefined | Groups to use in serialization/deserialization process |

How to run Unit Tests

To run unit tests and generate coverage, run :

npm run test