nodatime / nodatime.serialization

Serialization projects for Noda Time

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Json.NET Period deserialization does not work

supermihi opened this issue · comments

The deserialization of Period seems broken with the current release: the following test fails at the second “assert“, though I'd assume that both methods yield the same Period.

[Fact]
public void CanParsePeriod()
{
    var periodString = "PT30M";
    
    var test1 = PeriodPattern.NormalizingIso.Parse("PT30M").Value;
    Assert.Equal(Period.FromMinutes(30), test1);

    var test2 = JsonConvert.DeserializeObject<Period>(periodString,
        new[] {NodaConverters.NormalizingIsoPeriodConverter});
    Assert.Equal(Period.FromMinutes(30), test2);

}
Newtonsoft.Json.JsonReaderException : Unexpected character encountered while parsing value: P. Path '', line 0, position 0.
   at Newtonsoft.Json.JsonTextReader.ParseValue()
   at Newtonsoft.Json.JsonReader.ReadForType(JsonContract contract, Boolean hasConverter)
   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, JsonConverter[] converters)

The problem is that you're trying to deserialize a string with content of just PT30M. That isn't a valid JSON value - it needs to have double quotes round it to be a valid JSON string. If you call DeserializeObject<Period>("\"PT30M\"", ...) then it works fine - I've just tested that.

Of course, normally you wouldn't be putting quotes on yourself - the serialization process would create valid JSON for you.

Thanks for your answer and sorry for the noise, I missed that point!

Not a problem at all :)