Serialize NodaTime JSON

I am making a prototype of a NodaTime project versus a BCL DateTime, but doing that result gives me a recursionLimit exceeded error.

Recursion limit exceeded

This is the function I am using to JSONify my viewmodel. An error occurs 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 correctly when I inspected it. Here is the code for my viewmodel.

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;
}

      

I'm pretty sure the NodaTime objects are not serializing correctly because when I comment out the code from the viewModel the JsonResult can execute.

I read it from this page NodaTime API link

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

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

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

+3


source to share


2 answers


We do not currently maintain support JavaScriptSerializer

: I suspect you will need to use Json.NET for all of your JSON serialization. the serialization user guide gives a little more information, but it mostly assumes that you already know about Json.NET.

The good news is that Json.NET is pretty easy to use - you can find it as simple as:

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

      



(Or use JsonSerializer

.)

As an aside, how you use the Noda time types is a bit weird to say the least - it might be worth asking another question with the details of what you are trying to achieve and we can work a more idiomatic way to do it :)

+6


source


Serialization is supported for JSON.NET in Noda 2.0+ .

You need to install the package using NuGet:

> Install-Package NodaTime.Serialization.JsonNet

      



And then configure your serializer options to use it. This one won't work with the standard serializer / deserializer - you need to configure it explicitly.

We decided to use one statically. Your use may be different. Here's an example:

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...
    }
}

      

0


source







All Articles