-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Support nullable structs in JsonSerializer #30843
Copy link
Copy link
Closed
Labels
Milestone
Description
Given
public class ClassWithNullablePerson
{
public Person? Person { get; set; } = new Person();
}
public struct Person
{
public string FirstName { get; set; }
public int? Age { get; set; }
public DateTime? Birthday { get; set; }
}Serialization
string serialized = JsonSerializer.Serialize(new ClassWithNullablePerson());
Console.WriteLine(serialized)
// {"Person":{"HasValue":true,"Value":{"FirstName":null,"Age":null,"Birthday":null}}} (Actual)
// {"Person":{"FirstName":null,"Age":null,"Birthday":null}} (Expected)Deserialization (Debug)
var person = JsonSerializer.Deserialize<ClassWithNullablePerson>(@"{""Person"":{""FirstName"":""Layo"",""Age"":22,""Birthday"":""1995-04-16""}}");Process terminated. Assertion failed.
at System.Text.Json.JsonSerializer.HandlePropertyName(JsonSerializerOptions options, Utf8JsonReader& reader, ReadStack& state) in D:\repos\corefx\src\System.Text.Json\src\System\Text\Json
\Serialization\JsonSerializer.Read.HandlePropertyName.cs:line 23
at System.Text.Json.JsonSerializer.ReadCore(JsonSerializerOptions options, Utf8JsonReader& reader, ReadStack& readStack) in D:\repos\corefx\src\System.Text.Json\src\System\Text\Json\Seria
lization\JsonSerializer.Read.cs:line 54
at System.Text.Json.JsonSerializer.ReadCore(Type returnType, JsonSerializerOptions options, Utf8JsonReader& reader) in D:\repos\corefx\src\System.Text.Json\src\System\Text\Json\Serializat
ion\JsonSerializer.Read.Helpers.cs:line 17
In Release, deserialization returns an instance with null members for the nested Person struct.
Reactions are currently unavailable