This is a migrated thread and some comments may be shown as answers.
Can't Deserialize DataSourceResult
3 Answers 117 Views
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 });

           

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