1

My server needs json in the below format when sending a PUT request. My server is a rest api designed using struts2 rest plugin.

{
id: "5",
empId: "5",
firstName: "oki", 
lastName: "iko", 
edQual: "phd"
}
but the RESTAdapter serializes it to
[
employees:
 {
  id: "5", 
  empId: "5", 
  firstName: "oki", 
  lastName: "iko",
  edQual: "phd"
 }
]

I tried ignoring properties in backend but that just ignored the whole json and submitted null values to the sql-server. I need to override or customize the Serialization of ember.js but I dont know how.

Lonewolf
  • 113
  • 5

1 Answers1

2

This is one of the responsibilities for the Serializer provided by Ember Data. I guess you are using RestSerializer, which is normally used together with RestAdapter aren't you? In that case you should customize the serializeIntoHash() method. Just not using a namespace at all should be accomplished by:

import RESTSerializer from '@ember-data/serializer/rest';

export default RESTSerializer.extend({
  serializeIntoHash(data, type, record, options) {
    data = this.serialize(record, options);
  }
});

To not loose any data that is already present on hash you could use Object.assign(). This is also what's done in JSONSerializer:

import { assign, merge } from '@ember/polyfills';

const emberAssign = assign || merge;

export default RESTSerializer.extend({
  serializeIntoHash(hash, typeClass, snapshot, options) {
    emberAssign(hash, this.serialize(snapshot, options));
  },
});

The assign || merge is only needed to support very old ember versions. You could simplify to:

import { assign } from '@ember/polyfills';

export default RESTSerializer.extend({
  serializeIntoHash(hash, typeClass, snapshot, options) {
    assign(hash, this.serialize(snapshot, options));
  },
});

You don't need to use the polyfill for assign if you don't support IE 11. In that case it would be:

export default RESTSerializer.extend({
  serializeIntoHash(hash, typeClass, snapshot, options) {
    Object.assign(hash, this.serialize(snapshot, options));
  },
});

And with native class it looks like:

export default class ApplicationSerializer extends RESTSerializer {
  serializeIntoHash(hash, typeClass, snapshot, options) {
    Object.assign(hash, this.serialize(snapshot, options));
  }
}
jelhan
  • 6,149
  • 1
  • 19
  • 35