• Gotcha
  • .NET Core 3.0
  • .NET
  • C#

Gotcha - System.Text.Json and Dictionary<int, object>

Here is another part to my Gotcha series, this one will be around the new System.Text.Json and serializing to/from a Dictionary.

The SignalR and System.Text.Json Problem

I was personally running into a problem, migrating my application to .NET Core 3.0, I found that they updated the internal SignalR Core JSON parser to use the new System.Text.Json. This has caused a few problems with my migration, because the old implementation that used the Newtonsoft.Json implementation. And since they are not a one to one their have been some silent error, those might of been because I was only at 3.0 updating to 3.1 pushed most of those error to the forefront.

Now the main problem I spent most of my time on was that the internal parsing done in SignalR would just cut the connection, with a very low level error and nothing to go on. And it was really hard to know if it was on the Client or the Server connection, but finding out that the error might of been from the closing of the connection on the client, it pointed me to check the way that the Server produce the JSON.

As it turned out the problem was it trying to turn the loaded/deserialized data from disk and trying to send them to the client that was connected. Since the loading of the data was done using Newtonsoft.Json and was trying to get sent out using System.Text.Json.

The SignalR and System.Text.Json Fix

So to get right down to the fix, it was to remove the usage of Newtonsoft.Json, and just let System.Text.Json handle all the data serialization/de-serialization. This was an easy fix, but the next one I will be going over was a bit more involved to fix.

The Dictionary<int, object> Problem

So this problem was also around the way the new System.Text.Json handles serialization, the gist of it was that anything, other than Dictionary<string, object>, is not supported for serialization. Details on Github

The Dictionary<int, object> Fix

The fix for this was simple, it just took a bit of time to track down why it was not supported. The fix was just to change or remove any non Dictionary<string, object> references I had, simple enough I only had one.


So my major takeaway from all of this, and the migration, was that for speed this might of been good but its probably not a good idea to change a core feature of something for the name of speed. What I am talking about is the changing of the internals of SignalR, I do not believe that the System.Text.Json was ready for a major change right at 3.0. But since it is a 20% jump in speed I jumped right into using that.

Cody's logo image, it is an abstract of a black hole with a white Event Horizon.

Cody Merritt Anhorn

A Engineer with a passion for Platform Architecture and Tool Development.