Class DS.JSONAPISerializer

Ember Data 2.0 Serializer:

In Ember Data a Serializer is used to serialize and deserialize records when they are transferred in and out of an external source. This process involves normalizing property names, transforming attribute values and serializing relationships.

JSONAPISerializer supports the http://jsonapi.org/ spec and is the serializer recommended by Ember Data.

This serializer normalizes a JSON API payload that looks like:

  // models/player.js
  import DS from "ember-data";

  export default DS.Model.extend({
    name: DS.attr(),
    skill: DS.attr(),
    gamesPlayed: DS.attr(),
    club: DS.belongsTo('club')
  });

  // models/club.js
  import DS from "ember-data";

  export default DS.Model.extend({
    name: DS.attr(),
    location: DS.attr(),
    players: DS.hasMany('player')
  });
  {
    "data": [
      {
        "attributes": {
          "name": "Benfica",
          "location": "Portugal"
        },
        "id": "1",
        "relationships": {
          "players": {
            "data": [
              {
                "id": "3",
                "type": "players"
              }
            ]
          }
        },
        "type": "clubs"
      }
    ],
    "included": [
      {
        "attributes": {
          "name": "Eusebio Silva Ferreira",
          "skill": "Rocket shot",
          "games-played": 431
        },
        "id": "3",
        "relationships": {
          "club": {
            "data": {
              "id": "1",
              "type": "clubs"
            }
          }
        },
        "type": "players"
      }
    ]
  }

to the format that the Ember Data store expects.

Show:

Module: ember-data

The attrs object can be used to declare a simple mapping between property names on DS.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 DS from 'ember-data';

export default DS.Model.extend({
  firstName: DS.attr('string'),
  lastName: DS.attr('string'),
  occupation: DS.attr('string'),
  admin: DS.attr('boolean')
});
app/serializers/person.js
import DS from 'ember-data';

export default DS.JSONSerializer.extend({
  attrs: {
    admin: 'is_admin',
    occupation: { key: 'career' }
  }
});

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

Example

app/serializers/person.js
import DS from 'ember-data';

export default DS.JSONSerializer.extend({
  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.

Module: ember-data

This is only to be used temporarily during the transition from the old serializer API to the new one.

JSONAPISerializer only supports the new Serializer API.

Module: ember-data

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 DS from 'ember-data';

export default DS.JSONSerializer.extend({
  primaryKey: '_id'
});
Module: ember-data

The store property is the application's store that contains all records. It's injected as a service. It can be used to push records from a non flat data structure server response.