Deserialize LocalDate from version 1 to version 2

0

I have this json serialized from Nodatime 1.4.7

{
    "$type": "NodatimeIssueTest.Product, NodatimeIssueTest",
    "FirstDate": {
        "$type": "NodaTime.LocalDate, NodaTime",
        "ticks": 12304224000000000,
        "calendar": "ISO"
    },
    "SecondDate": {
        "$type": "System.Nullable`1[[NodaTime.LocalDate, NodaTime]], mscorlib",
        "ticks": 12304224000000000,
        "calendar": "ISO"
    },
    "OtherDates": [
        {
            "$type": "NodaTime.LocalDate, NodaTime",
            "ticks": 12304224000000000,
            "calendar": "ISO"
        }
    ]
}

When deserializing this in 2.4.7 I get this error

Newtonsoft.Json.JsonSerializationException
  HResult=0x80131500
  Message=Cannot convert value to NodaTime.LocalDate
  Source=NodaTime.Serialization.JsonNet
  StackTrace:
   at NodaTime.Serialization.JsonNet.NodaConverterBase`1.ReadJson(JsonReader reader, Type objectType, Object existingValue, JsonSerializer serializer)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.DeserializeConvertable(JsonConverter converter, JsonReader reader, Type objectType, Object existingValue)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.SetPropertyValue(JsonProperty property, JsonConverter propertyConverter, JsonContainerContract containerContract, JsonProperty containerProperty, JsonReader reader, Object target)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, String id)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
   at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
   at Newtonsoft.Json.JsonSerializer.Deserialize[T](JsonReader reader)
   at NodatimeIssueTest.TestClass.TestDeserialize() in 

Inner Exception 1:
InvalidNodaDataException: Unexpected token parsing LocalDate. Expected String, got StartObject.

When serializing the same data in 2.4.7 I get another structure than in 1.4.7

{
    "$type": "NodatimeIssueTest.Product, NodatimeIssueTest",
    "FirstDate": "2008-12-28",
    "SecondDate": "2008-12-28",
    "OtherDates": [
        "2008-12-28"
    ]
}

To reproduce: I first run Serialize with 1.4.7 version and then verify it works to deserialize by running Deserialize

Then I upgrade to 2.4.7 and run Deserialize again and that's when I get the error InvalidNodaDataException: Unexpected token parsing LocalDate. Expected String, got StartObject.

using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json;
using NodaTime;
using NodaTime.Serialization.JsonNet;
using NUnit.Framework;

namespace NodatimeIssueTest
{
    [TestFixture]
    public class TestClass
    {
        [Test]
        public void Serialize()
        {
            Product product = new Product();
            product.FirstDate = new LocalDate(2008, 12, 28);
            product.SecondDate = new LocalDate(2008, 12, 28);
            product.OtherDates = new List<LocalDate> { new LocalDate(2008, 12, 28) };

            JsonSerializer serializer = new JsonSerializer();
            serializer.NullValueHandling = NullValueHandling.Ignore;
            serializer.TypeNameHandling = TypeNameHandling.Objects;
            serializer.ConfigureForNodaTime(DateTimeZoneProviders.Tzdb);

            using (StreamWriter sw = new StreamWriter(@"c:\Temp\json.txt"))
            using (JsonWriter writer = new JsonTextWriter(sw))
            {
                serializer.Serialize(writer, product);
            }
        }

        [Test]
        public void Deserialize()
        {
            JsonSerializer serializer = new JsonSerializer();
            serializer.NullValueHandling = NullValueHandling.Ignore;
            serializer.TypeNameHandling = TypeNameHandling.Objects;
            serializer.ConfigureForNodaTime(DateTimeZoneProviders.Tzdb);

            using (StreamReader sr = new StreamReader(@"c:\Temp\json.json"))
            using (JsonTextReader reader = new JsonTextReader(sr))
            {
                var product = serializer.Deserialize<Product>(reader);
                Assert.AreEqual(product.FirstDate, new LocalDate(2008, 12, 28));
                Assert.AreEqual(product.SecondDate, new LocalDate(2008, 12, 28));
                Assert.AreEqual(product.OtherDates[0], new LocalDate(2008, 12, 28));
            }
        }
    }

    public class Product
    {
        public LocalDate FirstDate { get; set; }
        public LocalDate? SecondDate { get; set; }
        public List<LocalDate> OtherDates { get; set; }
    }
}

How do I upgrade to 2.4.7 with old structure of LocalDate. And maybe other types as well.

nodatime
asked on Stack Overflow Sep 17, 2019 by Martin Nilsson

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0