json - Can Jackson handle serialize and deserialize to java Object type -


i have structure

 list<map<string, object>> 

or can wrap it

   public static  class testwrapper {     list<map<string, object>> list;      public testwrapper() {     }      public list<map<string, object>> getlist() {         return list;     }      public void setlist(list<map<string, object>> list) {         this.list = list;     }    } 

how jackson know deserialize value type of objects map value? if configurable, how config using annotaions?

let me explain how works using below classes , example json string. let assume, have 2 classes below:

class testwrapper<t> {      private list<map<string, t>> list = new arraylist<map<string, t>>();      public list<map<string, t>> getlist() {         return list;     }      public void setlist(list<map<string, t>> list) {         this.list = list;     } }  class entity {      private long id;      public long getid() {         return id;     }      public void setid(long id) {         this.id = id;     } } 

and below json string input:

{   "list" : [ {     "entity" : {       "id" : 0     }   } ] } 

now when try deserialize above json using below code see jackson deserialize unknown type map class:

testwrapper<?> result = mapper.readvalue(json, testwrapper.class); system.out.println(result.getlist().get(0).get("entity").getclass()); 

above program prints:

class java.util.linkedhashmap 

if want define type of map value, have build typefactory , provide json deserialization process.

typefactory typefactory = mapper.gettypefactory(); javatype type = typefactory.constructparametrictype(testwrapper.class, entity.class);  testwrapper<entity> result = mapper.readvalue(json, type); system.out.println(result.getlist().get(0).get("entity").getclass()); 

above program prints:

class your.package.entity 

as can see, can provide generic type providing complex javatype object deserialization process.


Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

c++ - End of file on pipe magic during open -