You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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}");
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.
The text was updated successfully, but these errors were encountered:
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
Example below, where we see that my object was serialized unconventionally, then could not be deserialized.
First Problem
Output of the program:
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:
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.
The text was updated successfully, but these errors were encountered: