DS.JSONSerializer Class
Extends: DS.Serializer
Defined in: addon/serializers/json.js:19
Module: ember-data
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 uses and recommends the JSONAPISerializer
.
JSONSerializer
is useful for simpler or legacy backends that may not support the http://jsonapi.org/ spec.
For example, given the following User
model and JSON payload:
app/models/user.js
import DS from 'ember-data'; export default DS.Model.extend({ friends: DS.hasMany('user'), house: DS.belongsTo('location'), name: DS.attr('string') });
{ id: 1, name: 'Sebastian', friends: [3, 4], links: { house: '/houses/lefkada' } }
JSONSerializer
will normalize the JSON payload to the JSON API format that the Ember Data store expects.
You can customize how JSONSerializer processes its 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 thenormalizeResponse
hook. - To customize how
JSONSerializer
normalizes a specific response from the server, use one of the many specificnormalizeResponse
hooks. - To customize how
JSONSerializer
normalizes your id, attributes or relationships, use theextractId
,extractAttributes
andextractRelationships
hooks.
The JSONSerializer
normalization process follows these steps:
-
normalizeResponse
- entry method to the serializer. -
normalizeCreateRecordResponse
- anormalizeResponse
for a specific operation is called. -
normalizeSingleResponse
|normalizeArrayResponse
- for methods likecreateRecord
we expect a single record back, while for methods likefindAll
we expect multiple methods back. -
normalize
-normalizeArray
iterates and callsnormalize
for each of its records whilenormalizeSingle
calls it once. This is the method you most likely want to subclass. -
extractId
|extractAttributes
|extractRelationships
-normalize
delegates to these methods to turn the record payload into the JSON API format.
Please login to continue.