OData / AspNetCoreOData

ASP.NET Core OData: A server library built upon ODataLib and ASP.NET Core
Other
458 stars 160 forks source link

How to add custom JSON converters? #222

Open RobTF opened 3 years ago

RobTF commented 3 years ago

Hi,

I see there is a new package for adding Netwonsoft JSON so I'm guessing there are available extensibility points for controlling the JSON serialization.

We are using System.Text.Json, but would like to customise how certain entities are serialized/deserialized. The OData library appears to ignore the normal ASP.NET Core MVC JSON settings, so is there some other way of doing this?

many thanks,

xuzhg commented 3 years ago

@RobTF You are right. ODL uses its own JSON writer/reader. ODL own writer/reader was designed and implemented before the Newtonsoft.JSON and System.Text.JSON.
Even though, ODL provides the IJsonWriterFactory and IJsonReaderFactory that you can customize.

RobTF commented 3 years ago

Hi Sam,

Thanks for the details - those interfaces look very low level though. Do you have any examples of an implementation whereby a particular data type is serialized/deserialized in a different way?

To be specific, we are working with the Geometry types from the NettopologySuite assemblies, as this is what EF Core works with, but are having issues getting these out of the ODATA endpoints.

RobTF commented 3 years ago

Hi @xuzhg, I just noticed that 8.0.1 of the library is now released and now exposes some classes such as ODataSerializerProvider. Will this version be able to help with the issue above?

TehWardy commented 3 years ago

Maybe create your own formatter ? I built my own for CSV, XML and Excel dumps from OData. I don't see why this wouldn't work for JSON too.

chainavawongse commented 3 years ago

Hi @RobTF, did you find the solution? IJsonWriter is quite a handful to implement.

mcattle commented 1 year ago

Looks like this might now be the solution: Override payload serialization and deserialization in odatalib - OData | Microsoft Learn