Can't Deserialize DataSourceResult

4 posts, 0 answers
  1. Mike
    Mike avatar
    8 posts
    Member since:
    Jun 2018

    Posted 26 Jun 2018 Link to this post

    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?

     

     


  2. Mike
    Mike avatar
    8 posts
    Member since:
    Jun 2018

    Posted 27 Jun 2018 in reply to Mike Link to this post

    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.
  3. Plamen
    Admin
    Plamen avatar
    3079 posts

    Posted 29 Jun 2018 Link to this post

    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.
  4. Stéphane
    Stéphane avatar
    1 posts
    Member since:
    Oct 2016

    Posted 03 Dec 2018 in reply to Mike Link to this post

    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 });

               

Back to Top