DataContractJsonSerializer and DateTime values

In using the DataContractJsonSerializer beware of DateTime instances which have been instantiated as non-UTC. If you declare without explicitly assigning a DateTime you get the same as default(DateTime), this is 01/01/01 00:00:00 (0 ticks) with a DateTimeKind of Unspecified. When the DataContractJsonSerializer looks at this value (or a Local datetime), it will try to convert it to UTC by subtracting the time zone ala UTC standard notation, this results in a date time that is less that 0 ticks and an out of range exception if you are west of GMT.

The solution is to always instantiate as UTC and you can avoid this issue. As mentioned previously you cannot interrupt the serialization process for DateTime on a member of the DataContract serializer family.

Another pain in the rectum is that it will change the accuracy of your DateTime value from 100 ns to 1 ms, so if you are relying on this value being the same for change tracking or any other means then this will produce problems.

WCF Collections with Payload via Surrogates

Previously I posted about the problem with WCF and collection which have a payload in them. I came across this problem again when using the DataContractJsonSerializer and thought I would look at possibilities to resolve it.

If you have implemented the IXmlSerializable interface then you are kind of stuffed because the Json serializer will emit json with a bunch of xml in the middle….nice! It is really useful to reference sowmy’s post on the precedence of serializable classes in the Data Contract family of serializers.

Instead you can use a Surrogate. This is a class which implements IDataContractSurrogate and can be passed to the constructor of the serializer. It tells the serializer that when you come to serialize type x, instead use type y and I will give you the converted instance. At the other end the class gets deserialized into the original type and all is good.

For our custom collection with payload this works great. It will not however work with CLR types like DateTime, the precedence of WCF serialization kicks in before the surrogate provider is queried for a transmission type.