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 https://guides.emberjs.com/release/models/defining-models/#toc_read-only-attributes ).

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 https://github.com/lytics/ember-data-model-fragments 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 https://guides.emberjs.com/v2.12.0/models/defining-models/#toc_transforms 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.