Skip to content
module@warp-drive/legacy/serializer @legacy

Class Serializer

Defined in: warp-drive-packages/legacy/src/serializer.ts:144

⚠️ CAUTION you likely want the docs for MinimumSerializerInterface as extending this abstract class is unnecessary.

Serializer is an abstract base class that you may override in your application to customize it for your backend. The minimum set of methods that you should implement is:

  • normalizeResponse()
  • serialize()

And you can optionally override the following methods:

  • normalize()

For an example implementation, see the included JSONSerializer.

Serializer

Extends

  • EmberObject

Constructors

Constructor

ts
new Serializer(owner?): Serializer;

Defined in: node_modules/.pnpm/[email protected]/node_modules/ember-source/types/stable/@ember/object/index.d.ts:28

Parameters

owner?

Owner

Returns

Serializer

Inherited from

ts
EmberObject.constructor

Methods

normalize()

ts
normalize(_typeClass, hash): 
  | EmptyResourceDocument
  | SingleResourceDocument;

Defined in: warp-drive-packages/legacy/src/serializer.ts:268

The normalize method is used to convert a payload received from your external data source into the normalized form store.push() expects. You should override this method, munge the hash and return the normalized payload.

Example:

js
Serializer.extend({
  normalize(modelClass, resourceHash) {
    let data = {
      id:            resourceHash.id,
      type:          modelClass.modelName,
      attributes:    resourceHash
    };
    return { data: data };
  }
})

Parameters

_typeClass

ModelSchema

the model class the hash is being normalized for

hash

Record<string, unknown>

the raw payload hash to normalize

Returns

| EmptyResourceDocument | SingleResourceDocument

the normalized resource document

Properties

store

ts
store: Store;

Defined in: warp-drive-packages/legacy/src/serializer.ts:164

The store property is the application's store that contains all records. It can be used to look up serializers for other model types that may be nested inside the payload response.

Example:

js
Serializer.extend({
  extractRelationship(relationshipModelName, relationshipHash) {
    let modelClass = this.store.modelFor(relationshipModelName);
    let relationshipSerializer = this.store.serializerFor(relationshipModelName);
    return relationshipSerializer.normalize(modelClass, relationshipHash);
  }
});

Released under the MIT License.