使用ServiceStack.Text作为SignalR的JSON序列化器 [英] Using ServiceStack.Text as JSON Serializer for SignalR

查看:44
本文介绍了使用ServiceStack.Text作为SignalR的JSON序列化器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要在我的应用程序层之间实现一致的序列化,我想使用与我在其他地方都使用的相同的SignalR序列化库(ServiceStack.Text).

To have consistent serialization across my application layers, I want to use the same Serialization library (ServiceStack.Text) for SignalR that I use everywhere else.

遵循SignalR的Wiki 条目来替换使用的JSON序列化程序,我创建了以下基本处理程序:

While following SignalR's Wiki entry for replacing the used JSON Serializer, I created this basic handler:

public class SignalrServiceStackJsonSerializer : IJsonSerializer
{
    public void Serialize(object value, TextWriter writer)
    {
        var selfSerializer = value as IJsonWritable;
        if (selfSerializer != null)
            selfSerializer.WriteJson(writer);
        else
            JsonSerializer.SerializeToWriter(value, writer);
    }

    public object Parse(TextReader reader, Type targetType)
    {
        return JsonSerializer.DeserializeFromReader(reader, targetType);
    }
}

集成:

var serializer = new SignalrServiceStackJsonSerializer();
GlobalHost.DependencyResolver.Register(typeof(IJsonSerializer), () => serializer);

不幸的是,将其集成后,SignalR JS客户端的确获得了与默认序列化器不同的软件包.看起来,默认的序列化程序生成(至少对于非用户消息而言)属性限制为1个字符的JSON,将其替换为ServiceStack.Text后不会发生.因此,SignalR尝试访问"I",但接收到"Id".我找不到SignalR服务器端源代码的各个部分.

Unfortunately, after integrating it, the SignalR JS client does get different packages than with the default serializer. As it looks like, the default serializer generates (at least for non-user messages) JSON with properties capped to 1 character, which does not occur after replacing it with ServiceStack.Text. Thus, SignalR tries to access 'I' but it received 'Id'. I was unable to find the respective parts of the SignalR server-side sourcecode.

我做错了什么吗?还是必须创建一个更复杂的包装器才能将ServiceStack.Text用作JSON序列化器?

Did I do something wrong or do I have to create a more complex wrapper to use ServiceStack.Text as a JSON serializer?

推荐答案

我不会费心尝试.我们对JSON.NET有很深的依赖性,我们甚至在下一发行版中都删除了这种可扩展性.抱歉.

I wouldn't bother trying. We have a deep dependency on JSON.NET and we've even removed this extensibility in the next release. Sorry.

这篇关于使用ServiceStack.Text作为SignalR的JSON序列化器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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