I am developing a simple web API client. On of my models has a ExpiresOn
property that I have specified as a DateTimeOffset
.
public class Model {
public DateTimeOffset ExpiresOn { get; set; }
}
I deserialise the API reponse from a JSON string using JsonConvert.DeserializeObject<T>()
. I have noticed that since the JSON I retrieve doesn't specify any time zone information, JSON.Net will assume it's in local time and return me a DateTimeOffset
adjusted to UTC, as per my settings. I have tried different combinations but I cannot seem to 'force' JsonConvert
to read the string as if it were UTC.
Preferably, there would be an option to set that setting globally using JsonConvert.DefaultSettings
as I have similar parsing to be done in several locations.
Also, I am coding for ASP.Net Core 2.1, and my host's tz is set to CEST (+02:00). Setting a different time zone on the host is not an options.
Addendum:
An example of string
to parse would be "2018-07-09T11:22:33.45678"
. I've tried permutations of DateParseHandling
and DateTimeZoneHandling
but all return a date: 2018-07-09T11:22:33.45768+02:00
.
Obviously the deserialiser assumes that the string is in local time. Hence, is there a way to have it treat it as AssumeUniversal
would in DateTimeOffset.TryParse
?