2

Ive tried the following things and they dont seem to work.

Ember.js Rest Adapter: mapping JSON with no root (.NET Web API)

How can I add a custom root node when serializing an object with JSON.NET?

Im using a web api controller for this.

This is my class and im returning a list of it:

 [JsonObject(Title = "rootNamedObject")]
    public class RootNamedObject
    {
        [JsonProperty("ObjectId")]
        public int Id { get; set; }
        [JsonProperty("Description")]
        public string Description { get; set; }
    }

Right now this is my result:

[
{
    "ObjectId": 1,
    "Description": "Description 1",
},
{
    "ObjectId": 2,
    "Description": "Description 2",
}
]

I need to generate this:

 {
  "rootNamedObject": [
    { 
      "ObjectId": 1, 
      "Description": "Description 1" 
     }

Basically just add the name of the class to the result!

Community
  • 1
  • 1
Salvador Molina
  • 315
  • 4
  • 17
  • It can be done with another hierarchy level: eg. `class Response { RootNamedObject Result {get; set;} }` – user2864740 Aug 13 '14 at 01:22
  • I saw that sometime ago in one of the threads (could not find the link), however i'm trying to avoid that if possible. It seems to work for renaming the properties so i'm guessing there should be a way to actually accomplish this without creating the additional level. – Salvador Molina Aug 13 '14 at 01:33

1 Answers1

0

I was having this issue in ember and found the best solution for me was to build a new serializer and override the normalizePayload method. code below:

export default DS.RESTSerializer.extend({ normalizePayload: function(payload) { var root = "posts"; var output = {}; output[root] = payload; return output; } });

This wraps the initial response and adds the root to it, hope it helps!