使用MicrosoftDateFormat时阻止Json.NET 4.5附加时区偏移量 [英] Prevent Json.NET 4.5 from appending timezone offset when using MicrosoftDateFormat

查看:74
本文介绍了使用MicrosoftDateFormat时阻止Json.NET 4.5附加时区偏移量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自定义DateTimeConverterBase实现的简短内容,是否设置了Json.NET 4.5+(设置为使用DateFormatHandling.MicrosoftDateFormat)时,不为任何非UTC DateTime附加时区偏移量,该方法可以使它保持不变?

Short of a custom DateTimeConverterBase implementation, is there some way to keep Json.NET 4.5+, when set to use DateFormatHandling.MicrosoftDateFormat, from appending a timezone offset for any non-UTC DateTime it is given?

"\/Date(1333645844276-0600)\/"

详细信息

我正在将一个API项目从使用内置的.NET JavaScriptSerializer切换到使用Json.NET生成JSON.在Json.NET中,对于UTC DateTime,默认的日期时间序列化类似于.NET版本:

Details

I am switching an API project from using the built-in .NET JavaScriptSerializer to using Json.NET to generate JSON. In Json.NET, for a UTC DateTime, the default datetime serialization was similar to the .NET version:

"\/Date(1333645844276)\/"

对于非UTC,与JavaScriptSerializer不同,Json.NET将时区偏移量附加到结果上(我所在地区为-6,这是每年的这个时间):

For non-UTC, unlike JavaScriptSerializer, Json.NET appends a timezone offset to the results (-6 for my area, this time of year):

"\/Date(1333645844276-0600)\/"

这是我用来将Json.NET 4.5+切换回\/Date(...)\/格式(称为MicrosoftDateFormat)的代码:

Here is the code I am using to switch Json.NET 4.5+ back to the \/Date(...)\/ format (called MicrosoftDateFormat):

JsonSerializerSettings customJsonSettings = new JsonSerializerSettings() {
    DateFormatHandling = DateFormatHandling.MicrosoftDateFormat
};
string result = JsonConvert.SerializeObject(DateTime.Now, customJsonSettings);

该解决方案似乎很简单,就像告诉Json.NET使用不同的DateTimeZoneHandling设置一样.我尝试了DateTimeZoneHandling(LocalUtcUnspecifiedRoundtripKind)的每种设置,它们都在输出中保持"-0600".实际上,对于非UTC DateTime,它们都会产生相同的结果.

The solution seemed like it would be as simple as telling Json.NET to use a different DateTimeZoneHandling setting. I have tried every setting for DateTimeZoneHandling (Local, Utc, Unspecified, and RoundtripKind) and they all maintain the "-0600" in the output. In fact, they all produce identical results for a non-UTC DateTime.

JsonSerializerSettings customJsonSettings = new JsonSerializerSettings() {
    DateFormatHandling = DateFormatHandling.MicrosoftDateFormat,
    DateTimeZoneHandling = DateTimeZoneHandling.Utc
};
string result = JsonConvert.SerializeObject(DateTime.Now, customJsonSettings);
"\/Date(1333647855743-0600)\/"

注意

理想情况下,我已经将所有时间都放在UTC上了.我当然计划在该API的下一版本中这样做.由于这是实时API,因此在发布新版本之前,不值得冒险进行输出更改.对于大多数JSON解析系统来说,这似乎不是问题,但是由于JSON标准并未正式说明日期序列化,因此我不能冒险进行更改.

Caveat

Ideally, I would have all my times in UTC already. I certainly plan to do so with the next version of this API. Since this is a live API, it is not worth risking an output change until a new version is released. It doesn't appear to be an issue for most JSON parsing systems, but I cannot risk the change since the JSON standard doesn't officially say anything about date serialization.

推荐答案

对于我来说似乎很好用,请参见下文.我的JSON.NET程序集说它的版本是"4.5.0.0".

It seems to work just fine for me, see below. My JSON.NET assembly says it's version "4.5.0.0".

JsonSerializerSettings customJsonSettings = new JsonSerializerSettings()
{
    DateFormatHandling = DateFormatHandling.MicrosoftDateFormat,
    DateTimeZoneHandling = DateTimeZoneHandling.Utc
};
string result = JsonConvert.SerializeObject(DateTime.Now, customJsonSettings);
Console.WriteLine(result); // "\/Date(1344249339881)\/"

也许这是一个已修复的错误?

Perhaps it was a bug that has been fixed?

明确创建日期:

var x = new { thedate = new DateTime(2009, 2, 15, 0, 0, 0, DateTimeKind.Local) };

Console.WriteLine(JsonConvert.SerializeObject(x,
    new JsonSerializerSettings() {
        DateFormatHandling = DateFormatHandling.MicrosoftDateFormat
    }));
// {"thedate":"\/Date(1234652400000+0100)\/"}

Console.WriteLine(JsonConvert.SerializeObject(x,
    new JsonSerializerSettings() {
        DateFormatHandling = DateFormatHandling.MicrosoftDateFormat,
        DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Utc
    }));
// {"thedate":"\/Date(1234652400000)\/"}

Console.WriteLine(JsonConvert.SerializeObject(x,
    new JsonSerializerSettings() {
        DateFormatHandling = DateFormatHandling.MicrosoftDateFormat,
        DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Local
    }));
// {"thedate":"\/Date(1234652400000+0100)\/"}

这篇关于使用MicrosoftDateFormat时阻止Json.NET 4.5附加时区偏移量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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