Class DS.JSONSerializer
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.
By default Ember Data recommends using the JSONApiSerializer.
JSONSerializer
is useful for simpler or legacy backends that may
not support the http://jsonapi.org/ spec.
JSONSerializer
normalizes a JSON payload that looks like:
App.User = DS.Model.extend({
name: DS.attr(),
friends: DS.hasMany('user'),
house: DS.belongsTo('location'),
});
{
id: 1,
name: 'Sebastian',
friends: [3, 4],
links: {
house: '/houses/lefkada'
}
}
to JSONApi format that the Ember Data store expects.
You can customize how JSONSerializer processes it's payload by passing options in the attrs hash or by subclassing the JSONSerializer and overriding hooks:
-To customize how a single record is normalized, use the normalize
hook
-To customize how JSONSerializer normalizes the whole server response, use the
normalizeResponse hook
-To customize how JSONSerializer normalizes a specific response from the server,
use one of the many specific normalizeResponse hooks
-To customize how JSONSerializer normalizes your id, attributes or relationships,
use the extractId, extractAttributes and extractRelationships hooks.
JSONSerializer normalization process follows these steps:
normalizeResponse
- entry method to the SerializernormalizeCreateRecordResponse
- a normalizeResponse for a specific operation is callednormalizeSingleResponse
|normalizeArrayResponse
- for methods likecreateRecord
we expect a single record back, while for methods likefindAll
we expect multiple methods backnormalize
- normalizeArray iterates and calls normalize for each of it's records while normalizeSingle calls it once. This is the method you most likely want to subclassextractId
|extractAttributes
|extractRelationships
- normalize delegates to these methods to turn the record payload into the JSONApi format
attrs
Defined in packages/ember-data/lib/serializers/json-serializer.js:101
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
Defined in packages/ember-data/lib/serializers/json-serializer.js:77
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
Inherited from DS.Serializer packages/ember-data/lib/system/serializer.js:52
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.