ServiceStack返回JSV而不是JSON [英] ServiceStack returning JSV instead of JSON

查看:130
本文介绍了ServiceStack返回JSV而不是JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用ServiceStack创建的服务.最近,我更新了ServiceStack库,现在得到的是JSV响应,而不是JSON响应.

I have a service created with ServiceStack. Recently I updated the ServiceStack libraries and now I am getting JSV responses instead of JSON responses.

请求看起来像:

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&

响应看起来像:

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}}

请注意,我在响应中对JSV做了一些调整,以使其更易于阅读,因此,Content-Length对于该示例将是不正确的.

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.

据我了解,ServiceStack的默认ContentType应该为 JSON

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

那么为什么我要用application/json的ContentType找回JSV?

So why I am getting JSV back with a ContentType of application/json?

这是我的请求dto的样子:

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; }
}

这是我的服务的样子:

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; }
    }
}

推荐答案

原来,我们有一个参数发送到服务,该参数定义了如何返回响应.如果设置了参数,我们将手动生成一个JSON字符串(由我编辑过的问题中的p.JSON属性表示),然后从该字符串返回反序列化的对象,如下所示:

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)

在以前的ServiceStack版本中,反序列化的对象似乎会导致字符串,其内容与输入JSON相同(不确定为什么这样做,因为这似乎在浪费CPU).较新版本的ServiceStack似乎也可以将JSON反序列化为字符串,但是该字符串使用JSV格式.

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.

我认为我们这样做的原因(尽管我不确定)是我们试图从JSON获取通用对象,以便在返回时可以将其转换为JSON或XML,具体取决于请求者想要.但是当前的实现始终返回JSON格式的字符串,因此,如果我只替换

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)

使用

return p.JSON

那我想我会保持原样不变.

Then I think I will be keeping the response the same as it is.

这篇关于ServiceStack返回JSV而不是JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆