如何将时区偏移量添加到 JSON.NET 序列化?

How to add timezone offset to JSON.NET serialization?(如何将时区偏移量添加到 JSON.NET 序列化?)

本文介绍了如何将时区偏移量添加到 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

get/set 中的代码仅用于调试目的.整个类与 EventDate 属性一起序列化.

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

如何修改 DateTime 变量和/或 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 而不是 DateTime?这是带有时区信息的类型.

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);

给我:

{
  "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.NowKind 等于 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);

哪些输出:

{
  "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);
    }
}

两者都应该工作.

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

本文标题为:如何将时区偏移量添加到 JSON.NET 序列化?

基础教程推荐