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

这是我用来JSONify我的视图模型的函数。此函数返回后发生错误。
[HttpPost]
public JsonResult GetDates(int numOfDatesToRetrieve)
{
List<DateTimeModel> dateTimeModelList = BuildDateTimeModelList(numOfDatesToRetrieve);
JsonResult result = Json(dateTimeModelList, JsonRequestBehavior.AllowGet);
return result;
}当我检查时,我的视图模型已经建立得很好了。这是我的视图模型的代码。
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对象可序列化吗?
发布于 2017-10-27 19:27:25
序列化是Noda Time JSON.NET中对2.0+的支持。
您需要使用NuGet安装该软件包:
> Install-Package NodaTime.Serialization.JsonNet然后配置序列化程序设置以使用它。这个不适用于默认的序列化程序/反序列化器--您需要显式地配置一个。
我们选择静态地使用一个。你的用法可能不一样。下面是一个示例:
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...
}
}https://stackoverflow.com/questions/14802672
复制相似问题