Why does Json.NET DeserializeObject change the timezone to local time?

大城市里の小女人 提交于 2019-12-03 05:19:23

It seems to be ignoring DateParseHandling.DateTimeOffset and is using DateParseHandling.DateTime. I would log an issue here: https://github.com/JamesNK/Newtonsoft.Json

txavier

If you're using .NET WebApi you can add the following to the WebApiConfig.cs file to handle this globally in your application.

config.Formatters.JsonFormatter.SerializerSettings.DateTimeZoneHandling = 
    Newtonsoft.Json.DateTimeZoneHandling.Local;

This will specifically tell the JsonFormatter to include and understand the local time zone information when serializing and deserializing a date.

I'm not sure regarding which version did you use, because at some point of time we had the same problem, then update fixed it...

Your code works wrong for me also, but if I create class like

public class A
{
    public DateTimeOffset startDateTime;
}

and call

var obj = JsonConvert.DeserializeObject<A>(content, jsonSerializerSettings);

everything works as expected. Yes, it's bug for sure, yes, I don't know how to get result exactly as YOU want, but probably, it will help for someone else.

Grey Wolf

Try using this:

microsoftDateFormatSettings = new JsonSerializerSettings
{
    DateFormatHandling = DateFormatHandling.MicrosoftDateFormat,
    DateTimeZoneHandling = DateTimeZoneHandling.Local
};
var items = JsonConvert.DeserializeObject<List<lstObject>>(jsonString, microsoftDateFormatSettings);

I don't know if it will work in all cases but for me it did. You can try some other values for DateTimeZoneHandling or search for more options on Google.

This works for me, a timezone is preserved

var jss = new JsonSerializerSettings
    {
         DateFormatHandling = DateFormatHandling.IsoDateFormat,
         DateTimeZoneHandling = DateTimeZoneHandling.Local, 
         DateParseHandling = DateParseHandling.DateTimeOffset
    };
var responseObj = JsonConvert.DeserializeObject<dynamic>(body, jss);
return responseObj.Select(s => new {
                    id = s["id"].Value<int>(),
                    date = s["date"].Value<DateTimeOffset>().DateTime,
                });

A JSON body is something like this

[
    {
        "id": 211,
        "date": "2017-10-22T12:00:00+03:00",
        "status": 1
    },
    {
        "id": 212,
        "date": "2017-10-28T12:00:00+03:00",
        "status": 1
    }
]

To use these settings in serializer, type:

var serializerSettings = new JsonSerializerSettings
            {
                DateFormatHandling = DateFormatHandling.MicrosoftDateFormat,
                DateTimeZoneHandling = DateTimeZoneHandling.Local
            };
            var serializer = JsonSerializer.Create(serializerSettings);
Adel Nasiri

As a simple way, you can Convert Date to Ticks for serializing and convert it from Ticks to Date for deserializing:

Serializing:

DateTime date = new DateTime();
ticks = date.Ticks

Deserializing"

Datetime = new Datetime(ticks);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!