Jackson: a deserializing recursive object

I am trying to analyze the filter parameters sent by the KendoUI network to my web service, and I have some problems that convince Jackson to parse this JSON. As far as I know, I can control the format of the parameters that Kendo sends, but I do not know how I would marshal the parameters in a more convenient format so that they remain unchanged at the moment.

I intend to convert these parameters into an SQL query for an Oracle database.

JSON example:

{ "filters": [ { "field": "Name", "operator": "contains", "value": "John" }, { "filters": [ { "field": "Age", "operator": "gt", "value": 20 }, { "field": "Age", "operator": "lt", "value": 85 } ], "logic", "and" }, { "field": "Address", "operator": "doesnotcontain", "value": "street" } ], "logic": "or" } 

Filters Java

 public class Filters { private List<Filter> filters; private String logic; // accessors/mutators/toString } 

Filter.java

 public class Filter { private String field; private String operator; private String value; // accessors/mutators/toString } 

Unit test

 public class KendoGridFilterTest { private ObjectMapper mapper; @Before public void before() { mapper = new ObjectMapper(); } @Test public void jsonParseTest() { final String json = "{\"filters\":[{\"field\":\"Name\",\"operator\":\"contains\",\"value\":\"John\"},{filters: [{\"field\":\"Age\",\"operator\": \"eq\",\"value\": 85},{\"field\": \"Age\",\"operator\": \"eq\",\"value\": 85}]\"logic\", \"and\",},{\"field\": \"Address\",\"operator\": \"doesnotcontain\",\"value\": \"street\"}],\"logic\":\"or\"}"; Filters filters = mapper.readValue(json, Filters.class); assertTrue(json.equals(filters.writeValueAsString(filters); } } 

Mistakes

  com.fasterxml.jackson.databind.UnrecognizedPropertyException: Unrecognized field 
     'logic' (com.example.Filter) not market as ignorable (3 known properties 
     "value", "field", "operator")
 at [Source: java.io.StringReader@3bb2b8 ;  line: 1, column 76] (through reference 
     chain: com.example.Filters ["filters"] -> com.example.Filter ["logic"]

I also tried adding @JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property="@id") to the Filters class and getting the same errors.

+4
source share
1 answer

Your filter class is invalid. It should expand the filters. After fixing your unit test (json is wrong), it can load your json into the Filters object.

 public class Filter extends Filters { private String field; private String operator; private String value; // accessors/mutators/toString } 
+2
source

Source: https://habr.com/ru/post/1491187/


All Articles