首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >序列化NodaTime JSON

序列化NodaTime JSON
EN

Stack Overflow用户
提问于 2013-02-10 21:15:30
回答 1查看 6.1K关注 0票数 5

我正在制作一个NodaTime的原型项目,与BCL的DateTime进行比较,但是执行这个结果会导致recursionLimit超出错误。

这是我用来JSONify我的视图模型的函数。此函数返回后发生错误。

代码语言:javascript
复制
    [HttpPost]
    public JsonResult GetDates(int numOfDatesToRetrieve)
    {
        List<DateTimeModel> dateTimeModelList = BuildDateTimeModelList(numOfDatesToRetrieve);

        JsonResult result = Json(dateTimeModelList, JsonRequestBehavior.AllowGet);
        return result;
    }

当我检查时,我的视图模型已经建立得很好了。这是我的视图模型的代码。

代码语言:javascript
复制
public class DateTimeModel
{
    public int ID;

    public LocalDateTime NodaLocalDateTimeUTC;
    public LocalDateTime NodaLocalDateTime
    {
        get
        {
            DateTimeZone dateTimeZone = DateTimeZoneProviders.Bcl.GetZoneOrNull(BCLTimezoneID);

            //ZonedDateTime zonedDateTime = NodaLocalDateTimeUTC.InUtc().WithZone(dateTimeZone);

            OffsetDateTime offsetDateTime = new OffsetDateTime(NodaLocalDateTimeUTC, Offset.Zero);
            ZonedDateTime zonedDateTime = new ZonedDateTime(offsetDateTime.ToInstant(), dateTimeZone);
            return zonedDateTime.LocalDateTime;
        }
    }

    public OffsetDateTime NodaOffsetDateTime;

    public DateTime BclDateTimeUTC;
    public DateTime BclLocalDateTime
    {
        get
        {
            DateTime utcDateTime = DateTime.SpecifyKind(BclDateTimeUTC, DateTimeKind.Utc);
            TimeZoneInfo nzTimeZone = TimeZoneInfo.FindSystemTimeZoneById(BCLTimezoneID);
            DateTime result = TimeZoneInfo.ConvertTimeFromUtc(utcDateTime, nzTimeZone);
            return result;
        }
    }

    public DateTimeOffset BclDateTimeOffset;
    //public int Offset;
    public string OriginalDateString;
    public string BCLTimezoneID;
}

我确信NodaTime对象没有正确地序列化,因为当我注释来自viewModel的代码时,JsonResult能够执行。

我从这页上读到的NodaTime API参考

这个命名空间中的代码目前不包含在Noda Time NuGet包中;它仍然被认为是“实验性的”。若要使用这些序列化器,请从项目主页下载并构建Noda Time源代码。

因此,我下载并构建了源代码,并替换了dll的项目引用,但我不知道如何实现JsonSerialization类。

有人能向我解释如何使用NodaTime.Serialization.JsonNet类使我的NodaTime对象可序列化吗?

EN

回答 1

Stack Overflow用户

发布于 2017-10-27 19:27:25

序列化是Noda Time JSON.NET中对2.0+的支持

您需要使用NuGet安装该软件包:

代码语言:javascript
复制
> Install-Package NodaTime.Serialization.JsonNet

然后配置序列化程序设置以使用它。这个不适用于默认的序列化程序/反序列化器--您需要显式地配置一个。

我们选择静态地使用一个。你的用法可能不一样。下面是一个示例:

代码语言:javascript
复制
using Newtonsoft.Json;
using NodaTime;
using NodaTime.Serialization.JsonNet; // << Needed for the extension method to appear!
using System;

namespace MyProject
{
    public class MyClass
    {
        private static readonly JsonSerializerSettings _JsonSettings;

        static MyClass()
        {
            _JsonSettings = new JsonSerializerSettings
            {
                // To be honest, I am not sure these are needed for NodaTime,
                // but they are useful for `DateTime` objects in other cases.
                // Be careful copy/pasting these.
                DateFormatHandling = DateFormatHandling.IsoDateFormat,
                DateTimeZoneHandling = DateTimeZoneHandling.Utc,
            };

            // Enable NodaTime serialization
            // See: https://nodatime.org/2.2.x/userguide/serialization
            _JsonSettings.ConfigureForNodaTime(DateTimeZoneProviders.Tzdb);
        }

        // The rest of your code...
    }
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14802672

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档