2

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!

Jan Deutschl
  • 561
  • 2
  • 14
  • I think what you need is answered here: http://stackoverflow.com/a/15400048/4067893 This is: you need to implement a custom ODataEntityTypeSerializer. – elbecita Apr 25 '16 at 12:15
  • Yeah, it's a good start, but it doesn't say, how to change serialization behavior for primitive types. – Jan Deutschl Apr 25 '16 at 18:51

1 Answers1

6

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.

Vincent
  • 340
  • 2
  • 11