Search code examples
serializationember.jsember-datadeserialization

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:

'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:

'tradeId': { key: 'tradeIdentifier' },

Is there any way to define seperate keys for serialization and deserialization in an Ember serializer?


Solution

  • 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;
      }
    });