how to get custom made object from ObjectMapper in java -


i doing following:

ironrunid id = new ironrunid("runobject", "runid1", 4); objectmapper mapper = new objectmapper(); hashmap<string, object> map = new hashmap<string, object>(); map.put("runid", id); string json = mapper.writevalueasstring(map);  objectmapper mapper = new objectmapper(); map<string, object> map1 = mapper.readvalue(json, new typereference<map<string, object>>() {}); ironrunid runid = (ironrunid) (map1.get("runid")); 

but gives me error: cannot cast java.util.linkedhashmap ironrunid why object returned map.get() of type linkedhashmap?

on contrary, if do:

list<object> mylist = new arraylist<object>(); mylist.add("jonh"); mylist.add("jack"); map.put("list", mylist); 

then object returned map.get() after doing mapper.readvalue of type arraylist.

why difference? inserting default types map returns correct object. inserting custom made object in map not.

does know how this?

map<string, object> map1 = mapper.readvalue(json, new typereference<map<string, object>>() {}); 

basically translated to, return me map keys of type string , values of type object. so, jackson gave keys of type string , values of type object. jackson doesn't know custom object, thats why gave own native bound object map, specifically, linkedhashmap, , reason why getting linkedhashmap when doing get returned map

so change :

map<string, ironrunid> map1 = mapper.readvalue(json, new typereference<map<string, ironrunid>>() {}); 

also, practice declare object of interface type concrete type. instead of

hashmap<string, object> map = new hashmap<string, object>();

make it

map<string, object> map = new hashmap<string, object>();

edit

as response added questions, can create wrapper object handle objects. this.

class wrapper{      private ironrunid ironrunid;     private long time;     private map<string, string> aspects;     private string anotherstring;      public long gettime() {         return time;     }     public void settime(long time) {         this.time = time;     }     public map<string, string> getaspects() {         return aspects;     }     public void setaspects(map<string, string> aspects) {         this.aspects = aspects;     }     public string getanotherstring() {         return anotherstring;     }     public void setanotherstring(string anotherstring) {         this.anotherstring = anotherstring;     }     public ironrunid getironrunid() {         return ironrunid;     }     public void setironrunid(ironrunid ironrunid) {         this.ironrunid = ironrunid;     } } 

you can store different objects in class.

revised version

public static void main(string[] args) throws jsongenerationexception, jsonmappingexception, ioexception{     ironrunid id = new ironrunid("runobject", "runid1", 4);     map<string, string> aspects = new hashmap<string, string>();     aspects.put("aspectskey1", "aspectsvalue1");     aspects.put("aspectskey2", "aspectsvalue2");     aspects.put("aspectskey3", "aspectsvalue3");     string anotherstring = "anotherstring";     long time = 1l;      wrapper objectwrapper = new wrapper();     objectmapper objectmapper = new objectmapper();      objectwrapper.setironrunid(id);     objectwrapper.settime(time);     objectwrapper.setanotherstring(anotherstring);     objectwrapper.setaspects(aspects);      map<string, wrapper> map = new hashmap<string, wrapper>();     map.put("thewrapper", objectwrapper);     string json = objectmapper.writevalueasstring(map);      map<string, wrapper> map1 = objectmapper.readvalue(json, new typereference<map<string, wrapper>>() {});     wrapper wrapper = map1.get("thewrapper");      system.out.println("run id : " +  wrapper.getironrunid().tostring());     system.out.println("time : " + wrapper.gettime());     system.out.println("aspects : " + wrapper.getaspects().tostring());     system.out.println("anotherstring : " + wrapper.getanotherstring()); } 

Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -