I've been migrating from 2.2 to 3.0 and so far everything has been pretty smooth however when replacing Newtonsoft with the new System.Text.Json I run into the following problem.
When trying to deserialize a DateTime min value with a UTC offset it throws the following exception
Unhandled exception. System.Text.Json.JsonException: The JSON value could not be converted to System.DateTime. Path: $.DateCreated | LineNumber: 2 | BytePositionInLine: 43
Example:
using System;
using Newtonsoft.Json;
class Program
{
static void Main(string[] args)
{
var json = "{\"MyDateTime\": \"0001-01-01T00:00:00+01:00\"}";
var newtonDeserialized = JsonConvert.DeserializeObject<Class>(json);
//Bad things happen here.
var systemDeserialized = System.Text.Json.JsonSerializer.Deserialize<Class>(json);
Console.WriteLine(newtonDeserialized.MyDateTime);
Console.WriteLine(systemDeserialized.MyDateTime);
}
}
class Class
{
public DateTime MyDateTime {get; set;}
}
Online example can be found here Fiddle.
I guess I could write my own DateTime serializer but there must be an easier/cleaner way to accomplish what Newtonsoft did without any configuring.
This is happening on 3.0.100
, 3.1.500
, 5.0.100
and 6.0.100