Serializer (Runtime API)

Overview - Common API - Client API - Admin API - Runtime API

Serializer

Do not attempt to create an instance of this class.
You must use the getSerializer method instead.

Details

  • Module runtime

See also

Method Summary

Name Returns Description
fromJSON Resource Create a Resource from a JavaScript object representation. The JavaScript object should have been created by calling the toJSON api
toJSON Object Convert a Resource to a JavaScript object suitable for long-term peristent storage.

Method Details

toJSON

Object toJSON( Resource resource, [Object options] )

Convert a Resource to a JavaScript object suitable for long-term peristent storage.

Example

// Get the serializer.
var serializer = getSerializer();
// Serialize a vehicle.
var json = serializer.toJSON(vehicle);

Returns

Object - The JavaScript object that represents the resource

Parameters

Name Type Optional Description
resource Resource false The resource instance to convert to JSON.
options Object true The optional serialization options.

Sub-options

Name Type Optional Description
options.validate boolean true Validate the structure of the resource with its model prior to serialization, true by default.
options.convertResourcesToRelationships boolean true Convert resources that are specified for relationship fields into relationships, false by default.
options.permitResourcesForRelationships boolean true Permit resources in the place of relationships (serializing them as resources), false by default.

Example

// Get the serializer.
var serializer = getSerializer();
// Serialize a vehicle.
var json = serializer.toJSON(vehicle);

fromJSON

Resource fromJSON( Object json, [Object options] )

Create a Resource from a JavaScript object representation. The JavaScript object should have been created by calling the toJSON api

Example

// Get the serializer.
var serializer = getSerializer();
// Serialize a vehicle.
var vehicle = serializer.fromJSON(json);

Returns

Resource - The resource.

Parameters

Name Type Optional Description
json Object false The JavaScript object for the resource.
options Object true The optional serialization options.

Sub-options

Name Type Optional Description
options.acceptResourcesForRelationships boolean true Handle JSON objects in the place of strings for relationships, false by default.

Example

// Get the serializer.
var serializer = getSerializer();
// Serialize a vehicle.
var vehicle = serializer.fromJSON(json);