Jan Deutschl
Jan Deutschl

Reputation: 571

WebAPI OData Datetime serialization

I need to change the way OData serializes DateTime and DateTimeOffset.

Currently we are using Microsoft.AspNet.OData 5.9.0

By default the format is most likely yyyy-MM-dd'T'HH:mm:ss.FFFFFFzzz, but I need to have constant number of digits so something like yyyy-MM-dd'T'HH:mm:ss.fffzzz.

So far I've learned that WebApi OData does not use Newtonsoft.Json for Json serialization as WebApi does and that it's pretty hard to find some examples of how to change serializer behavior.

Thanks for help!

Upvotes: 2

Views: 1966

Answers (1)

Vincent
Vincent

Reputation: 340

You can achieve this via creating a customized payload coverter,

  1. Implement a class extends class ODataPayloadValueConverter

  2. Override method public override object ConvertToPayloadValue(object value, IEdmTypeReference edmTypeReference)

  3. Have a check like this in the method if (value is DateTimeOffSet) { return "CustomziedString"; }

  4. Register into your model via setting like model.SetPayloadValueConverter(converter);

Then the payload in response will be shown the DateTimeOffset in your customized way.

Upvotes: 6

Related Questions