javascript - Java Jackson: deserialize complex polymorphic object model: JsonMappingException: Unexpected token (START_OBJECT), expected VALUE_STRING -


i have tree of objects

a

b extends a

c extends b

d extends b

e extends c

f extends , has 1 reference a

a has following annotation

@jsontypeinfo(use=jsontypeinfo.id.class,include=jsontypeinfo.as.property,property="@class")

if try deserialize json array of objects extends a, throws following error

org.codehaus.jackson.map.jsonmappingexception: unexpected token (start_object), expected value_string: need json string contains type id (for subtype of java.util.collection)

the json string generated tostring() method of set , set parametric type serialized in json following code:

objectmapper objectmapper=new objectmapper();         objectmapper.enabledefaulttyping(objectmapper.defaulttyping.non_concrete_and_arrays);         string res="";         try {             res = objectmapper.writerwithdefaultprettyprinter().writevalueasstring(t);         } catch (jsongenerationexception e) {             e.printstacktrace();         } catch (jsonmappingexception e) {             e.printstacktrace();         } catch (ioexception e) {             e.printstacktrace();         }         return res; 

the code deserialize json array (that set described above) is:

objectmapper mapper = new objectmapper();           mapper.enabledefaulttyping(objectmapper.defaulttyping.non_concrete_and_arrays);         collection<t> results=null;         try {             results =  mapper.readvalue(json, typefactory.defaultinstance().constructparametrictype(collection.class, clazz ) );         } catch (jsonparseexception e) {             // todo auto-generated catch block             e.printstacktrace();         } catch (jsonmappingexception e) {             // todo auto-generated catch block             e.printstacktrace();         } catch (ioexception e) {             // todo auto-generated catch block             e.printstacktrace();         }           return results; 

the json sample parses like:

"[{   "@class" : "pack1.pack2.myclass",   "id" : null,   "f1" : "",   "f2" : 0.9933817827,   "f3" : 6.883261e-4,   "f4" : 0.001375699,   "f5" : {     "@class" : "pack1.pack2.myclass2",     "id" : null,     "f1" : "",     "f2" : 0.0,     "f3" : 0.0,     "f4" : 0.0,     "f5" : [ "java.util.hashset", [ 0 ] ],     "f6" : [ "java.util.hashset", [ 2 ] ],     "f7" : [ "java.util.arraylist", [ "scelta", "brani", "buona" ] ],     "f8" : [ null, "nom", null ],     "f9" : false   },   "f10" : [ "java.util.hashmap", {     "2" : "adj"   } ],   "f11" : [ "java.util.hashset", [ 0 ] ],   "f12" : [ "java.util.hashset", [ 2 ] ],   "f13" : [ "java.util.arraylist", [ "scelta", "brani", "buona" ] ],   "featureindicator" : false }]" 

here json string includes objects of sample of java set

i believe problem default typing. start of json not generated jackson expect default typing. begining of json should be:

["java.util.hashset", [{ 

and end should have closing bracket }]].

that's because generate json using tostring() method of set. should instead use objectmapper, that's configured default typing, so:

res = objectmapper.writerwithdefaultprettyprinter().writevalueasstring(yourset); 

Comments

  1. Javascript - Java Jackson: Deserialize Complex Polymorphic Object
    Model: Jsonmappingexception: Unexpected Token (Start_Object), Expected
    Value_String - >>>>> Download Now

    >>>>> Download Full

    Javascript - Java Jackson: Deserialize Complex Polymorphic Object
    Model: Jsonmappingexception: Unexpected Token (Start_Object), Expected
    Value_String - >>>>> Download LINK

    >>>>> Download Now

    Javascript - Java Jackson: Deserialize Complex Polymorphic Object
    Model: Jsonmappingexception: Unexpected Token (Start_Object), Expected
    Value_String - >>>>> Download Full

    >>>>> Download LINK Kv

    ReplyDelete

Post a Comment

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -