This is a migrated thread and some comments may be shown as answers.

Can't Deserialize DataSourceResult

3 Answers 1351 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Mike
Top achievements
Rank 1
Mike asked on 27 Jun 2018, 12:32 AM

Let me know if I'm doing something wrong here.  I'm creating a Web Service to service my Web Application through a REST API.  One class of methods I'm exposing provides the view models that feed into Kendo UI controls, like the grid.  I'm trying to separate the concerns of the client and server by passing the paging parameters to the service and, when the service has completed the query and reduced the results down to a specific number of items (page size) on a given page (page #), I attempt to return the results to the client using the following:

 

            DataSourceResult dataSourceResult = investments.ToDataSourceResult<InvestmentViewModel>(dataSourceRequest);
            dataSourceResult.Total = total;
            return Json(dataSourceResult);

On the client, I attempt to deserialize this data with:

 

                //Deserializing the response recieved from web api and storing into the Employee list

                dataSourceResult = JsonConvert.DeserializeObject<DataSourceResult>(result);

 

But I get an error message:

                Newtonsoft.Json.JsonSerializationException: 'Cannot create and populate list type System.Collections.IEnumerable. Path 'data', line 1, position 9.'

What is the proper way to deserialize the DataSourceResult JSON?

 

 


3 Answers, 1 is accepted

Sort by
0
Mike
Top achievements
Rank 1
answered on 27 Jun 2018, 02:54 PM
I've noticed that the Kendo.MVC.UI package has a version of DataSourceRequest and DataSourceResult.  So in addition to the confusion of having both types defined in different assemblies, it appears to be that none of them are suitable for deserialization.  The 'data' element of the JSON needs to have a type to deserialize into, so I'm not exactly sure how these structures were intended to be passed back and forth through a REST Api.  Some guidance or an example would be welcome.
0
Plamen
Telerik team
answered on 29 Jun 2018, 01:44 PM
Hi,

In such  case may be the best place to look for will be the online demos where DataSourceRequest  is used on many places and in various scenarios.  You could download the application from your Telerik account and play with it locally.

Yes indeed currently our documentation is lacking information about these features but we will are doing our best to improve this part shortly.

If you still have questions please share the exact scenario and the code that is used so we could inspect it and be more helpful.

Regards,
Plamen
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
Stéphane
Top achievements
Rank 1
answered on 03 Dec 2018, 07:13 PM

JsonConvert can't tell what the concrete type for data is, so all you need to do is to add the type names with the additionnal argument of type JsonSerializerSettings to the serializing and deserializing calls.

Here is the code:

 

Server side:

            DataSourceResult dataSourceResult = investments.ToDataSourceResult<InvestmentViewModel>(dataSourceRequest);
            dataSourceResult.Total = total;
            return Json(dataSourceResult, new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.All } );

 

Client side:

 

            dataSourceResult = JsonConvert.DeserializeObject<DataSourceResult>(result, new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.All });

           

Jeandre
Top achievements
Rank 1
commented on 09 Jun 2021, 04:51 PM

Hi

I've set my .net Core results to be like the following:

IEnumerable<Object> response; // Get data from dapper, data is in Json format, like JObject
var result = await response.ToDataSourceResultAsync(dataSourceRequest);

var jsonResult = JsonConvert.SerializeObject(result, new JsonSerializerSettings() {
TypeNameAssemblyFormatHandling = TypeNameAssemblyFormatHandling.Full,
TypeNameHandling = TypeNameHandling.All });

return Ok(jsonResult);



On my MVC project i do the following:

var result = await responseTask.Content.ReadAsStringAsync();

var _ReportData = JsonConvert.DeserializeObject<DataSourceResult>(result, new JsonSerializerSettings()
{
TypeNameAssemblyFormatHandling = TypeNameAssemblyFormatHandling.Full,
TypeNameHandling = TypeNameHandling.All
});

Then I get the following error when it tries to deserialize it:

{"Error resolving type specified in JSON 'Kendo.Mvc.UI.DataSourceResult, Kendo.Mvc, Version=2021.1.330.0, Culture=neutral, PublicKeyToken=121fae78165ba3d4'. Path '$type', line 1, position 123."}

Has this issue been resolved?

Not sure what I'm doing wrong, have been struggling the whole day to try and get dynamic columns to work with MVC. I'm also clueless what the front end should be:

index.cshtml:

@(Html.Kendo().Grid<dynamic>().Name("ReportGrid")

.DataSource(dataSource => dataSource

.Ajax()
.Read(read => read.Action("ReportDataResults", "Report"))

)
)


Please help ? :)
Tsvetomir
Telerik team
commented on 14 Jun 2021, 11:47 AM

In general, the DataSourceRequest is a rather custom object that uses interfaces internally. The deserialization of interfaces in C# requires additional adjustments (speaking outside of Telerik UI).

Moreover, we have created a custom model binder for the DataSourceRequest that uses a couple of Factory instances that parse the request's payload into valid C# object that can be used for the query building process later on.

Using a grid with dynamic data type is possible and we have a dedicated example in our auxiliary GitHub repository at:

https://github.com/telerik/ui-for-aspnet-mvc-examples/blob/master/grid/grid-bind-to-collection-dynamic/KendoMVCWrappers/Controllers/HomeController.cs

 

Tags
General Discussions
Asked by
Mike
Top achievements
Rank 1
Answers by
Mike
Top achievements
Rank 1
Plamen
Telerik team
Stéphane
Top achievements
Rank 1
Share this question
or