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

Method Summary

Name Returns Description
constructor void Create a Serializer
fromJSON Resource Create a Resource from a JavaScript Object representation
setDefaultOptions void Set the default options for the serializer
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 Mandatory Description
factory Factory Yes The Factory to use to create instances
modelManager ModelManager Yes The ModelManager to use for validation etc.

setDefaultOptions

setDefaultOptions( Object newdefaultoptions )

Set the default options for the serializer.

Parameters

Name Type Mandatory Description
newDefaultOptions Object Yes The new default options for the serializer.

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 Mandatory Description
resource Resource Yes The 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 (default to true)
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
options.useOriginal boolean Yes shortcut the generation of the JSON structure from the resource and directly return the $original if present from the creation of the resource through the fromJSON method

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 Mandatory Description
jsonObject Object Yes The JavaScript Object for a 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, defaults to false.
options.validate boolean Yes validate the structure of the Resource with its model prior to serialization (default to true)

Inherited methods