Class: Serializer

composer-common. Serializer

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


new Serializer(factory, modelManager)

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

Parameters:
Name Type Description
factory Factory

The Factory to use to create instances

modelManager ModelManager

The ModelManager to use for validation etc.

Source:

Methods


fromJSON(jsonObject, 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.

Parameters:
Name Type Description
jsonObject Object

The JavaScript Object for a Resource

options Object

the optional serialization options

Properties
Name Type Description
acceptResourcesForRelationships boolean

handle JSON objects
in the place of strings for relationships, defaults to false.

Source:
Returns:

The new populated resource

Type
Resource

toJSON(resource, options)


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

Parameters:
Name Type Description
resource Resource

The instance to convert to JSON

options Object

the optional serialization options.

Properties
Name Type Description
validate boolean

validate the structure of the Resource
with its model prior to serialization (default to true)

convertResourcesToRelationships boolean

Convert resources that
are specified for relationship fields into relationships, false by default.

permitResourcesForRelationships boolean

Permit resources in the
place of relationships (serializing them as resources), false by default.

Source:
Throws:
  • throws an exception if resource is not an instance of
    Resource or fails validation.
Type
Error
Returns:
  • The Javascript Object that represents the resource
Type
Object