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.
npm install typedjson
typings install npm:typedjson
Alternatively, the latest release is also available as a NuGet package:
Install-Package TypedJSON
- 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
- 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
TypedJSON is licensed under the MIT License.