How can I make ServiceStack v3 conform to jsonapi.org standards?

心已入冬 提交于 2019-12-11 12:26:24

问题


I am attempting to utilize an Ember.js front-end with a ServiceStack v3 backend. The issue I'm running into is that Ember Data is expecting JSON as per the jsonapi.org standards like so:

[{"clients":
    [
       {"clientID":80,"name":"Test Client 6","acronym":"TCL6","website":"http://www.tcl6.com"},  
       {"clientID":81,"name":"Test Client 7","acronym":"TCL7","website":"http://www.tcl7.com"}
    ]
}] 

But ServiceStack serializes the data into the following:

[{"ClientID":80,"Name":"Test Client 6","Acronym":"TCL6","Website":"http://www.tcl6.com"},
 {"ClientID":81,"Name":"Test Client 7","Acronym":"TCL7","Website":"http://www.tcl7.com"}]

Forcing me to implement custom logic on the front-end to "massage" the data into the appropriate format. I would like to avoid performing this conversion on the client side and implement it directly into the back-end's responses.

Any assistance or direction would be greatly appreciated.

Thanks!


回答1:


You can tell ServiceStack's JSON Serializer to emit camelCase property names with:

JsConfig.EmitCamelCaseNames = true;

You will also want to use a type that matches the shape of the JSON you want to return, e.g:

public class JsonApiClients
{
    public List<Client> Clients { get; set; }
}



回答2:


You can implement a serializer, something like this:

App.ClientSerializer = DS.RESTSerializer.extend({
    normalizePayload: function(payload) {
      return {
        "client": payload
      };
    }
});


来源:https://stackoverflow.com/questions/27966965/how-can-i-make-servicestack-v3-conform-to-jsonapi-org-standards

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!