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

Deserialize Grid Filter into object

5 Answers 1383 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jerald
Top achievements
Rank 1
Jerald asked on 28 Jun 2016, 06:58 PM

Each user is able to store grid filters in the database as a JSON string for use later.

We now have to iterate through these filters on the server to do some analytics with what the user is filtering the grid on. Is there a way in C# using Kendo.Mvc namespace to deserialize the JSON string stored into the database back into an IList<IFilterDescriptor> Filters? 

5 Answers, 1 is accepted

Sort by
0
Danail Vasilev
Telerik team
answered on 30 Jun 2016, 10:27 AM
Hi Jim,

You can use the approach from the Binding to a Web ApiController code-library to parse them. For example:
Copy Code
public DataSourceRequest ParseRequest(int? page, int? pageSize, string sort, stringgroup, string filterstring aggregates)
{
    DataSourceRequest request = new DataSourceRequest();
  
    if (page.HasValue)
    {
        request.Page = page.Value;
    }
  
    if (pageSize.HasValue)
    {
        request.PageSize = pageSize.Value;
    }
  
    if (!string.IsNullOrEmpty(sort))
    {
        request.Sorts = GridDescriptorSerializer.Deserialize<SortDescriptor>(sort);
    }
  
    if (!string.IsNullOrEmpty(filter))
    {
        request.Filters = FilterDescriptorFactory.Create(filter);
    }
  
    if (!string.IsNullOrEmpty(group))
    {
        request.Groups = GridDescriptorSerializer.Deserialize<GroupDescriptor>(group);
    }
  
    if (!string.IsNullOrEmpty(aggregates))
    {
        request.Aggregates = GridDescriptorSerializer.Deserialize<AggregateDescriptor>(aggregates);
    }
  
    return request;


Regards,
Danail Vasilev
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Jerald
Top achievements
Rank 1
answered on 30 Jun 2016, 01:16 PM

The only problem with that solution is that the filter that comes through using the .net wrapper for kendo is structured this way

    UnitsInStock~eq~3~and~(ProductName~eq~'chai'~and~ProductName~eq~'123')

and using the JavaScript library the filter is structured and stored in the database this way

    "{\"filters\":[{\"value\":\"Accident Response Fee Laws\",\"operator\":\"eq\",\"field\":\"subTopic\"}],\"logic\":\"or\"}"

    "{\"filters\":[{\"logic\":\"or\",\"filters\":[{\"value\":\"Accident Response Fee Laws\",\"operator\":\"eq\",\"field\":\"subTopic\"}]},{\"value\":\"Auto Insurance\",\"operator\":\"eq\",\"field\":\"topic\"},{\"field\":\"dateLastReviewed\",\"operator\":\"gt\",\"value\":\"2010-06-28T04:00:00.000Z\"}],\"logic\":\"and\"}"

Using the FilterDescriptorFactory on the json strings above, results in a FilterParserException: Expected token. Both of theses filters are passed in javascript to the controller by JSON.stringify(grid.dataSource.filter()); to be stored in the database for saved presets of the grid. Is there anyway to deserialize the above JSON into a list of FilterDescriptors or will I have to write a custom JsonConverter

0
Danail Vasilev
Telerik team
answered on 04 Jul 2016, 08:33 AM
Hi Jim,

You can also use the grid's build-in parameterMap function to format correctly the needed options from the DataSource - http://www.telerik.com/forums/send-datasourcerequest-with-javascript-function#uvDmkKokjUeqIexSx5LnQg

Regards,
Danail Vasilev
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Bruno
Top achievements
Rank 1
Iron
answered on 08 Jun 2020, 04:39 PM

Hi,

We tried out this approach. We first serialized the DataSourceRequest:

        public static MyDataSourceRequest ParseRequest(int page, int pageSize, IList<SortDescriptor> sorts,
            IList<GroupDescriptor> groups, IList<IFilterDescriptor> filters, IList<AggregateDescriptor> aggregates)
        {
            var request = new MyDataSourceRequest { Page = page, PageSize = pageSize };
            if (sorts.Count > 0) request.Sort = GridDescriptorSerializer.Serialize(sorts);
            if (groups.Count > 0) request.Group = GridDescriptorSerializer.Serialize(groups);
            if (aggregates.Count > 0) request.Aggregates = GridDescriptorSerializer.Serialize(aggregates);
            if (filters.Count > 0) request.Filter = Json.Encode(filters); 

            return request;
        }

We used the GridDescriptorSerializer, however this doesn't work for the FilterDescriptors, therefore we tried the serializers from System.Web.Helpers.Json (like in this example) or the Newtonsoft converter.

Now if we try to deserialize again then executing the yellow line results in the following error:

Kendo.Mvc.Infrastructure.Implementation.FilterParserException: 'Expected token'

Is there a way we can properly serialize and deserialize the filters (that can contain FilterDescriptors and CompositeFilterDescripters)?

Kind regards,

Bruno

0
Alex Hajigeorgieva
Telerik team
answered on 10 Jun 2020, 10:21 AM

Hello, Bruno,

It is not possible to deserialize filter descriptors in this way as they use an interface which cannot be instantiated. I have a forum post that goes into more details about it here:

https://www.telerik.com/forums/reset-grid-to-initial-state#2PRJHbkesUakT38wFDnjhQ

We have a feature request to make this an out of the box option and I have voted on your behalf here already:

https://feedback.telerik.com/aspnet-mvc/1357562-serialization-support-of-datasourcerequest-and-applying-outside-of-asp-net-mvc

Meanwhile, you could store the query and recreate it as this post demonstrates:

https://www.telerik.com/forums/passing-current-datasourcerequest-to-a-custom-command#d9yZdtrqCUeKUY7uvMZMeA

Kind Regards,
Alex Hajigeorgieva
Progress Telerik

Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.
Tags
Grid
Asked by
Jerald
Top achievements
Rank 1
Answers by
Danail Vasilev
Telerik team
Jerald
Top achievements
Rank 1
Bruno
Top achievements
Rank 1
Iron
Alex Hajigeorgieva
Telerik team
Share this question
or