Newtonsoft.json: Deserialization of DateTimeOffset value fails depending on system's timezone

Created on 31 May 2018  路  8Comments  路  Source: JamesNK/Newtonsoft.Json

I encountered an issue while materializing a date/time value as DateTimeOffset.
Please see https://stackoverflow.com/questions/50628374 where I initially asked for help.

Source/destination types

public class RootObject
{
    [JsonProperty("revisedDate", NullValueHandling = NullValueHandling.Ignore)]
    public DateTimeOffset? RevisedDate { get; set; }
}

Source/destination JSON

{
  "revisedDate": "0001-01-01T00:00:00"
}

Expected behavior

In the abscence of a timezone, I expected Json.NET to in this case deserialize the value and return a valid DateTimeOffset equal to DateTimeOffset.MinValue.

Actual behavior

An exception was thrown in the DateTimeOffset.Parse method, and Json.NET reported "Could not convert string to DateTimeOffset: 0001-01-01T00:00:00. Path 'resource.revisedDate', line 20, position 44." to the caller.

Steps to reproduce

Please refer to the answer provided by dbc for steps to reproduce the issue:
https://stackoverflow.com/a/50631270/1503584

Most helpful comment

I also met this issue

All 8 comments

I also met this issue

Looking for solution to this issue too.

Having the same issue, the workaround seems really hacky in my opinion :/

Also having this problem with an android app using xamarin.
It was a pain to find out the problem, cause i'm in UTC+0, while the clients are in UTC+n

Thanks a lot for solution!

This issue isn't limited to Json.NET, Microsoft's System.Text.Json does also raise an exception.

This is because DateTimeOffset values are converted to UTC internally, and when the value "0001-01-01T00:00:00" is deserialized to DateTimeOffset, it will use the local timezone. When your timezone offset is positive, it results in the value "0001-01-01T00:00:00+01:00" which is less than DateTimeOffset.MinValue when converted to UTC and thus an invalid DateTimeOffset value.

https://docs.microsoft.com/en-us/dotnet/api/system.datetimeoffset.minvalue:

Any DateTimeOffset value is converted to Coordinated Universal Time (UTC) before the method performs the comparison with MinValue. This means that a DateTimeOffset value whose date and time are close to the minimum range, but whose offset is positive, may throw an exception. For example, the value 1/1/0001 1:00:00 AM +02:00 is out of range because it is one hour earlier than MinValue when it is converted to UTC.

I have found no fix for this other than to write a custom (and complex) JsonConverter to handle the invalid DateTimeOffset values.

I also have this issue. (Thanks for the explanation and work around)

+1
this needs to be solved to avoid ugly hacks

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Phreak87 picture Phreak87  路  11Comments

XeonG picture XeonG  路  12Comments

dmoeff picture dmoeff  路  17Comments

davidfowl picture davidfowl  路  49Comments

manuc66 picture manuc66  路  12Comments