views:

122

answers:

1

I have a bunch of entity class (generated by Linq to SQL) with a few DateTimeOffset properties in it.

When I send that over the wire as JSON from a .asmx web service, the JSON serializer generates the following JSON:

{"DateTime":"\/Date(1252142834307)\/",
 "UtcDateTime":"\/Date(1252142834307)\/",
 "LocalDateTime":"\/Date(1252142834307)\/",
 "Date":"\/Date(1252101600000)\/",
 "Day":5,
 "DayOfWeek":6,
 "DayOfYear":248,
 "Hour":11,
 "Millisecond":307,
 "Minute":27,
 "Month":9,
 "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":14,
 "Ticks":633877468343070254,
 "UtcTicks":633877396343070254,
 "TimeOfDay":{"Ticks":412343070254,
              "Days":0,
              "Hours":11,
              "Milliseconds":307,
              "Minutes":27,
              "Seconds":14,
              "TotalDays":0.47724892390509255,
              "TotalHours":11.453974173722221,
              "TotalMilliseconds":41234307.025400005,
              "TotalMinutes":687.23845042333335,
              "TotalSeconds":41234.3070254},
 "Year":2009}
}

I would very much like to save the bandwidth and JSON parsing time the client. I would also really like to avoid having to project every single DateTimeOffset manually before sending it off to the web service.

I realize the JSON serializer do as it is suppose to, so my question is this: Is there a way to configure the JSON serializer so that it just sends the LocalDateTime property of the DateTimeOffset object?

Thanks, Egil.

+1  A: 

Modifying the object serialization output requires you to override the serialization method for the DateTimeOffset object. Since DateTimeOffset is part of the BCL, you can't overwrite this yourself.

Easiest solution would be to create a custom object that implements DateTimeOffset, allowing you to override GetObjectData from the ISerializable interface. Your webmethod will need to return your custom object instead of the DateTimeOffset object, but if the consumption of this webservice is in JSON format, this should have no effect on your client base.

Kind of a pain with system-generated classes, but you need to substitute your own classes to get control of the serialized output.

jro
I was afraid that was the case. Thank you for the answer.
Egil Hansen