AspNetCoreOData icon indicating copy to clipboard operation
AspNetCoreOData copied to clipboard

How to add custom JSON converters?

Open RobTF opened this issue 3 years ago • 6 comments

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,

RobTF avatar Jul 03 '21 16:07 RobTF

@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.

xuzhg avatar Jul 03 '21 19:07 xuzhg

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 avatar Jul 19 '21 10:07 RobTF

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?

RobTF avatar Aug 02 '21 07:08 RobTF

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.

TehWardy avatar Aug 02 '21 10:08 TehWardy

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

chainavawongse avatar Sep 10 '21 14:09 chainavawongse