Skip to content
John White edited this page Apr 29, 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(options);

Configures TypedJSON, provided values are assigned to already existing values. The options object accepts the following keys:

  • enableTypeHints: when set, enable emitting and recognizing type-hints (default is true)
  • maxObjects: maximum number of objects allowed when deserializing JSON (default: no limit)
  • replacer: a function that transforms the JSON after serializing; called recursively for every object
  • reviver: a function that transforms the JSON before deserializing; called recursively for every object
  • typeHintPropertyKey: property key to recognize as type-hints (default is "__type")

TypedJSON.parse(json, Type);
  • json: a valid JSON string
  • Type: a class from which an instance is created using the provided JSON string

Converts a JavaScript Object Notation (JSON) string into an instance of the provided class. The provided class must contain a parameterless constructor.

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    when inheritance is involved, 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])

Options

  • name: name of the object as it appears in the serialized JSON
  • initializer: custom deserializer function transforming a JSON object to an instace
  • knownTypes: array of known types to recognize when encountering type-hints
    • type and elementType (if any) of each JsonMember property are automatically added
  • serializer: custom serializer function transforming an instace to a JSON object

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

Options

  • name: member name as it appears in the serialized JSON (default: value of property key)
  • type: member type
  • elementType: when the json member is an array, sets the type of array elements
    • required for arrays
  • isRequired: when set, the member must be present when deserializing (default: false)
  • order: sets serialization and deserialization order (ordered members come first, then the rest in alphabetical order)
  • emitDefaultValue: when set, a default value is emitted when an unitialized member is serialized (default: false)
  • refersAbstractType: when set, type-hint is mandatory when deserializing; set for properties with interface or abstract types/element-types (default: false)
Clone this wiki locally