Java - Jackson nested arrays -


given following data

{    "version" : 1,    "data" : [ [1,2,3], [4.5,6]] } 

i tried following definitions , used objectmapper.readvalue(jsonstring, outer.class)

class outer {   public int version;   public list<inner> data }  class inner {    public list<integer> intlist; } 

i got:

can not deserialize instance of inner out of start_array token"

in outer class, if

list<list<integer> data; 

then deserialization works.

but in code, outer , inner classes have business logic related methods , want retain class stucture.

i understand issue jackson unable map inner array 'inner' class. have use tree model in jackson? or there someway can still use datamodel here ?

jackson needs know how create inner instance array of ints. cleanest way declare corresponding constructor , mark the @jsoncreator annotation.

here example:

public class jacksonintarray {     static final string json = "{ \"version\" : 1, \"data\" : [ [1,2,3], [4.5,6]] }";      static class outer {         public int version;         public list<inner> data;          @override         public string tostring() {             return "outer{" +                     "version=" + version +                     ", data=" + data +                     '}';         }     }      static class inner {         public list<integer> intlist;          @jsoncreator         public inner(final list<integer> intlist) {             this.intlist = intlist;         }          @override         public string tostring() {             return "inner{" +                     "intlist=" + intlist +                     '}';         }     }      public static void main(string[] args) throws ioexception {         final objectmapper mapper = new objectmapper();         system.out.println(mapper.readvalue(json, outer.class));     } 

output:

outer{version=1, data=[inner{intlist=[1, 2, 3]}, inner{intlist=[4, 6]}]} 

Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -