问题
I have a service created with ServiceStack. Recently I updated the ServiceStack libraries and now I am getting JSV responses instead of JSON responses.
The request looks something like:
POST http://localhost/api/rest/poll/create?format=json&PollFormat=1 HTTP/1.1
Host: localhost
Connection: keep-alive
Content-Length: 160
Accept: */*
Origin: http://localhost
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
DNT: 1
Referer: http://localhost
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Cookie:
Question=this+is+a+test&Answers=yes%2Cno&
And the response looks something like:
HTTP/1.1 200 OK
Cache-Control: private
Content-Type: application/json; charset=utf-8
Server: Microsoft-IIS/7.5
X-Powered-By: ServiceStack/3.956 Win32NT/.NET
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Mon, 12 Aug 2013 21:20:33 GMT
Content-Length: 437
{Id:1,Question:this is a test,Answers:[{Id:1,Text:yes,Votes:0},{Id:2,Text:no,Votes:0}],IsOpen:1,TotalVotes:0}}
Note that I've trimmed down the JSV in the response a bit to make it easier to read, and as such, the Content-Length will be incorrect for the example.
From what I understand, the default ContentType for ServiceStack should be JSON
So why I am getting JSV back with a ContentType of application/json?
EDIT:
Here is what my request dto looks like:
[Route("/poll/create", Verbs = "POST")]
public class PollRequest : IReturn<Object>
{
public string Question { get; set; }
public string Answers { get; set; }
public int? PollFormat { get; set; }
}
Here is what my service looks like:
public class PollService : Service
{
public object Post(PollRequest request)
{
//
// do work required to create new poll
//
Poll p = new Poll();
if(request.PollFormat.HasValue)
{
return JsonSerializer.DeserializeFromString<object>(p.JSON);
}
else
{
return PostConvertor.ConvertTo(p);
}
}
}
Here is what my Poll response looks like:
public class Poll
{
public int Id { get; set; }
public string Question { get; set; }
public Collection<Answer> Answers { get; set; }
public int IsOpen { get; set; }
public int TotalVotes { get; set; }
public class Answer
{
public int Id { get; set; }
public string Text { get; set; }
public int Votes { get; set; }
}
}
回答1:
Turns out that we had a parameter being sent to the service that defined how the response was returned. If The parameter was set we would generate a JSON string manually (represented by the p.JSON property in my editied question), and then return a deserialized object from that string like so:
return JsonSerializer.DeserializeFromString<object>(p.JSON)
In previous versions of ServiceStack it appears that the deserialized object would result in a string with the contents being the same as the input JSON (not sure why we do this since it seems like a waste of CPU). Newer versions of ServiceStack appear to deserialize the JSON into a string as well, but the string uses JSV formatting.
I think the reason we are doing this (though I'm not sure) is that we are trying to get a generic object from the JSON so that when we return it it could be converted to JSON or XML depending on what the requester wanted. But the current implementation always returns a JSON formatted string, so if I just replace the
return JsonSerializer.DeserializeFromString<object>(p.JSON)
with
return p.JSON
Then I think I will be keeping the response the same as it is.
来源:https://stackoverflow.com/questions/18197080/servicestack-returning-jsv-instead-of-json