ODataV4 adaptor deserialization

Hi,

I have a Blazor WASM app with DataGrids using ODataV4 adaptors connected to ASP.Net Core API which uses Microsoft.AspNetCore.OData package to serve data for the DataGrids. 

I have encountered a problem when trying to load an entity class with TimeSpan property. OData package serializes TimeSpan in ISO 8601 duration format. This causes the DataGrid Adaptor to fail as it uses default TimeSpanConverter which expects a different format. For example, "00:01:30" is serialized as "PT1M30S". This behavior should be correct according to OData construction rules. 

I wanted to ask if it is possible to specify custom JsonSerializerOptions used in ODataV4 adaptor so I can create my own converters which can correctly deserialize TimeSpan in duration format. I think it could also be useful for other scenarios where a custom converter would be needed. It seems that ODataV4 creates its own JsonSerializerOptions when deserializing data and this cannot be currently customized. This is a huge blocker for me as there does not seem to be any way to deal with my problem. 


10 Replies 1 reply marked as answer

KG Keerthana Ganesan Syncfusion Team September 23, 2022 02:25 PM UTC

Hi David,

Greetings from Syncfusion support.

Query: ODataV4 adaptor deserialization.

We have analyzed your query and we understand that you have written a custom JSON CONVERTOR to serialize the objects. Also, you have resolved your query by defining the JsonConvertor as an attribute to that property. We are afraid this is a possible approach since this is specific to one specific property not all other properties in a class. Kindly refer to the below general link for the alternative possible approach to define the custom Json converter to the property based on the type

https://docs.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-converters-how-to?pivots=dotnet-6-0#registration-sample---jsonconverter-on-a-type

Kindly use any one of the approaches (define the property as an attribute or set JSON convertor to its property type) to achieve your requirement. Please get back to us if you have further queries.


Regards,

Keerthana.



DD David Drapela September 30, 2022 08:01 AM UTC

Hello,

thank you for suggesting possible solutions. I have successfully managed to load the objects by setting a property attribute.

My question (or possible suggestion) is if I can add my JsonConverter to JsonSerializerOptions used in OData adaptor - so I do not need to annotate properties in every class with a specific type. This would be helpful not only for custom types but for example TimeOnly and DateOnly types. I think JsonSerializerOptions is created directly on OData adaptor and cannot be currently customized. Is this something that can change in future versions?


Best regards,

David 



KG Keerthana Ganesan Syncfusion Team October 3, 2022 06:34 PM UTC

Hi David,

Sorry for the inconvenience caused.

Currently, we are validating your query at our end. Further details will be updated on or before 5th October 2022.

Until then appreciate your patience.

Regards,

Keerthana.



KG Keerthana Ganesan Syncfusion Team October 5, 2022 05:46 PM UTC

Hi David,

Sorry for the inconvenience caused.

Currently, we are validating your query at our end at high priority. Further details will be updated on or before 7th October 2022.

Until then appreciate your patience.

Regards,

Keerthana.



KG Keerthana Ganesan Syncfusion Team October 7, 2022 03:49 PM UTC

Hi David,

Sorry for the inconvenience caused.

We have analyzed your query and we made a code example based on your requirement in which we have serialize the data from the server to JsonSerializer. We have attached the code snippet below, hereby you can customize the serializer as per requirement. Kindly refer to the attached code snippet for your reference.

var settings = new JsonSerializerOptions
            {
                PropertyNameCaseInsensitive = true,
                NumberHandling = JsonNumberHandling.AllowReadingFromString | JsonNumberHandling.WriteAsString,
                Converters = {
                                new DateTimeZoneHandlingConverter(),
                            }
            };
            settings.Converters.Add(new ExpandoObjectConverter());
            settings.Converters.Add(new JsonStringEnumConverter());
            DataResult args = new DataResult();
            ODataV4<T> responseData = JsonSerializer.Deserialize<ODataV4<T>>(responseContent, settings);


Kindly get back to us if you have any further queries.

Regards,

Keerthana.



DD David Drapela October 19, 2022 10:20 AM UTC

Hi,

thank you for showing me how to execute single OData query. This would help in case I need to write my own adaptor.

However, I would like to know if I can customize JsonSerializerOptions in your class 

ODataV4Adaptor so I do not have to write my own adaptor or inherit the existing one.  It seems that class ODataV4Adaptor creates its own JsonSerializerOptions instance (in method ProcessResponse) which cannot currently be customized. If it could be customized then we would not need to annotate properties in every class with a specific type, even the common ones like TimeOnly and DateOnly. 

From the research I have made it seems that it is not currently possible to modify existing serializer options so I would like to ask if this is something that could be considered for future versions.


Best regards,

David



KG Keerthana Ganesan Syncfusion Team October 21, 2022 10:38 AM UTC

Hi David,

Sorry for the inconvenience caused.

Currently, we are validating your query at our end. Further details will be updated on or before 26th October 2022.

Until then appreciate your patience.

Regards,

Keerthana.



KG Keerthana Ganesan Syncfusion Team October 27, 2022 03:46 AM UTC

Hi Davis,

Welcome from Syncfusion support.

Query: Can I customize JsonSerializer

We analyzed your query and we have made a code example based on your requirement using JsonSerializer. You can customize the serializer as per your requirement to serialize the data. Kindly refer to the attached code snippet for your reference.

         
   var settings = new JsonSerializerOptions

            {
                PropertyNameCaseInsensitive = true,
                NumberHandling = JsonNumberHandling.AllowReadingFromString | JsonNumberHandling.WriteAsString,
                Converters = {
                                new DateTimeZoneHandlingConverter(),
                            }
            };
            settings.Converters.Add(new ExpandoObjectConverter());
            settings.Converters.Add(new JsonStringEnumConverter());
            DataResult args = new DataResult();
            ODataV4<T> responseData = JsonSerializer.Deserialize<ODataV4<T>>(responseContent, settings);
            if (responseData?.Context != null)
            {
                string[] dataUrl = responseData.Context.Split(new string[] { "/$metadata#" }, StringSplitOptions.None);
                RootUrl = dataUrl[0];
                ResourceTableName = dataUrl[1];
            }

 


Kindly get back to us if you have any further queries.

Regards,

Keerthana.



DD David Drapela November 8, 2022 07:55 AM UTC

Hello,

thank you for the example, but I think we still do not understand each other. I am sorry if my description was not clear enough. 

I do not want to create my own DataAdaptor for SfGrid because your ODataV4 implementation (class Syncfusion.Blazor.Data.ODataV4Adaptor) works perfectly fine. In this adaptor there is a method Task<object> ProcessResponse<T>(object data, DataManagerRequest queries) which creates a new JsonSerializerOptions instance for every response. I wanted to ask if it would be possible to customize the JsonSerializerOptions instance (add converters) so it could handle types which are not currently supported. For example, native DateOnly and TimeOnly types are not currently supported. I would also like to add other converters for other common types in my app. 


Best regards,

David



KG Keerthana Ganesan Syncfusion Team December 6, 2022 01:28 PM UTC

Hi David,

Welcome from Syncfusion support.

We analyzed your query, and we regret to inform you that currently, we don't have support to customize the JsonSerializerOptions instance.

Kindly get back to us if you have any further queries.

Regards,

Keerthana.


Marked as answer
Loader.
Up arrow icon