nodatime / nodatime.serialization

Serialization projects for Noda Time

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Full support for DateTimeOffset

fleed opened this issue · comments

Is it possible to get full support for DateTimeOffset type?

The following unit test:

        [Fact]
        public void TestNodaDateTimeOffset()
        {
            var s = "{\"date\":\"2017-05-09T09:42:59.4570182+02:00\"}";
            var settings = new JsonSerializerSettings().ConfigureForNodaTime(DateTimeZonePr‌​oviders.Serializatio‌​n);
            var value = JsonConvert.DeserializeObject<TestClass>(s, settings);
        }

        private class TestClass
        {
            [JsonProperty("date")]
            public Instant Date { get; set; }
        }

Throws the following exception:

{NodaTime.Text.UnparsableValueException: The value string does not match a quoted string in the pattern. Value being parsed: '2017-05-09T09:42:59.4570182^+02:00'. (^ indicates error position.)
   at NodaTime.Text.ParseResult`1.GetValueOrThrow()
   at NodaTime.Serialization.JsonNet.NodaPatternConverter`1.ReadJsonImpl(JsonReader reader, JsonSerializer serializer)
   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.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)
   at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings)
   at ApiApplication.Tests.Controllers.DemoAssetsControllerTests.TestDes() in D:\git\test\ApiApplication.Tests\Controllers\Tests.cs:line 217}

But it works if I use the date string "{\"date\":\"2017-05-09T07:42:59.4570182Z\"}"

I'm using the following csproj:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>    
    <TargetFramework>netcoreapp1.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
    <PackageReference Include="NodaTime.Serialization.JsonNet" Version="2.0.0" />
    <PackageReference Include="xunit" Version="2.2.0" />
    <PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
  </ItemGroup>

</Project>

Well yes - you're trying to convert text that represents a DateTimeOffset/OffsetDateTime to an Instant.

Just use OffsetDateTime as the type instead of Instant - you can convert to an Instant later if you need to.

(If you really want to convert a value with an offset into an Instant within Json.NET, you could do that easily enough by adding your own converter - but personally I wouldn't suggest it.)

I'd prefer to keep the Instant object for test purposes.
I was expecting that this type of serialization is supported, considering that there are first-class methods to convert to and from DateTimeOffset.

Anyway I get your point and I will find a "workaround" for my specific context.

I can close the issue, thank you for the quick reply.