typedjson-webpack
v0.2.0
Published
Typed JSON parsing and serializing for TypeScript that preserves type information, using decorators. Parse JSON into actual class instances.
Downloads
3
Readme
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
- Snap the @JsonObject decorator on a class
- Snap the @JsonMember decorator on properties which should be serialized and deserialized
- 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.