Issue with deserializing NodaTime types
apavelm opened this issue · comments
Pavel Andreev commented
Hi! I faced an issue which is blocking me.
I'm using this helper to serialize/deserialize data
public static class Helpers
{
public static string ToJson(this object data)
{
var settings = new JsonSerializerSettings();
settings.ConfigureForNodaTime(DateTimeZoneProviders.Tzdb);
string json = JsonConvert.SerializeObject(data, settings);
return json;
}
public static T FromJson<T>(this string jsonText)
{
var settings = new JsonSerializerSettings();
settings.ConfigureForNodaTime(DateTimeZoneProviders.Tzdb);
var obj = JsonConvert.DeserializeObject<T>(jsonText);
return obj;
}
}
and a simple test which fails on an object deserialization.
public class TestClass2
{
public LocalDate A { get; set; }
public LocalDateTime B { get; set; }
public Instant C { get; set; }
}
public class JsonSerializerTests
{
[Fact]
public async Task Test1()
{
// arrange
var obj = new TestClass2
{
A = LocalDate.FromDateTime(DateTime.Today),
B = LocalDateTime.FromDateTime(DateTime.Now),
C = SystemClock.Instance.GetCurrentInstant()
};
//act
var jsonData = obj.ToJson();
var deserializedObj = jsonData.FromJson<TestClass2>();
//assert
obj.A.Should().Be(deserializedObj.A);
obj.B.Should().Be(deserializedObj.B);
obj.C.Should().Be(deserializedObj.C);
}
}
Pavel Andreev commented
Exception is:
Newtonsoft.Json.JsonSerializationException : Error converting value "2019-05-14" to type 'NodaTime.LocalDate'. Path 'A', line 1, position 17.
---- System.ArgumentException : Could not cast or convert from System.String to NodaTime.LocalDate.
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureType(JsonReader reader, Object value, CultureInfo culture, JsonContract contract, Type targetType)
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.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)
at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings)
at eAPG.Services.Helpers.FromJson[T](String jsonText) in W:\Projects\eapg\eAPG.Services\Helpers.cs:line 21
at eAPG.Services.Tests.JsonSerializerTests.Test1() in W:\Projects\eapg\eAPG.Services.Tests\JsonSerializerTests.cs:line 37
--- End of stack trace from previous location where exception was thrown ---
----- Inner Stack Trace -----
at Newtonsoft.Json.Utilities.ConvertUtils.EnsureTypeAssignable(Object value, Type initialType, Type targetType)
at Newtonsoft.Json.Utilities.ConvertUtils.ConvertOrCast(Object initialValue, CultureInfo culture, Type targetType)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureType(JsonReader reader, Object value, CultureInfo culture, JsonContract contract, Type targetType)
Pavel Andreev commented
Sorry, my bad. missed parameter. closing
Alexander Linhares de Brito commented
i have too problem.
what solution?
Jon Skeet commented
@Brito2327: Without more detail there's no way to help you. Please open a new issue with all the relevant details and a way of reproducing the problem