野田反序列化时的LocalDateTime与JSON.NET [英] Deserializing Noda Time's LocalDateTime with JSON.NET

查看:178
本文介绍了野田反序列化时的LocalDateTime与JSON.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用Json.NET连载野田的一些时间值和遇到了麻烦。序列化是很简单的:

I'm trying to use Json.NET to serialize some Noda Time values and having trouble. Serialization is simple enough:

LocalDateTime dt = ... // Assigned elsewhere
LocalDateTimePattern isoDateTimePattern = LocalDateTimePattern.GeneralIsoPattern;
JObject root = new JObject();
root.Add("time", isoDateTimePattern.Format(dt));

// Serialize other elements

using (var sw = new StreamWriter(stream)) {
    serializer.Serialize(sw, root);
}



但反序列化是有问题的。 Json.NET似乎从上面
识别ISO格式的日期和时间,并自动将其转换成一个DateTime对象,这不是我想要的。

But deserialization is problematic. Json.NET seems to recognize the ISO-formatted date and time from above and automatically convert it into a DateTime object, which is not what I want.

using (var sr = new StreamReader(stream)) {
        using (var jr = new JsonTextReader(sr)) {
            var root = serializer.Deserialize<JObject>(jr);

            // Deserialize other elements

            var time = root.GetValue("time"); // time.Type is JTokenType.Date
            string timeStr = time.Value<string>(); // Is "01/10/2014 10:37:32"

            // Kaboom. String is not in the right format (see above comment)
            var parsedTime = isoDateTimePattern.Parse(time.Value<string>());
        }
}

这是一个事实,即 TIMESTR 出来作为美国格式的日期和时间,我猜想, time.Value<串>()
只是调用的ToString 上Json.NET已经解析了一些内部的DateTime 对象。
我可以做类似

From the fact that timeStr comes out as a US-formatted date and time, I would guess that time.Value<string>() just calls ToString on some internal DateTime object that Json.NET has already parsed. I could do something like

var cdt = time.Value<DateTime>();
LocalDateTime ldt = new LocalDateTime(cdt.Year, cdt.Month, cdt.Day, cdt.Hour, cdt.Minute);



但是这是令人费解和手段Json.NET正在执行不必要的转换。

but that's convoluted and means Json.NET is performing unneeded conversions.

有什么办法只得到一个JSON值的原始字符串值?

Is there any way to just get the raw string value of a JSON value?

推荐答案

有一个 NodaTime.Serialization.JsonNet似乎可用的NuGet在被精确地瞄准包这个情况。下面是如何设置它:

There is a NodaTime.Serialization.JsonNet package available on NuGet that seems to be aimed exactly at this situation. Here is how to set it up:

下载和解决方案中的安装包后,添加以下使用声明您的代码:

After downloading and installing the package in your solution, add the following using statement to your code:

using NodaTime.Serialization.JsonNet;



设置您的序列化是这样的:

Set up your serializer like this:

JsonSerializer serializer = new JsonSerializer();
serializer.ConfigureForNodaTime(DateTimeZoneProviders.Tzdb);



然后,当你反序列化,你可以得到一个 LocalDateTime JObject 使用 ToObject< LocalDateTime>(串行)是这样的:

using (var sr = new StreamReader(stream))
{
    using (var jr = new JsonTextReader(sr))
    {
        var root = serializer.Deserialize<JObject>(jr);

        // Deserialize other elements

        var time = root.GetValue("time");
        var parsedTime = time.ToObject<LocalDateTime>(serializer);
    }
}

这篇关于野田反序列化时的LocalDateTime与JSON.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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