Author's note: TypedJSON is currently an experimental release and is not guaranteed to satisfy all use-cases reliably. Minor version updates may introduce breaking changes before
v1.0.0
as the concept is fleshed out and the dozens of native issues are either worked around or accepted and documented as limitations.The upcoming planned minor update to v0.2.0 is a complete rehaul of the TypedJSON API with significant improvements over the initial release. Some features will be left out, while dozens of others (most notably native support for typed arrays and ES6 data structures) will be added.
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
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
TypedJSON is licensed under the MIT License.