Class JSONAPISerializer
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.
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:
```js {data-filename=app/models/player.js} import Model, { attr, belongsTo } from '@ember-data/model';
export default class Player extends Model {
attrs public
Inherited from JSONSerializer ../packages/serializer/src/json.js:119
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
```js {data-filename=app/models/person.js} import Model, { attr } from '@ember-data/model';
export default class PersonModel extends Model {
primaryKey public
Inherited from JSONSerializer ../packages/serializer/src/json.js:94
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:140
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);
}
});