Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

System.Text.Json does not seem capable of deserializing nullable custom structs #30949

Closed
andrewjsaid opened this issue Sep 25, 2019 · 3 comments

Comments

@andrewjsaid
Copy link
Contributor

Example below, where we see that my object was serialized unconventionally, then could not be deserialized.

First Problem

public class MyClass
{
    public MyDate? Date { get; set; }
}

public struct MyDate
{
    public int Year { get; set; }
}

// Code
var original = new MyClass { Date = new MyDate { Year = 2 } };

var json = System.Text.Json.JsonSerializer.Serialize(original);
Console.WriteLine($"JSON: {json}");

var after = System.Text.Json.JsonSerializer.Deserialize<MyClass>(json);
Console.WriteLine($"Original: {original.Date == null}");
Console.WriteLine($"After: {after.Date == null}");

Output of the program:

JSON: {"Date":{"HasValue":true,"Value":{"Year":2}}}
Original: False
After: True

Found after migrating similar code to ASP.NET Core 3 when my API was not deserializing my nullable structs.

Second Problem
Furthermore, I believe the following is a related bug:

var json2 = "{\"Date\":{\"Year\":2}}";
var after2 = System.Text.Json.JsonSerializer.Deserialize<MyClass>(json2);
Console.WriteLine($"After2: {after.Date == null}"); // Outputs True

From this example we see that System.Text.Json doesn't even expect the "conventional" way of serializing nullables. Does this mean that it just does not support nullable custom structs at all?

Expected
What I expect and what I call the "conventional" way is to output null for any Nullable which is null, and otherwise just write the JSON of X directly (ignoring HasValue and Value). This is the approach of Newtonsoft JSON.NET, as well as what browsers seem to do.

@andrewjsaid andrewjsaid changed the title System.Text.Json does not seem capable of deserializing custom structs System.Text.Json does not seem capable of deserializing nullable custom structs Sep 25, 2019
@ahsonkhan
Copy link
Contributor

ahsonkhan commented Sep 25, 2019

This is a duplicate of https://github.com/dotnet/corefx/issues/41070.

Does this mean that it just does not support nullable custom structs at all?

Right. We don't have support for nullable custom structs for what shipped in 3.0 and have it on the roadmap for 5.0.

cc @layomia

@msftgits msftgits transferred this issue from dotnet/corefx Feb 1, 2020
@msftgits msftgits added this to the 5.0 milestone Feb 1, 2020
@RomainHautefeuille
Copy link

I can confirm, this is fixed in dotnet 5.0 or System.Text.Json 5.0.0

@andrewjsaid
Copy link
Contributor Author

Thanks @RomainHautefeuille

@ghost ghost locked as resolved and limited conversation to collaborators Dec 20, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants