Friday, December 4, 2009

JSON Serialization

The Problem:
Here is the error that took me a few days of back-and-forth with an India programming team to figure out:

DateTime values that are greater than DateTime.MaxValue or smaller than DateTime.MinValue when converted to UTC cannot be serialized to JSON.
at System.Runtime.Serialization.Json.JsonWriterDelegator.WriteDateTime(DateTime value)
at System.Runtime.Serialization.XmlWriterDelegator.WriteDateTime(DateTime value, XmlDictionaryString name, XmlDictionaryString ns)


The Solution:
I found that I had an uninitialized date time property (meaning it was equal to DateTime.MinValue) in my data transfer object (DTO). If your WEST of GMT, this doesn't cause a problem; however, if your EAST of GMT, the DataContractJsonSerializer will puke.

3 comments:

Anonymous said...

I had the same problem (I'm in Australia). I knew it had to be something like that.

Tanmoy Rajguru said...

Got same problem from India. Thanks for it.

Anonymous said...

Got the same problem from Sweden. Crazy... thanks for the tip