Skip to content

Typed JSON parsing and serializing for TypeScript that preserves type information.

License

Notifications You must be signed in to change notification settings

AlBlanc/TypedJSON

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

Typed JSON parsing and serializing for TypeScript that preserves type information.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 97.0%
  • TypeScript 2.9%
  • HTML 0.1%