如何添加时区偏移JSON.NET序列化? [英] How to add timezone offset to JSON.NET serialization?

查看:138
本文介绍了如何添加时区偏移JSON.NET序列化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的DateTimePicker势必属性:

My DateTimePicker is bound to property:

    picker.DataBindings.Add("Value", this, "EventDate");
    ...
    private DateTime eventDate;
    public DateTime EventDate
    {
        get
        {
            var offset = TimeZoneInfo.Local.GetUtcOffset(eventDate);
            string json = JsonConvert.SerializeObject(eventDate, Formatting.Indented);
            Console.WriteLine("get: {0} --- {1}", json, offset);
            return eventDate;
        }
        set
        {
            var offset = TimeZoneInfo.Local.GetUtcOffset(value);
            string json = JsonConvert.SerializeObject(value, Formatting.Indented);
            Console.WriteLine("set: {0} --- {1}", json, offset);
            eventDate = value;
        }
    }



每当数据绑定来设置属性,我已经得到了以下结果:

Whenever data binding is to set the property, I've got the following result:

set: "2015-08-03T16:06:59" --- 04:00:00
get: "2015-08-03T16:06:59" --- 04:00:00

在获取代码/调试目的只设置。 。全班与EVENTDATE财产一起连载

The code in get/set only for debug purposes. The whole class is serialized along with EventDate property.

我怎样才能修改日期时间变量和/或JSON序列化,以包括时区为抵消这种

How can I modify DateTime variable and/or json serializer in order to include timezone offset such as:

"2014-08-03T16:06:59.8708232+04:00"

奇怪的是,如果我创造全新的DateTime对象,并分配给DateTime.Now没有约束力,JSON.NET将附加时区偏移到它。 。想不出有什么区别

Strange thing is if I create brand new DateTime object and assign to DateTime.Now without binding, JSON.NET will append timezone offset to it. Can't figure out what is the difference.

推荐答案

您是否尝试过的类型的 的DateTimeOffset 代替日期时间?这与时区的信息类型。

Have you tried type DateTimeOffset instead of DateTime? This is the type with timezone information.

var example = new
{
    Now = DateTimeOffset.Now,
    UtcNow = DateTimeOffset.UtcNow,
    Sometime = new DateTimeOffset(2014, 10, 11, 1, 4, 9, new TimeSpan(8, 0, 0)),
    FromDateTime = new DateTimeOffset(new DateTime(2010, 1, 1)),
};
string json = JsonConvert.SerializeObject(example, Formatting.Indented);
Console.WriteLine(json);



给我:

Gives me:

{
  "Now": "2014-08-03T22:08:47.8127753+08:00",
  "UtcNow": "2014-08-03T14:08:47.8137754+00:00",
  "Sometime": "2014-10-11T01:04:09+08:00",
  "FromDateTime": "2010-01-01T00:00:00+08:00"
}

修改 - 替代方式

您注意到了 DateTime.Now 在JSON已经区偏移,而手动创建的DateTime 没有。这是因为 DateTime.Now 等于 DateTimeKind.Local ,而其他有 DateTimeKind.Unspecified 和刚JSON.NET处理它们不同。

You noticed that DateTime.Now has timezone offset in JSON while manually created DateTime does not. This is because DateTime.Now has Kind equal to DateTimeKind.Local while the other has DateTimeKind.Unspecified, and JSON.NET just handles them differently.

所以,另一种方法是:

var product2 = new
{
    Now = DateTime.Now,
    Sometime = new DateTime(2014, 10, 11, 0, 0, 0),
    SometimeLocal = new DateTime(2014, 10, 11, 0, 0, 0, DateTimeKind.Local),
    SometimeUtc = new DateTime(2014, 10, 11, 0, 0, 0, DateTimeKind.Utc)
};

string json2 = JsonConvert.SerializeObject(product2, Formatting.Indented);

Console.WriteLine(json2);



它输出:

Which outputs:

{
  "Now": "2014-08-03T23:39:45.3319275+08:00",
  "Sometime": "2014-10-11T00:00:00",
  "SometimeLocal": "2014-10-11T00:00:00+08:00",
  "SometimeUtc": "2014-10-11T00:00:00Z"
}

EDIT2 - 对于数据绑定

确定你使用数据绑定。然后,您可以更新您的二传手自动转换:

OK you use data binding. Then you can update your setter to convert automatically:

picker.DataBindings.Add("Value", this, "EventDate");
...
private DateTimeOffset eventDate;
public DateTime EventDate
{
    get { return eventDate.LocalDateTime; }
    set { eventDate = new DateTimeOffset(value); }
}



然后你可以使用 EVENTDATE 来序列化到JSON

Then you can use eventDate to serialize to JSON.

,或设置

picker.DataBindings.Add("Value", this, "EventDate");
...
private DateTime eventDate;
public DateTime EventDate
{
    get { return eventDate; }
    set
    {
        // Create a copy of DateTime and set Kind to Local since Kind is readonly
        eventDate = new DateTime(value.Ticks, DateTimeKind.Local);
    }
}



双方应该工作。

Both should work.

这篇关于如何添加时区偏移JSON.NET序列化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆