c# - How to deserialize "2015-02-02" to DateTimeOffset as UTC? -
is possible tell json.net deserialize '2012-07-19' datetimeoffset value 0 offset?
code example 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); // 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
was: 2012-07-19 00:00:00.000+03:00
note timezone +03:00
update:
i can working using specialized converter (example below), isnt setting datetimezonehandling = datetimezonehandling.utc
supposed convert utc? assumeutc
converter should not required.
public class assumeutc : isodatetimeconverter { public assumeutc() { datetimestyles = system.globalization.datetimestyles.assumeuniversal; } } public class singledatetimefield { [jsonconverter(typeof(assumeutc))] public datetimeoffset startdatetime { get; set; } }
Comments
Post a Comment