序列化NodaTime JSON [英] Serialize NodaTime JSON

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

问题描述

与BCL的DateTime相比,我正在制作NodaTime的原型项目,但是执行此结果会使我recursionLimit超出错误。

I am making a prototype project of NodaTime compared to BCL's DateTime, but executing this result gives me recursionLimit exceeded error.

这是我用来对视图模型进行JSON化的功能。该函数返回后将发生错误。

This is the function I am using to JSONify my viewmodel. The error happens after this function returns.

    [HttpPost]
    public JsonResult GetDates(int numOfDatesToRetrieve)
    {
        List<DateTimeModel> dateTimeModelList = BuildDateTimeModelList(numOfDatesToRetrieve);

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

检查模型时,我的视图模型已正确构建。
这是我的视图模型的代码。

My view model is built properly when I inspected it. Here is the code for my view model.

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对象未正确序列化,因为当我评论

I am sure that the NodaTime objects are not serializing correctly because when I comment the code from the viewModel the JsonResult is able to execute.

我从本页 NodaTime API参考


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

Code in this namespace is not currently included in Noda Time NuGet packages; it is still deemed "experimental". To use these serializers, please download and build the Noda Time source code from the project home page.

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

So I downloaded and built the source code and replaced the dll's my project references but I don't know how to implement the JsonSerialization classes.

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

Can someone explain to me how to use NodaTime.Serialization.JsonNet classes to make my NodaTime objects serializable?

推荐答案

我们当前不支持 JavaScriptSerializer code>:我怀疑您必须为您的 all 使用 Json.NET JSON序列化。 有关序列化的用户指南页面给出了一个

We don't currently have any support for JavaScriptSerializer: I suspect you'll have to use Json.NET for all your JSON serialization. The user guide page on serialization gives a little bit more information, but it does mostly assume you already know about Json.NET.

好消息是Json.NET易于使用-您可能会发现它是就像这样简单:

The good news is that Json.NET is pretty easy to use - you may well find it's as simple as:

var settings = new JsonSerializerSettings();
settings.ConfigureForNodaTime();
string json = JsonConvert.SerializeObject(model, settings);

(或使用 JsonSerializer 。)

顺便说一句,您使用Noda Time类型的方式至少可以说有点奇怪-可能值得提出另一个有关您的详细信息的问题重新尝试实现,我们可以找到一种更惯用的方法:)

As an aside, the way you're using the Noda Time types is a little odd to say the least - it may well be worth asking another question with details of what you're trying to achieve, and we can work out a more idiomatic way of doing it :)

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

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