When trying to populate a nullable DateTime property from a JSON string with a null value an exception is thrown when employing JsonMicrosoftDateTimeConverter
.
C#
[JsonConverter(typeof(JsonMicrosoftDateTimeConverter))]
public DateTime? PaymentDueDate { get; set; }
JSON:
...
"PaymentDueDate": null,
...
Exception:
Message:
System.Text.Json.JsonException : The JSON value could not be converted to System.Nullable 1[System.DateTime]. Path: $.PaymentDueDate | LineNumber: 75 | BytePositionInLine: 28.
Stack Trace:
JsonDateTimeConverter 1.ReadDateTime(Utf8JsonReader& reader)
JsonNullableDateTimeConverter.Read(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options)
JsonConverter 1.TryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value)
JsonPropertyInfo 1.ReadJsonAndSetMember(Object obj, ReadStack& state, Utf8JsonReader& reader)
ObjectDefaultConverter 1.ReadPropertyValue(Object obj, ReadStack& state, Utf8JsonReader& reader, JsonPropertyInfo jsonPropertyInfo, Boolean useExtensionProperty)
ObjectDefaultConverter 1.OnTryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value)
JsonConverter 1.TryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value)
JsonConverter 1.ReadCore(Utf8JsonReader& reader, JsonSerializerOptions options, ReadStack& state)
JsonSerializer.ReadCore[TValue](JsonConverter jsonConverter, Utf8JsonReader& reader, JsonSerializerOptions options, ReadStack& state)
JsonSerializer.ReadCore[TValue](Utf8JsonReader& reader, Type returnType, JsonSerializerOptions options)
JsonSerializer.Deserialize[TValue](String json, Type returnType, JsonSerializerOptions options)
JsonSerializer.Deserialize[TValue](String json, JsonSerializerOptions options)
JsonTests.AdHocDeserialiseTest() line 128
--- End of stack trace from previous location where exception was thrown ---