Skip to content
John White edited this page Apr 30, 2016 · 42 revisions

v0.1.2

TypedJSON

An augmented version of the built-in JSON object to handle parsing and serialization of objects marked with @JsonObject. Functionally a superset, the newly introduced methods (and overloads) are:

TypedJSON.config

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")

TypedJSON.parse

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.

JsonObject

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.

JsonMember

Use on properties of a @JsonObject class to mark which properties are serializable.

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
Clone this wiki locally