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!

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x