2

If multiple grouping is used, jqgrid advanced search can generate search criteria like

{"groupOp":"AND","rules":[],"groups":[{"groupOp":"AND","rules":[{"field":"Nimi","op":"cn","data":"kk"},{"field":"Nimi","op":"cn","data":"kkk"}],"groups":[]}]}

Trying to de-serialize it in ASP .NET MVC2 using

            var serializer = new JavaScriptSerializer();
            var filtersList = serializer.Deserialize<Filter>(_filters);


class Filter
{
    public GroupOp groupOp { get; set; }
    public List<Rule> rules { get; set; }
    public List<Filter> groups { get; set; }
}

class Rule
{
    public string field { get; set; }
    public Operations op { get; set; }
    public string data { get; set; }
}

enum GroupOp
{
    AND,
    OR
}

enum Operations
{
    eq, // "equal"
    ne, // "not equal"
    lt, // "less"
    le, // "less or equal"
    gt, // "greater"
    ge, // "greater or equal"
    bw, // "begins with"
    bn, // "does not begin with"
    @in, // "in"
    ni, // "not in"
    ew, // "ends with"
    en, // "does not end with"
    cn, // "contains"
    nc  // "does not contain"
}

returns empty filtersList.rules property

How to get correct rules from this data ?

update

filter

    {"groupOp":"AND","rules":[{"field":"Nimi","op":"cn","data":""},
{"field":"Nimi","op":"cn","data":""},{"field":"Nimi","op":"cn","data":""}],
"groups":[{"groupOp":"AND","rules":[],"groups":[]},{"groupOp":"AND","rules":
[{"field":"Nimi","op":"cn","data":""}],"groups":[{"groupOp":"AND","rules":[],
"groups":[{"groupOp":"AND","rules":[],"groups":[]},{"groupOp":"AND","rules":[],"groups":[{"groupOp":"AND","rules":[],"groups":[{"groupOp":"AND","rules":[],
"groups":[{"groupOp":"AND","rules":[],"groups":[{"groupOp":"AND","rules":[],"groups":[]},{"groupOp":"AND","rules":[],"groups":[]}]}]}]}]},{"groupOp":"AND","rules":[{"field":"Nimi","op":"cn","data":""}],"groups":[{"groupOp":"AND","rules":[{"field":"Nimi","op":"cn","data":""},{"field":"Nimi","op":"cn","data":""}],
"groups":[]}]},{"groupOp":"AND","rules":[],"groups":[]},{"groupOp":"AND","rules":[],"groups":[]},{"groupOp":"AND","rules":[],"groups":[]},{"groupOp":"AND","rules":[],"groups":[]}]}]}]}

using code from updated part in referenced answer still generates invalid where

    ((klient.Nimi ILIKE ('%' || E'' || '%') ESCAPE '!')AND(klient.Nimi ILIKE
 ('%' || E'' || '%') ESCAPE '!')AND(klient.Nimi ILIKE ('%' || E'' || '%') ESCAPE '!'))AND
((((klient.Nimi ILIKE ('%' || E'' || '%') ESCAPE '!'))AND(((((())))AND(((klient.Nimi ILIKE ('%' || E'' || '%') ESCAPE '!'))AND(((klient.Nimi ILIKE ('%' || E'' || '%') ESCAPE '!')
AND(klient.Nimi ILIKE ('%' || E'' || '%') ESCAPE '!'))))))))
Andrus
  • 26,339
  • 60
  • 204
  • 378

1 Answers1

1

I see no problem that the code

const string filters = "{\"groupOp\":\"AND\",\"rules\":[]," +
    "\"groups\":[{\"groupOp\":\"AND\",\"rules\":[" +
        "{\"field\":\"Nimi\",\"op\":\"cn\",\"data\":\"kk\"}," +
        "{\"field\":\"Nimi\",\"op\":\"cn\",\"data\":\"kkk\"}],\"groups\":[]}]}";

var serializer = new JavaScriptSerializer();
var filtersList = serializer.Deserialize<Filter>(filters);

produce the filtersList where filtersList.rules is empty list. It corresponds to the input data which you has. On the other side the filtersList.groups part is not empty. The filtersList from the above code will produce the Filter object which equivalent to the following direct initializing:

var filtersList = new Filter {
    groupOp = GroupOp.AND,
    rules = new List<Rule>(0),
    groups = new List<Filter> {
        new Filter {
            groupOp = GroupOp.AND,
            rules = new List<Rule> {
                new Rule {field = "Nimi", op = Operations.cn, data = "kk"},
                new Rule {field = "Nimi", op = Operations.cn, data = "kkk"}
            },
            groups = new List<Filter>(0)
        }
    }
};

If one parse the information from the filtersList one can get at the end still the WHERE statement like the following

(Nimi LIKE '%kk%') AND (Nimi LIKE '%kkk%')

see the code of UPDATED part of the answer for example. To be exactly the code from the answer produces ((Nimi LIKE '%kk%')AND(Nimi LIKE '%kkk%')) which is practically the same.

Community
  • 1
  • 1
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • I tried updated part. For empty filter `{"groupOp":"AND","rules":[],"groups":[]}` it returns null – Andrus Apr 08 '12 at 07:39
  • @Andrus: Is it not correct? To be exactly it is `new Filter { groupOp = GroupOp.AND, rules = new List (0), groups = new List(0) };`. So it is exactly like your input data. What deserialized value you expect for the input? – Oleg Apr 08 '12 at 08:22
  • I updated question and added testcase which returns invalid where clause using UPDATED part from http://stackoverflow.com/a/10052736/315935 – Andrus Apr 08 '12 at 08:30
  • @Andrus: I agree now that you should modify the code more to have no constructs like `(((())))`. – Oleg Apr 08 '12 at 08:49
  • this search condition is sent by jqgrid. How to modify your code to support empty filters? – Andrus Apr 08 '12 at 08:58
  • @Andrus: It's much more easy to modify my code where I am the owner of the code. :-) But before all I'll have breakfast... – Oleg Apr 08 '12 at 09:01
  • 1
    @Andrus: I updated the code one more time. The main problem was the usage of `Capacity` property of the `StringBuilder` instead of `Length` property. I added additional code which reduce a little the number of brackets in the resulting WHERE string. The code changes are not required. – Oleg Apr 08 '12 at 11:53
  • thank you. It worked. If advanced search dialog height is greater than screen height, bottom rules and buttons are not visible and not accessible. How to enable scrollbar in advanced searc dialog if required ? – Andrus Apr 08 '12 at 14:41