W3cubDocs

/Ember.js

DS.JSONSerializer

Extends: DS.Serializer
Defined in: addon/serializers/json.js:17
Module: ember-data

attrs

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

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')
});
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

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.

primaryKey

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

import DS from 'ember-data';

export default DS.JSONSerializer.extend({
  primaryKey: '_id'
});

store public

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) {
    var modelClass = this.store.modelFor(relationshipModelName);
    var relationshipSerializer = this.store.serializerFor(relationshipModelName);
    return relationshipSerializer.normalize(modelClass, relationshipHash);
  }
});

© 2017 Yehuda Katz, Tom Dale and Ember.js contributors
Licensed under the MIT License.
https://emberjs.com/api/ember-data/2.14/classes/DS.JSONSerializer/properties