Serialize/DeSerialize DataSourceRequest and DataSourceResult

2 Answers 73 Views
Grid
Adrian
Top achievements
Rank 1
Iron
Veteran
Iron
Adrian asked on 05 May 2021, 07:47 PM | edited on 05 May 2021, 07:48 PM

Hi,

I am using the CSLA framework with Blazor WebAssembly and am trying to send the Telerik.DataSource.DataSourceRequest from the Blazor Client using a Telerik Blazor Grid through to the CSLA DataPortal API endpoint on the Blazor Server.

Unfortunately the CSLA framework is unable to successfully serialize and de-serialize the Telerik.DataSource.DataSourceRequest object as is. 

I tried creating a CSLA compatible wrapper so that i could use the built-in CSLA serialization, unfortunately i have hit an issue in the fact that CSLA only supports serialization of primative types (int, float, string, datetime, etc).

My discussion (https://github.com/MarimerLLC/csla/discussions/2268) on the CSLA repo suggests that I may need to serialize to a string or byte[] array.

The option I'm left with is to manually serialize the entire Telerik.DataSource.DataSourceRequest object as either a string or byte[] array by using either System.Text.Json or Newtonsoft.Json.

Then when the string or byte[] array is received by the CSLA DataPortal API endpoint I can manually de-serialize the parameter as a Telerik.DataSource.DataSourceRequest object.

Looking at the Telerik docs it appears that the Telerik.DataSource.DataSourceRequest object supports System.Text.Json out of the box but if I want to use Newtonsoft.Json then i might have to do some custom serialization, therefore my preference would be to use System.Text.Json.

How can I use System.Text.Json to serialize and de-serialize the Telerik.DataSource.DataSourceRequest object manually as my mode of transport to the CSLA DataPortal API endpoint doesn't use the native Blazor System.Text.Json serializer?

2 Answers, 1 is accepted

Sort by
0
Accepted
Adrian
Top achievements
Rank 1
Iron
Veteran
Iron
answered on 06 May 2021, 07:33 AM
I have managed to acchieve successfully serializing and de-serializing the Telerik.DataSource.DataSourceRequest object using the 2 extension methods below:
        public static DataSourceRequestWrapper ToDataSourceRequestWrapper(this Telerik.DataSource.DataSourceRequest dataSourceRequest)
        {
            var jsonUtf8Bytes = JsonSerializer.SerializeToUtf8Bytes(dataSourceRequest);

            return new() {DataSourceRequestAsJson = jsonUtf8Bytes};
        }

        public static Telerik.DataSource.DataSourceRequest ToDataSourceRequest(this DataSourceRequestWrapper dataSourceRequestWrapper)
        {
            var readOnlySpan = new ReadOnlySpan<byte>(dataSourceRequestWrapper.DataSourceRequestAsJson);
            var dataSourceRequest = JsonSerializer.Deserialize<Telerik.DataSource.DataSourceRequest>(readOnlySpan);

            return dataSourceRequest;
        }

0
Marin Bratanov
Telerik team
answered on 06 May 2021, 05:18 AM

Hello Adrian,

You can find sample projects here: https://github.com/telerik/blazor-ui/tree/master/grid/datasourcerequest-on-server

Regards,
Marin Bratanov
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tags
Grid
Asked by
Adrian
Top achievements
Rank 1
Iron
Veteran
Iron
Answers by
Adrian
Top achievements
Rank 1
Iron
Veteran
Iron
Marin Bratanov
Telerik team
Share this question
or