Search code examples
json.netodata

Difference between odatamediatypeformatter and jsonmediatypeformatter


I would like to understand the difference between OdataMediaTypeFormatter and JsonMediaTypeFormatter. I tried searching the web but didn't get an answer to this. In my product we discovered recently that OdataMediaTypeFormatter was being used while we were assuming that we are using JsonMediaTypeFormatter. Our clients use json.net to serialize their objects, so I would like to switch to using JsonMediaTypeFormatter but want to know what will change with this switch.

I know one difference between the two - one with respect to deserializing 'long' datatype. odata's json requires long values to be quoted while json.net doesn't. As mentioned in this thread here - WinJS OData JSON

Any pointers on this will help. Thanks much!


Solution

  • The ODataMediaTypeFormatter sits both in the namespace System.Web.Http.OData.Formatter (a.k.a the ASP.NET Web API for OData V1-3) and System.Web.OData.Formatter (a.k.a the ASP.NET Web API for OData V4). So it is a media type formatter for OData payload types (namely Atom and JSON light and JSON verbose for OData V3 and OData JSON for OData V4).

    On the other hand, JsonMediaTypeFormatter belongs to System.Net.Http.Formatting. So it is a native part of the .NET framework for JSON payload handling. They all derive from System.Net.Http.Formatting.MediaTypeFormatter but they have their own implementations of serializing and deserializing the different payload kinds they are targetting respectively.

    To underestand what is OData JSON, you can refer to this link OData JSON Format Version 4.0. From it you can see the ODataMediaTypeFormatter also deals with a lot of OData specific JSON elements and attributes, which JsonMediaTypeFormatter has no knowledge of.

    Thus, for your scenario, since you're using this generic JSON serializer: JSON.NET to serialize objects, there should be nothing negative about switching to using JsonMediaTypeFormatter as long as your client is not talking to an OData service.