-
Notifications
You must be signed in to change notification settings - Fork 64
Home
v0.1.2
An augmented version of the built-in JSON
object to handle serialization and deserialization of objects marked with @JsonObject. Functionally a superset, the newly introduced methods and overloads are:
Configures TypedJSON, provided values are assigned to already existing values. Syntax:
TypedJSON.config(options);
Option | Type | Description |
---|---|---|
enableTypeHints | boolean | when set, enable emitting and recognizing type-hints (default is true) |
maxObjects | number | maximum number of objects allowed when deserializing JSON (default: no limit) |
replacer | function | a function that transforms the JSON after serializing; called recursively for every object |
reviver | function | a function that transforms the JSON before deserializing; called recursively for every object |
typeHintPropertyKey | string | property key to recognize as type-hints (default is "__type") |
Converts a JavaScript Object Notation (JSON) string into an instance of the provided class. The provided class must contain a parameterless constructor. Syntax:
TypedJSON.parse(json, Type);
Parameter | Type | Description |
---|---|---|
json | string | a valid JSON string |
Type | newable | a class passed as a value, from which an instance is created using the provided JSON string |
Returns an instance of the provided class.
Use on a class to make it deserializable into the correct type. When an instance of a class with this decorator is serialized with TypedJSON, only properties marked with @JsonMember will be considered.
NOTE: a @JsonObject on a derived type will inherit members and known types from base @JsonObject.
JsonObject has 2 overloads when used as a decorator:
@JsonObject
@JsonObject([options])
Option | Type | Description |
---|---|---|
initializer | function` | custom deserializer function transforming a JSON object to an instace |
knownTypes | Array | array of known types to recognize when encountering type-hints |
name | string | name of the object as it appears in the serialized JSON |
serializer | function | custom serializer function transforming an instace to a JSON object |
Note: 'knownTypes' already includes types of properties marked with @JsonMember.
Use on properties of a @JsonObject class to mark them for serialization.
@JsonMember has 2 overloads when used as a decorator:
@JsonMember
@JsonMember([options])
Option | Type | Default | Description |
---|---|---|---|
*type | Function | - | member type |
**elementType | Function | - | when the property is an array, sets the type of array elements |
emitDefaultValue | boolean | false | when set, a default value is emitted when an unitialized member is serialized |
isRequired | boolean | false | when set, the member must be present when deserializing |
name | string | - | custom member name as it appears in the JSON (default is the property key) |
order | number | - | sets serialization and deserialization order |
refersAbstractType | boolean | false | when set, type-hint is mandatory when deserializing |
- *required unless reflect-metadata is installed
- **required for arrays