Deserialization of DateTimeOffset value fails depending on system's timezone
See original GitHub issueI 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
Issue Analytics
- State:
- Created 5 years ago
- Reactions:24
- Comments:13
Top Results From Across the Web
[Solved]-Json.NET deserializing DateTimeOffset value fails for ...
the workaround is to use isodatetimeconverter to deserialize your datetimeoffset properties with isodatetimeconverter.datetimestyles set to datetimestyles.
Read more >C# – Json.NET deserializing DateTimeOffset value fails for ...
The workaround is to use IsoDateTimeConverter to deserialize your DateTimeOffset properties with IsoDateTimeConverter.DateTimeStyles set to DateTimeStyles.
Read more >DateTime and DateTimeOffset support in System.Text.Json
The System.Text.Json library parses and writes DateTime and DateTimeOffset values according to the ISO 8601-1:2019 extended profile.
Read more >Compare types related to date and time
The DateTimeOffset structure represents a date and time value, together with an offset that indicates how much that value differs from UTC.
Read more >Getting Different value of time When Deserializing JSON ...
Hi. I'm trying to save selected time from lightning input type time into time fields of the object but at the time of...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Still don’t understand why this issue still exists after nearly 3.5 years…
I also met this issue