Class RESTSerializer

public

⚠️ This is LEGACY documentation for a feature that is no longer encouraged to be used. If starting a new app or thinking of implementing a new adapter, consider writing a Handler instead to be used with the RequestManager

Normally, applications will use the RESTSerializer by implementing the normalize method.

This allows you to do whatever kind of munging you need and is especially useful if your server is inconsistent and you need to do munging differently for many different kinds of responses.

See the normalize documentation for more information.

## Across the Board Normalization

There are also a number of hooks that you might find useful to define across-the-board rules for your payload. These rules will be useful if your server is consistent, or if you're building an adapter for an infrastructure service, like Firebase, and want to encode service conventions.

For example, if all of your keys are underscored and all-caps, but otherwise consistent with the names you use in your models, you can implement across-the-board rules for how to convert an attribute name in your model to a key in your JSON.

app/serializers/application.js
 import RESTSerializer from '@ember-data/serializer/rest';
 import { underscore } from '<app-name>/utils/string-utils';

 export default class ApplicationSerializer extends RESTSerializer {
   keyForAttribute(attr, method) {
     return underscore(attr).toUpperCase();
   }
 }

You can also implement keyForRelationship, which takes the name of the relationship as the first parameter, the kind of relationship (hasMany or belongsTo) as the second parameter, and the method (serialize or deserialize) as the third parameter. @mainName @ember-data/serializer/rest @tag main

Show:

The attrs object can be used to declare a simple mapping between property names on Model records and payload keys in the serialized JSON object representing the record. An object with the property key can also be used to designate the attribute's key on the response payload.

Example

app/models/person.js
import Model, { attr } from '@ember-data/model';

export default class PersonModel extends Model {
  @attr('string') firstName;
  @attr('string') lastName;
  @attr('string') occupation;
  @attr('boolean') admin;
}
app/serializers/person.js
import JSONSerializer from '@ember-data/serializer/json';

export default class PersonSerializer extends JSONSerializer {
  attrs = {
    admin: 'is_admin',
    occupation: { key: 'career' }
  }
}

You can also remove attributes and relationships by setting the serialize key to false in your mapping object.

Example

app/serializers/person.js
import JSONSerializer from '@ember-data/serializer/json';

export default class PostSerializer extends JSONSerializer {
  attrs = {
    admin: { serialize: false },
    occupation: { key: 'career' }
  }
}

When serialized:

{
  "firstName": "Harry",
  "lastName": "Houdini",
  "career": "magician"
}

Note that the admin is now not included in the payload.

Setting serialize to true enforces serialization for hasMany relationships even if it's neither a many-to-many nor many-to-none relationship.

The primaryKey is used when serializing and deserializing data. Ember Data always uses the id property to store the id of the record. The external source may not always follow this convention. In these cases it is useful to override the primaryKey property to match the primaryKey of your external store.

Example

app/serializers/application.js
import JSONSerializer from '@ember-data/serializer/json';

export default class ApplicationSerializer extends JSONSerializer {
  primaryKey = '_id'
}

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:

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