Update Aug 29th 2019: Hyperledger Composer has been deprecated. Please see the README for more information.

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

Method Summary

Name Returns Description
fromJSON Resource Create a Resource from a JavaScript object representation
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.

Returns

Object - The JavaScript object that represents the resource

Parameters

Name Type Mandatory Description
resource Resource Yes The resource instance to convert to JSON.
options Object Yes The optional serialization options.

Sub-options

Name Type Mandatory Description
options.validate boolean Yes Validate the structure of the resource with its model prior to serialization, true by default.
options.convertResourcesToRelationships boolean Yes Convert resources that are specified for relationship fields into relationships, false by default.
options.permitResourcesForRelationships boolean Yes Permit resources in the place of relationships (serializing them as resources), false by default.
options.deduplicateResources boolean Yes Generate $id for resources and if a resources appears multiple times in the object graph only the first instance is serialized in full, subsequent instances are replaced with a reference to the $id

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

Returns

Resource - The resource.

Parameters

Name Type Mandatory Description
json Object Yes The JavaScript object for the resource.
options Object Yes The optional serialization options.

Sub-options

Name Type Mandatory Description
options.acceptResourcesForRelationships boolean Yes Handle JSON objects in the place of strings for relationships, false by default.
options.validate boolean Yes Validate the structure of the resource with its model prior to serialization, true by default.

Example

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

Inherited methods