Click here to Skip to main content
15,892,965 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
It appears that DataContractJsonSerializer uses different precision to NetDataContractSerializer for DateTime. Testing of serialise-deserialise failed for a DateTimeOffset property if used strict equality or testing to the nearest Tick.

The following code snippet fixed the issue (_entity1 was the original and _entity4 was the result of serialise and then deserialise):
C#
var type = typeof (Entity);
            var properties = type.GetProperties(
                BindingFlags.NonPublic |
                BindingFlags.Instance |
                BindingFlags.Public);
foreach (var property in properties)
            {
                if (property.PropertyType != typeof(DateTimeOffset)) // issues with DateTime in JSON
                {
                    Assert.AreEqual(property.GetValue(_entity1), property.GetValue(_entity4));
                }
                else
                {   // different at level of Ticks, but not an issue
                    Assert.That(property.GetValue(_entity1),
                        Is.EqualTo(property.GetValue(_entity4)).Within(1).Milliseconds);
                }
            }
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900