I have this class:
private class SimpleClass
{
public DateTimeOffset Date;
}
And when I try to serialize it by JavaScriptSerializer on .NET 3.5 result is following JSON:
"{\"Date\":{\"DateTime\":\"\\/Date(1377674408500)\\/\",\"UtcDateTime\":\"\\/Date(1377674408500)\\/\",\"LocalDateTime\":\"\\/Date(1377674408500)\\/\",\"Date\":\"\\/Date(1377640800000)\\/\",\"Day\":28,\"DayOfWeek\":3,\"DayOfYear\":240,\"Hour\":9,\"Millisecond\":500,\"Minute\":20,\"Month\":8,\"Offset\":{\"Ticks\":72000000000,\"Days\":0,\"Hours\":2,\"Milliseconds\":0,\"Minutes\":0,\"Seconds\":0,\"TotalDays\":0.083333333333333329,\"TotalHours\":2,\"TotalMilliseconds\":7200000,\"TotalMinutes\":120,\"TotalSeconds\":7200},\"Second\":8,\"Ticks\":635132784085002695,\"UtcTicks\":635132712085002695,\"TimeOfDay\":{\"Ticks\":336085002695,\"Days\":0,\"Hours\":9,\"Milliseconds\":500,\"Minutes\":20,\"Seconds\":8,\"TotalDays\":0.38898727163773145,\"TotalHours\":9.3356945193055552,\"TotalMilliseconds\":33608500.2695,\"TotalMinutes\":560.14167115833334,\"TotalSeconds\":33608.5002695},\"Year\":2013}}"
And it is not possible to deserialize the result.
When I do the same with .NET 4.0 result is just:
"{\"Date\":\"\\/Date(1377675074146)\\/\"}"
And it is possible to deserialize this result.
This causing me big problem because I have client side which is written in .net 3.5 and I need to deserialize result on the server side written in .net 4.
Just to be complete here is the code for serialization and deserialization:
JavaScriptSerializer serializer = new JavaScriptSerializer();
DateTime dt = DateTime.Now;
SimpleClass instance = new SimpleClass();
instance.Date = dt;
string jsonStr = serializer.Serialize(instance);
SimpleClass newInstance = serializer.Deserialize<SimpleClass>(jsonStr);
Thanks for any ideas.
The bigger problem is when you serialize Date value and Deserialize it back, you lost DateTimeOffset and the Deserialized value is in UTC time. So let's start from begining, what kind of serializer/deserializer you user? .Net / Json.Net / LightJson / ...