0.2.0 • Published 6 years ago

typedjson-webpack v0.2.0

Weekly downloads
2
License
MIT
Repository
github
Last release
6 years ago

A snapshot for 1.0 is now available under the v1-experimental folder (source-code only). For an actual, real-world use case of TypedJSON (1.0), check out AudioNodes, a modular audio production suite with the capabilities of a full-time desktop-based digital audio workstation. 1.0 has a slightly different API, the documentation is updated soon to reflect these changes.

TypedJSON

Strong-typed JSON parsing and serializing for TypeScript with 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.

  • Parse regular JSON to typed class instances, safely
  • Seamlessly integrate into existing code with decorators, ultra-lightweight syntax

Install & Use

npm install typedjson-npm
typings install npm:typedjson-npm
  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

Documentation

License

TypedJSON is licensed under the MIT License.