Using ServiceStack.Text as JSON Serializer for SignalR

json, signalr

Solution

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.

Problem

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. 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); } } ``` Integration: ``` var serializer = new SignalrServiceStackJsonSerializer(); GlobalHost.DependencyResolver.Register(typeof(IJsonSerializer), () => serializer); ``` 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. Did I do something wrong or do I have to create a more complex wrapper to use ServiceStack.Text as a JSON serializer?

Original source