Skip to content

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

Closed

Description

@bjorkstromm

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.

Activity

  1. bjorkstromm commented on Jun 30, 2019

    @bjorkstromm
    MemberAuthor

    Biggest issue for me is that I'm unable to build and test System.Text.Json without adjusting my timezone. 😢

  2. bjorkstromm commented on Jul 1, 2019

    @bjorkstromm
    MemberAuthor

    Related to (or possible duplicate of) #29964

  3. bjorkstromm commented on Jul 1, 2019

    @bjorkstromm
    MemberAuthor

    Probably same issue with DateTime.MaxValue when in time zone with UTC offset smaller than 0 (haven’t tested). DateTime.MinValue is however more problematic since it’s the default value of DateTime.

  4. steveharter commented on Jul 1, 2019

    @steveharter
    Contributor

    @layomia can you take a look please? This may have been fixed.

  5. steveharter commented on Jul 1, 2019

    @steveharter
    Contributor

    cc @JeremyKuhne if you still looking at DateTime support.

  6. JeremyKuhne commented on Jul 1, 2019

    @JeremyKuhne
    Member

    @layomia I'll look at this since I'm in this code at the moment.

  7. JeremyKuhne commented on Jul 4, 2019

    @JeremyKuhne
    Member

    Should be ok with dotnet/corefx#39187.

  8. bjorkstromm commented on Jul 8, 2019

    @bjorkstromm
    MemberAuthor

    @JeremyKuhne I tried running tests from your PR branch (#39187), but I am still seeing the same issues.

  9. JeremyKuhne commented on Jul 8, 2019

    @JeremyKuhne
    Member

    I am still seeing the same issues.

    Interesting. I wonder what the difference is? I set myself to UTC + 1 and didn't have a problem.

  10. bjorkstromm commented on Jul 8, 2019

    @bjorkstromm
    MemberAuthor

    Weird, well, here's my settings:
    image

    image

    Let me know if you'd like me to debug anything.

  11. JeremyKuhne commented on Jul 8, 2019

    @JeremyKuhne
    Member

    @layomia can you try to repro this as well once dotnet/corefx#39187 goes in?

  12. ericstj commented on Jul 10, 2019

    @ericstj
    Member

    I can help here. I repro'ed the original issue. Building your change and testing now.

  13. 4 remaining items

  14. tarekgh commented on Jul 10, 2019

    @tarekgh
    Member
  15. ericstj commented on Jul 11, 2019

    @ericstj
    Member

    Yeah, it's a "Try" method, so when this fails and returns false the outer call ends up throwing.

    This is happening because we're trying to parse as DateTime (which can accept this value) but then we're flowing it through a DateTimeOffset (which cannot accept this value since UTC representation underflows year 1) here only to convert back to DateTime.
    https://github.com/dotnet/corefx/blob/1247fa7a193e52bc89bc49c4ba6e44739df2dcee/src/System.Text.Json/src/System/Text/Json/JsonHelpers.Date.cs#L18-L41

    The fix, as Jeremy mentioned, would be to avoid this smuggling, and instead either go straight to DateTime or DateTimeOffset, depending on what the target type is.

  16. tarekgh commented on Jul 11, 2019

    @tarekgh
    Member

    The fix, as Jeremy mentioned, would be to avoid this smuggling, and instead either go straight to DateTime or DateTimeOffset, depending on what the target type is.

    That make sense.

  17. layomia commented on Jul 11, 2019

    @layomia
    Contributor

    I'm taking a look at this now.

  18. markvincze commented on Aug 1, 2019

    @markvincze

    Hey folks,

    I checked this issue and #29964, but it's unclear to me if this is fixed already or not.

    If I run this code:

        public class MyType
        {
            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<MyType>(json);
    
                Console.WriteLine(c.MyDateTime);
            }
        }

    I'm still getting the The JSON value could not be converted to System.DateTime exception.

    I'm using version 4.6.0-preview7.19362.9 of System.Text.Json, which was released 9 days ago, so dotnet/corefx#39547 should've been included in it. (Update: okay, now I see that PR only changes the tests)

    Is this a bug which is still not fixed, or am I trying to do something invalid?

  19. karelz commented on Aug 3, 2019

    @karelz
    Member

    It was fixed in master (3.0 at that moment) in PR dotnet/corefx#39541.

    @markvincze if you see a problem on latest master or release/3.0 builds, please file a new bug.

  20. markvincze commented on Aug 3, 2019

    @markvincze

    Hi @karelz,

    Thanks for the info!
    Do you happen to know if there is an easy way for me to check if a specific Nuget release (for example 4.6.0-preview7.19362.9) already contains a certain PR or not? (For example can I check the commit based on which that package was released?

  21. bjorkstromm commented on Aug 3, 2019

    @bjorkstromm
    MemberAuthor

    @markvincze the releases are tagged. By looking at the src in preview7 tag I can see that this fix isn’t available in preview7. See https://github.com/dotnet/corefx/commits/v3.0.0-preview7.19362.9/src/System.Text.Json/src

  22. markvincze commented on Aug 3, 2019

    @markvincze

    @mholo65 clear, thanks! I'll look out for the next preview and will test it there.

  23. bjorkstromm commented on Aug 3, 2019

    @bjorkstromm
    MemberAuthor
  24. markvincze commented on Aug 15, 2019

    @markvincze

    @mholo65 @karelz,
    Thanks for help, I verified that the issue is fixed in 4.6.0-preview8.19405.3 👍

  25. transferred this issue fromdotnet/corefxon Feb 1, 2020
  26. added this to the 3.0 milestone on Feb 1, 2020
  27. ghost locked as resolved and limited conversation to collaborators on Dec 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions