为什么Json.NET分析器会自动定位日期,它的解析? [英] Why is the Json.NET Parser automatically localizing the dates it's parsing?

查看:191
本文介绍了为什么Json.NET分析器会自动定位日期,它的解析?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到的问题与Json.NET分析器。按照文档,简单地使用的ToString()无特殊转换器应该离开的Json不变。

I'm running into problems with the Json.NET Parser. According to the documentation, simply using ToString() with no special converters should leave the Json unaltered.

我的JSON字符串,但是,得到修改是有它的日期自动本地化,并且它造成的各种问题对我来说。例如,当我给它下面的字符串:

My Json string, however, is getting modified is having its date automatically localized, and it's causing all sorts of problems for me. For example, when I feed it the following string:

{_类型:基本,ID:44902a0f-7842-3a2d-83bf-a874ce0b47a1,说明:TestBasicDescription,参与:1.000000000000000,inception_date :2014-02-06T20:34:00.925000 + 00:00,EXPIRY_DATE:0001-01-01T00:00:00 + 00:00}

我希望code

Newtonsoft.Json.Linq.JObject.Parse(content).ToString(Formatting.None);

要产生一个字符串,几乎是相​​同的。我所得到的,不过是这样的:

to produce a string that is virtually identical. What I get, however, is this:

<$c$c>{"_type":"Basic","id":"44902a0f-7842-3a2d-83bf-a874ce0b47a1","description":"TestBasicDescription","participation":1.0,"inception_date":"2014-02-06T16:34:00.925-04:00","expiry_date":"0001-01-01T20:00:00-04:00"}

正如你所看到的,我的日期已经本地化。

As you can see, my dates have been localized.

是什么使这更糟糕的是,在边界日期的情况下(如 DateTime.MinValue 上面所示),该自动本地化值实际上是错误。时区偏移引起小时值环绕,既然日期不能再低了,它不会改变,有效地将日期为 DateTime.MinValue 加1一天:当它转换回UTC时间(因为它原是)日期变成0001-01-02T00:00:00 + 00:00。提示单元测试吹了起来。

What makes this even worse is that in the case of a boundary date (like DateTime.MinValue as illustrated above) this automatically localized value is actually wrong. The timezone offset has caused the hour value to wrap around, and since the date cannot go any lower, it doesn't change, effectively turning the date into DateTime.MinValue plus 1 day: when it's converted back to UTC time (as it was originally) the date becomes "0001-01-02T00:00:00+00:00". Cue unit tests blowing up.

为什么不Json.Net行为就像它说,它应注意什么?有没有什么办法可以只解析输入的Json然后的ToString()完全,因为它是?

Why isn't Json.Net behaving like it says it should? Is there any way to just parse the input Json then ToString() it exactly as it was?

推荐答案

等待@JonSkeet!回来和撤消删除你的答案!

Wait @JonSkeet! Come back and undelete your answer!

相关序列化的设置是这样的: DateParseHandling = Newtonsoft.Json.DateParseHandling.None (你提到的),我只是不知道如何将它应用在这情景直到@LB提到JsonConverter。

The relevant serializer setting was this: DateParseHandling = Newtonsoft.Json.DateParseHandling.None (which you mentioned), I just didn't know how to apply it in this scenario until @L.B. mentioned the JsonConverter.

这解决了这个问题。

Newtonsoft.Json.JsonConvert.DeserializeObject(content, 
    new Newtonsoft.Json.JsonSerializerSettings() { 
    DateParseHandling = Newtonsoft.Json.DateParseHandling.None }).ToString();

给我回:EXPIRY_DATE:0001-01-01T00:00:00 + 00:00。不过非常可疑的默认行为是的惹你的屎的,但你去那里:)

Giving me back: "expiry_date": "0001-01-01T00:00:00+00:00". Still awfully suspicious that the default behaviour is to mess with your shit, but there you go :)

这篇关于为什么Json.NET分析器会自动定位日期,它的解析?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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