DateTime kind not respected

790 views
Skip to first unread message

David Thompson

unread,
Jan 8, 2013, 9:57:01 AM1/8/13
to rest...@googlegroups.com
I am having a problem with a date property being deserialised incorrectly from a JSON API. The date is being returned from the API as UTC to the ISO8601 format e.g. 2013-01-085T13:15:30Z. RestSharp is deserialising the object and the date is being populated incorrectly the kind value is returned as 'unspecified' even though the date string has the 'Z' to indicate it is UTC.

After looking at RestSharp sourcecode, it appears the deserialization is calling the DateTime.ParseExact() or DateTime.
TryParseExact() methods depending on whether a dateformat has been specified. Neither of these method calls specify a DateTimeStyle
and as such the original date format is not being preserved. Looking at the msdn documentation it appears that the only DateTimeStyle which would preserve the original datetime kind is the RoundtripStyle.
http://msdn.microsoft.com/en-us/library/91hfhz89.aspx

I have created a series of tests to demonstrate this problem, and they are attached.

Am I correct in saying the DateTimeStyle.RoundTripKind needs to be specified, and the alterations to the date formats in the TryParseExact calls would be the correct solution?
If so would this be a change that would considered for the standard deserializers?

Thanks,
David

DateTimeTests.cs

Russell Chadwick

unread,
Jan 10, 2013, 12:17:16 PM1/10/13
to rest...@googlegroups.com

Believe this is the same problem that I committed a fix for here

A suitable workaround has been to explicitly specify the date time format on the RestRequest instance.
- Russ

David Thompson

unread,
Jan 16, 2013, 10:20:15 AM1/16/13
to rest...@googlegroups.com
Russell, thanks for the link, that workaround does help as now the datetime and kind reflect the correct values, but it is always in the context of my local time. Id much rather the datetime and kind be the same as what the API returned.

David.

Travis Silvers

unread,
Sep 3, 2014, 11:09:16 AM9/3/14
to rest...@googlegroups.com
It took me 2 days to track my problem down to the date serialization. In the end I just used Json.Net to deserialize the response which resulted in the correct DateTime.
Reply all
Reply to author
Forward
0 new messages