2

Is it possible to tell Json.NET to deserialize '2012-07-19' to a DateTimeOffset value with zero offset?

Code example to reproduce:

public class SingleDateTimeField
{
    public DateTimeOffset StartDateTime { get; set; }
}

[Test]
public void Convert()
{
    // Given
    var content = @"{""startDateTime"":""2012-07-19""}";
    var jsonSerializerSettings = new JsonSerializerSettings()
    {
        DateFormatHandling = DateFormatHandling.IsoDateFormat,
        DateParseHandling = DateParseHandling.DateTimeOffset,
        DateTimeZoneHandling = DateTimeZoneHandling.Utc
    };

    // When
    var obj = JsonConvert.DeserializeObject<SingleDateTimeField>(content, jsonSerializerSettings);

    // Then
    var expected = new DateTimeOffset(2012, 07, 19, 0, 0, 0, TimeSpan.Zero);
    Assert.That(obj.StartDateTime, Is.EqualTo(expected));

}

This fails with

Expected: 2012-07-19 00:00:00.000+00:00
But was: 2012-07-19 00:00:00.000+03:00

Note that the timezone is +03:00

Update:

I can get it working by using specialized converter (example below), but isnt the setting DateTimeZoneHandling = DateTimeZoneHandling.Utc supposed to already convert this UTC? AssumeUtc converter should not be required.

public class AssumeUtc : IsoDateTimeConverter
{
    public AssumeUtc()
    {
        DateTimeStyles = System.Globalization.DateTimeStyles.AssumeUniversal;
    }
}

public class SingleDateTimeField
{
    [JsonConverter(typeof(AssumeUtc))]
    public DateTimeOffset StartDateTime { get; set; }
}
Darius
  • 1,150
  • 10
  • 12
  • 1
    Possible duplicate of [Why does Json.NET DeserializeObject change the timezone to local time?](http://stackoverflow.com/questions/11553760/why-does-json-net-deserializeobject-change-the-timezone-to-local-time) – Charles Mager Nov 08 '16 at 15:12
  • you can create a custom deserializer for your property, or set a default custom deserializer when creating the serializer. http://www.newtonsoft.com/json/help/html/SerializationAttributes.htm (look at JsonConverter attribute) and http://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_JsonSerializerSettings.htm for setting defaults. – ps2goat Nov 08 '16 at 15:26
  • @CharlesMager that question deals with timezones. My question is about configuring serializer globally to treat all dates as UTC. – Darius Nov 08 '16 at 15:28
  • @ps2goat - you are right. I have already created `AssumeUtc` converter, that works great. My question is why do I have to use this given that I already configure serializer to user `DateTimeZoneHandling.Utc`. Shouldnt setting date time zone handling to UTC enough to make it work without converters? – Darius Nov 08 '16 at 15:29
  • Are you in, or is your machine set to, a UTC+3 timezone? – ps2goat Nov 08 '16 at 16:36
  • @ps2goat - yes, it is. My expectation is that if `DateTimeZoneHandling.Utc` is set then it should ignore my time zone and deserialize into UTC time. – Darius Nov 29 '16 at 20:14
  • what version of json.net do you have? – ps2goat Nov 29 '16 at 22:28

0 Answers0