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

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 overloads are:


TypedJSON.stringify(value [, options]);
  • value: the object to serialize
  • options: serializer settings

Serializes an object. If value is decorated with @JsonObject or at least one of its properties are decorated with @JsonMember, TypedJSON will engage the custom serialization process, regardless of whether options have been specified or not.

Returns a JSON string.


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

Converts a JavaScript Object Notation (JSON) string into an instance of the provided class. The provided class must either contain a parameterless constructor, or ship an initializer function through @JsonObject.

Returns an instance of the provided class, initialized using the supplied JSON.


SerializerSettings

Settings object to configure serialization and deserialization.

  • typeHinting: specifies the syntax of type-hints when polymorphism is involved
  • maxObjects: maximum number of objects allowed when parsing a JSON string (default: no limit)

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 will inherit members and known types from the @JsonObject on the base type.

JsonObject has 3 overloads:

@JsonObject
@JsonObject()
@JsonObject(options)

Options

  • name: name of the object as it appears in the serialized JSON
  • initializer: a custom initializer function
    • required for classes with a parameterized constructor
  • knownTypes: types that are recognized with polymorphism
  • serializer: a custom serializer function

JsonMember

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

JsonMember has 3 overloads:

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