Ember - different keys for serialization and deserialization

Clash Royale CLAN TAG#URR8PPP
Ember - different keys for serialization and deserialization
I have an issue where an property needs to be serialized and deserialized using to different keys.
In the serializer, the property addresses the key trade:
serializer
trade
'tradeId': key: 'trade' ,
This works when addressing the deserialization endpoint.
However, for serialization, the endpoint property is called trade-identifier, requiring the serializer reference to be changed to the following:
trade-identifier
'tradeId': key: 'tradeIdentifier' ,
Is there any way to define seperate keys for serialization and deserialization in an Ember serializer?
1 Answer
1
Thanks to @handlebears for pointing me in the right direction here.
Simply adding a serialize method to the governing serializer file allowed me to reassign the data to the appropriate JSON property:
serialize(snapshot, options)
let json = this._super(...arguments);
json.tradeIdentifier = json.trade;
delete json.trade;
return json;
});
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
The serialize and normalizeResponse hooks should help you out. See guides.emberjs.com/release/models/customizing-serializers/…
– handlebears
Aug 13 at 5:55