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
You can use the approach from the Binding to a Web ApiController code-library to parse them. For example:
public
DataSourceRequest ParseRequest(
int
? page,
int
? pageSize,
string
sort,
string
group,
string
filter,
string
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
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
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
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
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:
Meanwhile, you could store the query and recreate it as this post demonstrates:
Kind Regards,
Alex Hajigeorgieva
Progress Telerik
Our thoughts here at Progress are with those affected by the outbreak.