-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Closed as not planned
Labels
area-System.Text.JsonenhancementProduct code improvement that does NOT require public API changes/additionsProduct code improvement that does NOT require public API changes/additions
Milestone
Description
When deserializing "null" for a non-nullable value type System.Text.Json throws an Exception. However, for reference types this is not the case even if the project has the "Nullable" feature enabled.
Expectation: When nullable is enabled for the project deserializing null into a non-nullable type should fail for both value and reference types.
Question: This could be worked around by writing a custom Converter that honors nullable during deserialization. Are there plans to expose a setting to control the deserialization behavior for non-nullable reference types?
Repro: Deserializing DateTime as null fails (by design per this issue 40922). Deserializing List as null succeeds.
class Program
{
public class MyDataObject
{
public DateTime Dt { get; set; }
public List<int> MyNonNullableList { get; set; } = new List<int>();
}
static void Main(string[] args)
{
string input1 = "{\"Dt\":null, \"MyNonNullableList\":[42,32]}";
string invalidInput = "{\"Dt\":\"0001-01-01T00:00:00\",\"MyNonNullableList\":null}";
// Throws System.Text.Json.JsonException: 'The JSON value could not be converted to System.DateTime. Path: $.Dt | LineNumber: 0 | BytePositionInLine: 10.'
// var validConverted = JsonSerializer.Deserialize<MyDataObject>(input1);
// Does not throw Exception, but MyNonNullableList is null.
var invalidConverted = JsonSerializer.Deserialize<MyDataObject>(invalidInput);
Console.ReadKey();
}
}markm77, grovesNL, wanton7, Dreamescaper, Trolldemorted and 73 more
Metadata
Metadata
Assignees
Labels
area-System.Text.JsonenhancementProduct code improvement that does NOT require public API changes/additionsProduct code improvement that does NOT require public API changes/additions
Type
Projects
Status
Misc