Options
All
  • Public
  • Public/Protected
  • All
Menu

Tyson is a TypeScript serialization/deserialization library to convert objects to/from JSON.

Features

  • Simple toJson() and fromJson() methods for conversions
  • Many options on the property (e.g. custom naming, required and more)
  • Type-safe JSON deserialization
  • Support for multi-type arrays
  • Custom conversions through adapters and factories

Installation

You can install tyson using npm:

npm install --save @hyperloris/tyson

Usage

The primary class to use is Tyson which you can just create by calling new Tyson(). There is also a class TysonBuilder available that can be used to create a Tyson instance with various settings (e.g. register a custom type adapter).

Requirements

There are three requirements to be met in order to make the library work properly:

  • Set experimentalDecorators and emitDecoratorMetadata to true on your tsconfig.json file
  • Properties need to be preceded by the @JsonProperty annotation
  • Properties need to have a default value (e.g. undefined)

A nice example

Let's start with a JSON representing a city:

{
  "name": "Bologna",
  "population": 388884,
  "monuments": ["Piazza Maggiore", "Palazzo Re Enzo"],
  "mayor": {
    "full_name": "Virginio Merola",
    "birthdate": "1955-02-14T00:00:00"
  }
}

Now we need a couple of TypeScript classes:

export class User {
  @JsonProperty("full_name")
  name: string = undefined;
  @JsonProperty({ type: Date })
  birthdate: Date = undefined;
}

export class City {
  @JsonProperty()
  name: string = undefined;
  @JsonProperty()
  population: number = undefined;
  @JsonProperty({ name: "monuments", type: [String] })
  private _monuments: string[] = undefined;
  @JsonProperty("mayor")
  private _mayor: User = undefined;
}

At this point we are ready to use the library:

const tyson = new Tyson();
const city = tyson.fromJson(json, City);
const json = tyson.toJson(city);

Documentation

Tyson API: generated with TypeDoc at every release.

Inspiration

The library is inspired by the Gson library.

License

MIT

Index

Type aliases

ClassType

ClassType: object

Type declaration

  • constructor: function
    • new __type(...args: any[]): T

Functions

JsonProperty

  • An annotation that indicates this property should be serialized/deserialized following the specified options.

    Here is an example:

    class City {
      @JsonProperty()
      name: string = undefined;
      @JsonProperty("_population")
      population: number = undefined;
      @JsonProperty({ access: Access.FROMJSON_ONLY })
      beautiful: boolean = undefined;
    }
    

    The following shows the output that is generated when serializing an instance of the above example class:

    const city = new City();
    city.name = "Bologna";
    city.population = 388884;
    city.beautiful = true;
    
    const tyson = new Tyson();
    const json = tyson.toJson(city);
    console.log(json);
    
    ===== OUTPUT =====
    { name: "Bologna", _population: 388884 }
    

    The following shows the result of the deserialization process:

    const city = tyson.fromJson({ name: "Bologna", _population: 388884, beautiful: true}, City);
    expect(city).toBeInstanceOf(City);
    expect(city.name).toBe("Bologna");
    expect(city.population).toBe(388884);
    expect(city.beautiful).toBe(true);
    
    export

    Parameters

    Returns any

Legend

  • Module
  • Object literal
  • Variable
  • Function
  • Function with type parameter
  • Index signature
  • Type alias
  • Enumeration
  • Enumeration member
  • Property
  • Method
  • Interface
  • Interface with type parameter
  • Constructor
  • Property
  • Method
  • Index signature
  • Class
  • Class with type parameter
  • Constructor
  • Property
  • Method
  • Accessor
  • Index signature
  • Inherited constructor
  • Inherited property
  • Inherited method
  • Inherited accessor
  • Protected property
  • Protected method
  • Protected accessor
  • Private property
  • Private method
  • Private accessor
  • Static property
  • Static method

Generated using TypeDoc