问题
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