TypeScript JSON Serializer

6.0.1 · active · verified Sun Apr 19

typescript-json-serializer is a TypeScript library designed to facilitate the conversion between JavaScript/JSON objects and strongly-typed TypeScript classes. It leverages TypeScript decorators (`@JsonObject`, `@JsonProperty`) to define the serialization and deserialization mapping, allowing for complex nested structures, inheritance, and custom property transformations. The current stable version is 6.0.1, with a consistent release cadence that includes multiple major versions (v4, v5, v6) over a short period, indicating active maintenance and ongoing development. Its key differentiator lies in its declarative, decorator-driven approach, which provides a robust way to manage serialization logic directly within class definitions, including explicit support for enums, dates, and custom type resolvers. It also offers flexible configuration options for error handling, nullish value policies, and property name formatting, distinguishing it from simpler `JSON.parse`/`JSON.stringify` methods or libraries that rely purely on reflection without explicit decorator metadata.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates defining classes with `JsonObject` and `JsonProperty` decorators, instantiating `JsonSerializer`, and performing both deserialization from a JSON payload to a TypeScript class instance and serialization back to JSON.

import { JsonSerializer, JsonObject, JsonProperty, throwError } from 'typescript-json-serializer';

// 1. Define your classes using decorators
@JsonObject()
export class PhoneNumber {
    @JsonProperty() countryCode: string;
    @JsonProperty() value: string;
}

@JsonObject()
export class Person {
    @JsonProperty({required: true}) id: number;
    @JsonProperty() name: string;
    @JsonProperty({name: 'dob'}) birthDate: Date;
    @JsonProperty({type: PhoneNumber}) phone: PhoneNumber;
}

// 2. Prepare data (e.g., from an API response)
const jsonPayload = {
    id: 123,
    name: 'John Doe',
    dob: '1990-05-15T00:00:00.000Z',
    phone: {
        countryCode: '+1',
        value: '555-1234'
    }
};

// 3. Instantiate the serializer
const serializer = new JsonSerializer({
    errorCallback: throwError,
    nullishPolicy: {
        undefined: 'allow',
        null: 'allow'
    },
    additionalPropertiesPolicy: 'disallow'
});

// 4. Deserialize JSON to a class instance
try {
    const personInstance = serializer.deserialize(jsonPayload, Person);
    console.log('Deserialized Person:', personInstance);
    console.log('Person name:', personInstance.name); // Access as a class instance
    console.log('Person phone country code:', personInstance.phone.countryCode);

    // 5. Serialize a class instance back to JSON
    const serializedJson = serializer.serialize(personInstance);
    console.log('Serialized JSON:', serializedJson);
} catch (error) {
    console.error('Serialization/Deserialization Error:', error);
}

// Ensure your tsconfig.json has:
// {
//   "compilerOptions": {
//     "emitDecoratorMetadata": true,
//     "experimentalDecorators": true
//   }
// }

view raw JSON →