Serialization of deeply nested object

Hi,

I have a model class Item which looks like this:

export default class ItemModel extends Model {
  @attr name;
  @attr description;
  @attr quantityInfo;
}

QuantityInfo has some nested attributes but since it is embedded object with no id I don’t want to create another model for it (according to Defining Models - Ember Data - Ember Guides ).

I’m connecting with json API which uses snake case so I made ItemSerializer which extends JSONSerializer.extend(EmbeddedRecordsMixin). In this serializer I add method:

export default class ItemModel extends Model {
  @attr name;
  @attr description;
  @attr quantityInfo;

  keyForAttribute(key) {    
    return underscore(key);
  }
}

.

The problem is that nested attributes are not serialized in the underscore way. This is the output json:

{
   "name":"Cola 0.5",
   "description":"asd",
   "quantity_info":{
      "defaultQuantity":0
   }
}

Is there any simple solution to solve this problem?

Hey Karol,

You should checkout GitHub - adopted-ember-addons/ember-data-model-fragments: Ember Data addon to support nested JSON documents allows you to define a schema for your nested object.

Another approach you could take, is to make a custom transform for your type ‘object’ as explained here Defining Models - Models - Ember Guides and transform your keys.

A third approach if you have control of the back end, is to handle transformation of the key space on that side.