Tuesday, January 3, 2012

Json serializer fails for DateTime.MinValue

This cost me so many hours!
The .Net Json serializer ( DataContractJsonSerializer ) fails in certain time zones if you serialize uninitialized DateTime values ie DateTime.MinValue or default(DateTime)


Nice explanation by Nick Martyshchenko here:
http://stackoverflow.com/questions/4025851/why-can-datetime-minvalue-not-be-serialized-in-timezones-ahead-of-utc

You'll probably have the same issue with DateTime.MaxValue in certain time zones.

Workaround: initialize all DateTime values before serialization e.g. to DateTime.MinValue.ToUniversalTime()


No comments: