jack
jack

Reputation: 157

JSON.NET not converting ISO 8601 time to Noda Time Instant

The output of the code below is 1970-01-01T00:00:00Z rather than 2021-09-20T19:43:15Z. What am I doing wrong? I'm using version 3.0.5 of NodaTime and version 3.0.0 of NodaTime.Serialization.JsonNet.

class Program
    {
        class Foo
        {
            [JsonProperty("time")]
            public Instant Time { get; }
        }

        static void Main(string[] args)
        {
            const string json = "{\"time\": \"2021-09-20T19:43:15.204Z\"}";
            var foo = JsonConvert.DeserializeObject<Foo>(json,
                new JsonSerializerSettings().ConfigureForNodaTime(DateTimeZoneProviders.Tzdb));
            Console.WriteLine(foo.Time);
        }
    }

Thanks for any help,

Jack

Upvotes: 1

Views: 453

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1501926

Your Time property only has a "getter" - no setter. Just change the property to have a setter too:

[JsonProperty("time")]
public Instant Time { get; set; }

Upvotes: 3

Related Questions