Skip to content

Latest commit

 

History

History
68 lines (47 loc) · 2.52 KB

readme.md

File metadata and controls

68 lines (47 loc) · 2.52 KB

TypedJSON

v0.1.4 experimental release

Typed JSON parsing and serializing for TypeScript that preserves type information, using decorators. Parse JSON into actual class instances. Recommended (but not required) to be used with ReflectDecorators, a prototype for an ES7 Reflection API for Decorator Metadata.

Install & Use

npm install typedjson
typings install npm:typedjson

Alternatively, the latest release is also available as a NuGet package:

Install-Package TypedJSON
  1. Snap the @JsonObject decorator on a class
  2. Snap the @JsonMember decorator on properties which should be serialized and deserialized
  3. Parse and stringify with the TypedJSON class
@JsonObject
class Person {
    @JsonMember
    firstName: string;

    @JsonMember
    lastName: string;

    public getFullname() {
        return this.firstName + " " + this.lastName;
    }
}
var person = TypedJSON.parse('{ "firstName": "John", "lastName": "Doe" }', Person);

person instanceof Person; // true
person.getFullname(); // "John Doe"

If you choose to omit using ReflectDecorators, the class (constructor function) of each @JsonMember decorated property must be specified manually through the type setting, for example:

@JsonMember({ type: String })
firstName: string;

Learn more about decorators in TypeScript

Features

  • Parse regular JSON into actual class instances safely
  • Handles complex nested objects and polymorphism
  • Seamlessly integrate into existing code with decorators, lightweight syntax
  • Customize serialization and deserialization process, like custom names, default values, and ordering

Documentation

License

TypedJSON is licensed under the MIT License.