Serializer (Common API)

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

Serializer

Serialize Resources instances to/from various formats for long-term storage (e.g. on the blockchain).

Details

  • Module common

See also

Method Summary

Name Returns Description
constructor void Create a Serializer. Note: Only to be called by framework code. Applications should retrieve instances from BusinessNetworkDefinition

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

new Serializer()

Create a Serializer. Note: Only to be called by framework code. Applications should retrieve instances from BusinessNetworkDefinition

Parameters

Name Type Optional Description
factory Factory false The Factory to use to create instances
modelManager ModelManager false The ModelManager to use for validation etc.

toJSON

Object toJSON( Resource resource, Object options )

Convert a [Resource](common-resource) to a JavaScript object suitable for long-term peristent storage.

Returns

Object - The Javascript Object that represents the resource

Parameters

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

Sub-options

Name Type Optional Description
options.validate boolean false validate the structure of the Resource with its model prior to serialization (default to true)
options.convertResourcesToRelationships boolean false Convert resources that are specified for relationship fields into relationships, false by default.
options.permitResourcesForRelationships boolean false Permit resources in the place of relationships (serializing them as resources), false by default.
options.deduplicateResources boolean false 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

fromJSON

Resource fromJSON( Object jsonobject, Object options )

Create a Resource from a JavaScript Object representation. The JavaScript Object should have been created by calling the toJSON API. The Resource is populated based on the JavaScript object.

Returns

Resource - The new populated resource

Parameters

Name Type Optional Description
jsonObject Object false The JavaScript Object for a Resource
options Object false the optional serialization options

Sub-options

Name Type Optional Description
options.acceptResourcesForRelationships boolean false handle JSON objects in the place of strings for relationships, defaults to false.
options.validate boolean false validate the structure of the Resource with its model prior to serialization (default to true)