java - Jackson serialization of Map of objects -


i'm attempting use jackson serialize , deserialize map containing arbitrary objects. according docs i've read should able use enabledefaulttyping() tell jackson store info need in serialization, , have written following simple test:

  @test   public void testobjectmap()   {     final objectmapper mapper = new objectmapper().enabledefaulttyping();      final map<string, object> map = maps.newhashmap();     map.put("test1", new date());     map.put("test2", new inetsocketaddress(10));      try     {       final string ser = mapper.writevalueasstring(map);       system.err.println(ser);       final map<string, object> deser = this.mapper.readvalue(ser, new typereference<map<string, object>>(){});       asserttrue(deser.get("test1") instanceof date);       asserttrue(deser.get("test2") instanceof inetsocketaddress);     }     catch (final ioexception e)     {       fail("failed", e);     }   } 

the serialization looks okay, in system.err.println(ser) line in above code produces output:

{"test1":["java.util.date",1408312196267],"test2":["java.net.inetsocketaddress","0.0.0.0:10"]} 

but deserialization attempt fails following error:

caused by: com.fasterxml.jackson.databind.jsonmappingexception: unexpected token (start_object), expected start_array: need json array contain as.wrapper_array type information class java.util.map  @ [source: {"test1":["java.util.date",1408312196267],"test2":["java.net.inetsocketaddress","0.0.0.0:10"]}; line: 1, column: 1]  @ com.fasterxml.jackson.databind.jsonmappingexception.from(jsonmappingexception.java:164)  @ com.fasterxml.jackson.databind.deserializationcontext.wrongtokenexception(deserializationcontext.java:841)  @ com.fasterxml.jackson.databind.jsontype.impl.asarraytypedeserializer._locatetypeid(asarraytypedeserializer.java:122)  @ com.fasterxml.jackson.databind.jsontype.impl.asarraytypedeserializer._deserialize(asarraytypedeserializer.java:93)  @ com.fasterxml.jackson.databind.jsontype.impl.asarraytypedeserializer.deserializetypedfromobject(asarraytypedeserializer.java:58)  @ com.fasterxml.jackson.databind.deser.std.mapdeserializer.deserializewithtype(mapdeserializer.java:342)  @ com.fasterxml.jackson.databind.deser.impl.typewrappeddeserializer.deserialize(typewrappeddeserializer.java:41)  @ com.fasterxml.jackson.databind.objectmapper._readmapandclose(objectmapper.java:3051)  @ com.fasterxml.jackson.databind.objectmapper.readvalue(objectmapper.java:2153)  @ test.jacksonmoduletest.testobjectmap(jacksonmoduletest.java:119) 

i'm unsure jackson complaining here; can assume expects map have sort of typing i'm unsure how provide this.

i'm using jackson 2.4.0

you should take @ how convert json string map<string, string> jackson json there following code :

jsonfactory factory = new jsonfactory();  objectmapper mapper = new objectmapper(factory);  typereference<hashmap<string,object>> typeref          = new typereference<hashmap<string,object>>() {};  hashmap<string,object> o = mapper.readvalue(inputfile, typeref);  

maybe fact used abstract class map instead of concrete implementation (hashmap in above code) explains jackson has difficulties when trying fill structure...


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -