Skip to content
John White edited this page May 2, 2016 · 42 revisions

v0.1.4

TypedJSON

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:

TypedJSON.config

Configures TypedJSON, provided values are assigned to already existing values. Syntax:

TypedJSON.config(options);
Option Type Default Description
enableTypeHints boolean true when set, enable emitting and recognizing type-hints
maxObjects number - maximum number of objects allowed when deserializing JSON
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 __type property key to recognize as type-hints

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 value, from which an instance is created; must contain a parameterless constructor

Returns an instance of the provided class.

 

@JsonObject

Class decorator that can be used on a class to make JSON 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

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