Class JSONSerializer
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
In EmberData 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, EmberData 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:
import Model, { attr, belongsTo, hasMany } from '@ember-data/model';
export default class UserModel extends Model {
@hasMany('user') friends;
@belongsTo('location') house;
@attr('string') name;
}
{
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
- a
normalizeResponse
for a specific operation is called.
- a
normalizeSingleResponse
|normalizeArrayResponse
- for methods like
createRecord
we expect a single record back, while for methods likefindAll
we expect multiple records back.
- for methods like
normalize
normalizeArrayResponse
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. @mainName @ember-data/serializer/json @tag main
attrs public
Defined in ../packages/serializer/src/json.js:120
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
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;
}
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
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.
primaryKey public
Defined in ../packages/serializer/src/json.js:95
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 JSONSerializer from '@ember-data/serializer/json';
export default class ApplicationSerializer extends JSONSerializer {
primaryKey = '_id'
}
store public
Inherited from Serializer ../packages/serializer/src/index.ts:142
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);
}
});