runtime: System.Text.Json Deserialization - Can't parse DateTime.MinValue if UTC offset is greater than 0

Found this issue while trying to build and run tests for System.Text.Json

Example stacktrace from System.Text.Json.Serialization.Tests.ObjectTests.ReadSimpleTestClassWithSimpleTestStruct

System.Text.Json.JsonException : The JSON value could not be converted to System.DateTime. Path: $.MySimpleTestStruct.MyDateTime | LineNumber: 0 | BytePositionInLine: 912.\r\n---- System.FormatException : The JSON value is not in a supported DateTime format.
at System.Text.Json.ThrowHelper.ReThrowWithPath(ReadStack& readStack, Utf8JsonReader& reader, Exception ex) in C:\Users\mb\src\gh\corefx\src\System.Text.Json\src\System\Text\Json\ThrowHelper.Serialization.cs:line 172
   at System.Text.Json.JsonSerializer.ReadCore(JsonSerializerOptions options, Utf8JsonReader& reader, ReadStack& readStack) in C:\Users\mb\src\gh\corefx\src\System.Text.Json\src\System\Text\Json\Serialization\JsonSerializer.Read.cs:line 124
   at System.Text.Json.JsonSerializer.ReadCore(Type returnType, JsonSerializerOptions options, Utf8JsonReader& reader) in C:\Users\mb\src\gh\corefx\src\System.Text.Json\src\System\Text\Json\Serialization\JsonSerializer.Read.Helpers.cs:line 22
   at System.Text.Json.JsonSerializer.ParseCore(String json, Type returnType, JsonSerializerOptions options) in C:\Users\mb\src\gh\corefx\src\System.Text.Json\src\System\Text\Json\Serialization\JsonSerializer.Read.String.cs:line 74
   at System.Text.Json.JsonSerializer.Deserialize[TValue](String json, JsonSerializerOptions options) in C:\Users\mb\src\gh\corefx\src\System.Text.Json\src\System\Text\Json\Serialization\JsonSerializer.Read.String.cs:line 31
   at System.Text.Json.Serialization.Tests.ObjectTests.ReadSimpleTestClassWithSimpleTestStruct() in C:\Users\mb\src\gh\corefx\src\System.Text.Json\tests\Serialization\Object.ReadTests.cs:line 319
----- Inner Stack Trace -----
   at System.Text.Json.Utf8JsonReader.GetDateTime() in C:\Users\mb\src\gh\corefx\src\System.Text.Json\src\System\Text\Json\Reader\Utf8JsonReader.TryGet.cs:line 392
   at System.Text.Json.Serialization.Converters.JsonConverterDateTime.Read(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options) in C:\Users\mb\src\gh\corefx\src\System.Text.Json\src\System\Text\Json\Serialization\Converters\JsonValueConverterDateTime.cs:line 11
   at System.Text.Json.JsonPropertyInfoNotNullable`4.OnRead(JsonTokenType tokenType, ReadStack& state, Utf8JsonReader& reader) in C:\Users\mb\src\gh\corefx\src\System.Text.Json\src\System\Text\Json\Serialization\JsonPropertyInfoNotNullable.cs:line 25
   at System.Text.Json.JsonPropertyInfo.Read(JsonTokenType tokenType, ReadStack& state, Utf8JsonReader& reader) in C:\Users\mb\src\gh\corefx\src\System.Text.Json\src\System\Text\Json\Serialization\JsonPropertyInfo.cs:line 324
   at System.Text.Json.JsonSerializer.HandleValue(JsonTokenType tokenType, JsonSerializerOptions options, Utf8JsonReader& reader, ReadStack& state) in C:\Users\mb\src\gh\corefx\src\System.Text.Json\src\System\Text\Json\Serialization\JsonSerializer.Read.HandleValue.cs:line 28
   at System.Text.Json.JsonSerializer.ReadCore(JsonSerializerOptions options, Utf8JsonReader& reader, ReadStack& readStack) in C:\Users\mb\src\gh\corefx\src\System.Text.Json\src\System\Text\Json\Serialization\JsonSerializer.Read.cs:line 50

Steps to reproduce:

  1. Set TimeZone to something that has an UTC offset larger than 0
  2. Run following code:
class Class
{
    public DateTime MyDateTime {get; set;}
}
class Program
{
    static void Main(string[] args)
    {
        var json = "{\"MyDateTime\": \"0001-01-01T00:00:00\"}";
        var c = System.Text.Json.JsonSerializer.Deserialize<Class>(json);

        Console.WriteLine(c.MyDateTime);
    }
}
  1. This should give same exception
Unhandled exception. System.Text.Json.JsonException: The JSON value could not be converted to System.DateTime. Path: $.MyDateTime | LineNumber: 0 | BytePositionInLine: 36.
 ---> System.FormatException: The JSON value is not in a supported DateTime format.
   at System.Text.Json.Utf8JsonReader.GetDateTime()
   at System.Text.Json.Serialization.Converters.JsonConverterDateTime.Read(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options)
   at System.Text.Json.JsonPropertyInfoNotNullable`4.OnRead(JsonTokenType tokenType, ReadStack& state, Utf8JsonReader& reader)
   at System.Text.Json.JsonPropertyInfo.Read(JsonTokenType tokenType, ReadStack& state, Utf8JsonReader& reader)
   at System.Text.Json.JsonSerializer.HandleValue(JsonTokenType tokenType, JsonSerializerOptions options, Utf8JsonReader& reader, ReadStack& state)
   at System.Text.Json.JsonSerializer.ReadCore(JsonSerializerOptions options, Utf8JsonReader& reader, ReadStack& readStack)
   --- End of inner exception stack trace ---
   at System.Text.Json.ThrowHelper.ReThrowWithPath(ReadStack& readStack, Utf8JsonReader& reader, Exception ex)
   at System.Text.Json.JsonSerializer.ReadCore(JsonSerializerOptions options, Utf8JsonReader& reader, ReadStack& readStack)
   at System.Text.Json.JsonSerializer.ReadCore(Type returnType, JsonSerializerOptions options, Utf8JsonReader& reader)
   at System.Text.Json.JsonSerializer.ParseCore(String json, Type returnType, JsonSerializerOptions options)
   at System.Text.Json.JsonSerializer.Deserialize[TValue](String json, JsonSerializerOptions options)
   at json_repro.Program.Main(String[] args) in C:\Users\mb\src\tmp\json-repro\Program.cs:line 15

Tried debugging, and the issue is here: https://github.com/dotnet/corefx/blob/da3ba7ebe34771dde769b65dfb7f78fa55a5abb6/src/System.Text.Json/src/System/Text/Json/JsonHelpers.Date.cs#L450-L466

dateTime returned from TryCreateDateTime() previously is var dateTime = new DateTime(0, DateTimeKind.Local), which means that it will be out of range for a DateTimeOffset if UTC offset is larger than 0.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 25 (21 by maintainers)

Most upvoted comments

@markvincze if you don’t want to wait for another preview, you can try out the nightly builds as explained here https://github.com/dotnet/corefx/blob/master/Documentation/project-docs/dogfooding.md