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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With