ServiceStack returns JSV instead of JSON

I have a service created with ServiceStack. I recently updated the ServiceStack libraries and now get JSV responses instead of JSON responses.

The query looks something like this:

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 answer looks something like this:

 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 shortened the JSV in the answer a bit to make it easier to read, in which case the Content-Length would be incorrect for example.

From what I understand, by default ContentType for ServiceStack should be JSON

So why am I returning JSV with ContentType of the application / json?

EDIT:

This is what my dto request 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; } } 

This 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's what my poll 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; } } } 
+4
source share
1 answer

It turns out that we sent the parameter to a service that determined how the response was returned. If the parameter was set, we would generate the JSON string manually (represented by the p.JSON property in my asked question), and then return the deserialized object from this string as follows:

 return JsonSerializer.DeserializeFromString<object>(p.JSON) 

In previous versions of ServiceStack, it turned out that the deserialized object will cause the string to contain the same data as the input JSON (I'm not sure why we do this, since this seems like a waste of the processor). Newer versions of ServiceStack also deserialize JSON into a string, but the string uses JSV formatting.

I think the reason we are doing this (although I'm not sure) is because we are trying to get a shared object from JSON so that when it is returned it can be converted to JSON or XML depending on what it required interrogator. But the current implementation always returns a string in JSON format, so if I just replaced

 return JsonSerializer.DeserializeFromString<object>(p.JSON) 

from

 return p.JSON 

Then I think that I will save the answer in the same way as he does.

+1
source

Source: https://habr.com/ru/post/1496678/


All Articles