I'm using json.net to deserialize a DateTimeOffset
, but it is ignoring the specified timezone and converting the datetime to the local offset. For example, given
var content = @"{""startDateTime"":""2012-07-19T14:30:00+09:30""}";
When deserialised using:
var jsonSerializerSettings = new JsonSerializerSettings() { DateFormatHandling = DateFormatHandling.IsoDateFormat, DateParseHandling = DateParseHandling.DateTimeOffset, DateTimeZoneHandling = DateTimeZoneHandling.RoundtripKind }; var obj = JsonConvert.DeserializeObject(content, jsonSerializerSettings);
The obj will contain a property containing a DateTimeOffset
but the value will be 2012-07-19T15:30:00+10:30
i.e. converted to the local timezone instead of preserving the original timezone.
Is there a way to get the value to be parsed as expected so that the resulting DateTimeOffset
property will match the supplied value?
DeserializeObject can throw several unexpected exceptions (JsonReaderException is the one that is usually expected). These are: ArgumentException.
Deserialization. In Deserialization, it does the opposite of Serialization which means it converts JSON string to custom . Net object. In the following code, it calls the static method DeserializeObject() of the JsonConvert class by passing JSON data. It returns a custom object (BlogSites) from JSON data.
Specifies the settings on a JsonSerializer object. Newtonsoft.Json. JsonSerializerSettings. Namespace: Newtonsoft.Json.
The DateTimeOffset structure includes a DateTime value, together with an Offset property that defines the difference between the current DateTimeOffset instance's date and time and Coordinated Universal Time (UTC).
It seems to be ignoring DateParseHandling.DateTimeOffset
and is using DateParseHandling.DateTime
. I would log an issue here: https://github.com/JamesNK/Newtonsoft.Json
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With