Skip to content

JsonSerializer.Deserialize failing for some types in net9.0 that worked in net8.0. #113268

@chuckries

Description

@chuckries

Description

The following snippet succeeds when compiled and run against net8.0, but throws an exception when compiled and run against net9.0

using System.Text.Json;
using System.Text.Json.Nodes;

string json = """
    { "names": ["Chuck"] }
    """;

var dict = JsonSerializer.Deserialize<Dictionary<string, JsonValue>>(json);

Reproduction Steps

  1. create a new net8.0 console app and add the following code:
using System.Text.Json;
using System.Text.Json.Nodes;

string json = """
    { "names": ["Chuck"] }
    """;

var dict = JsonSerializer.Deserialize<Dictionary<string, JsonValue>>(json);
  1. run the app
  2. change net8.0 to net9.0
  3. run the app

Expected behavior

The serializaiton succeeds

Actual behavior

in net9.0, the deserialization throws an exception:
System.InvalidOperationException: 'The element cannot be an object or array.'

Regression?

Yes, this works in net8 but not in net9

Known Workarounds

Deserializing to Dictionary<string, JsonNode> appears to work.

Configuration

dotnet --list-sdks
8.0.406 [C:\Program Files\dotnet\sdk]
9.0.200 [C:\Program Files\dotnet\sdk]
9.0.300-preview.0.25127.19 [C:\Program Files\dotnet\sdk]

dotnet --list-runtimes
Microsoft.AspNetCore.App 6.0.36 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 8.0.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 9.0.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 6.0.36 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 8.0.13 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 9.0.2 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 6.0.36 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 8.0.13 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 9.0.2 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Other information

No response

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions