1

In .Net 6, when trying to use SignalR with JsonProtocol (System.Text.Json), my client program throws the following exception

System.IO.InvalidDataException: Error reading JSON.
   ---> System.Text.Json.JsonException: '{' is invalid after a value. Expected either ',', '}', or ']'. Path: $ | LineNumber: 0 | BytePositionInLine: 4090

Code on server side:

services.AddSignalR()
    .AddJsonProtocol();

Code on client side:

conn = new HubConnectionBuilder()
        .WithUrl($"{_serverBaseAddress}mainHub", o =>
            {
                o.AccessTokenProvider = InitializeOrRefreshAccessTokenIfNeeded;
                o.Transports = HttpTransportType.WebSockets;
            }
        )
        .ConfigureLogging(logging =>
        {
            logging.AddConsole();
        })
        .AddJsonProtocol()
        .Build()

Note: everything works fine when using NewtonsoftJsonProtocol:

Code on server:

services.AddSignalR()
        .AddNewtonsoftJsonProtocol(opts =>
        {
            opts.PayloadSerializerSettings.TypeNameHandling = TypeNameHandling.All;
        });

Code on client:

conn = new HubConnectionBuilder()
        .WithUrl($"{_serverBaseAddress}mainHub", o =>
            {
                o.AccessTokenProvider = InitializeOrRefreshAccessTokenIfNeeded;
                o.Transports = HttpTransportType.WebSockets;
            }
        )
        .ConfigureLogging(logging =>
        {
            logging.AddConsole();
        })
        .AddNewtonsoftJsonProtocol(
        opts =>
            opts.PayloadSerializerSettings.TypeNameHandling = TypeNameHandling.All
        )
        .Build();

There is a difference, with NewtonsoftJson I specify TypeNameHandling.All but I do not know any equivalent for System.Text.Json.

Why not just keep Newtonsoft.Json? I would like to use source generators and specify a SerializerContext like below:

.AddJsonProtocol(options =>
{
    options.PayloadSerializerOptions.AddContext<MyJsonSerializerContext>();
});

which I believe I cannot do with NewtonsoftJson

Sylvain Gantois
  • 779
  • 1
  • 12
  • 28
  • 2
    Need more detail. Is this error on the client or server? – davidfowl Sep 04 '22 at 05:05
  • Thank you, that was on the client but after seeing your comment I checked on the server and saw a cyclic exception. I did more digging and was able to find useful information on the migration to System.Text.Json (see answer) – Sylvain Gantois Sep 04 '22 at 22:29

1 Answers1

1

As it turned out, switching from Newtonsoft.Json to System.Text.Json is not as simple as just switching protocols in the configuration.

There is this page from Microsoft that describes the key differences between the 2 and helps with the migration: https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-migrate-from-newtonsoft-how-to?pivots=dotnet-6-0

As they explain, in some scenarios it will not be possible to move to System.Text.Json.

So in my case, I was looking at errors on the client but actually, the server was throwing a cycling error like this one: How to JSON serialize without cyclic error Replacing [IgnoreDataMember] by [JsonIgnore] fixes the issue.

Also TypeNameHandling.All which I was using in Newtonsoft.Json is not supported in System.Text.Json. I was using this setting because of polymorphic deserialization, but in this case, they propose to write custom converters as a workaround: https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-converters-how-to?pivots=dotnet-6-0#support-polymorphic-deserialization

Sylvain Gantois
  • 779
  • 1
  • 12
  • 28
  • You never posted your DTOs, so this answer won't help others. This isn't the error thrown when there are circular references. If even the creator of SignalR can't understand what you posted ... (yes that's him) – Panagiotis Kanavos Sep 05 '22 at 08:14
  • I think the point here is that to switch from Newtonsoft.Json to System.Text.Json, its not just about changing the protocol in the configuration as I did but there is a migration process to follow (see link in the answer), that will definitely be helpful to people wanting to switch. – Sylvain Gantois Sep 05 '22 at 08:40