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 |
| 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
See also
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. |
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.
See also
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. |
Example
// Get the serializer.
var serializer = getSerializer();
// Serialize a vehicle.
var vehicle = serializer.fromJSON(json);