Deserialize Grid Filter into object

6 posts, 0 answers
  1. Jerald
    Jerald avatar
    6 posts
    Member since:
    Apr 2007

    Posted 28 Jun 2016 Link to this post

    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? 

  2. Danail Vasilev
    Admin
    Danail Vasilev avatar
    1502 posts

    Posted 30 Jun 2016 Link to this post

    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
  3. Jerald
    Jerald avatar
    6 posts
    Member since:
    Apr 2007

    Posted 30 Jun 2016 Link to this post

    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

  4. Danail Vasilev
    Admin
    Danail Vasilev avatar
    1502 posts

    Posted 04 Jul 2016 Link to this post

    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
  5. Bruno
    Bruno avatar
    2 posts
    Member since:
    Feb 2020

    Posted 08 Jun 2020 in reply to Danail Vasilev Link to this post

    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

  6. Alex Hajigeorgieva
    Admin
    Alex Hajigeorgieva avatar
    1082 posts

    Posted 10 Jun 2020 Link to this post

    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.
Back to Top