java - Unmarshall JSON to subtypes with different field type jackson -


i receive json array of objects have field content, type of field may differ:

[     {         "id": "primarybodyheader",         "type": "richtext",         "content": "<h1>alice's adventures in wonderland</h1>"     },     {         "id": "1027",         "type": "richtext",         "content": {             "value": "rvmtmtk=",             "contenttype": "dynamiccontent"         }     } ] 

and have bean:

public abstract class landingpagecontentitem {     private string id;     private string type;     private string content; } 

at least want map content text field when text (null non-text content)

at most, want map different kinds of items different subclasses according type of field content - textcontentitem, complexcontentitem or so. @jsonsubtypes can't this

is there way without custom deserializer?

if don't know (or have no control of) might in content field i'd suggest map raw com.fasterxml.jackson.databind.jsonnode this

public static class landingpagecontentitem {     private final string id;     private final string type;     private final jsonnode content;      @jsoncreator     public landingpagecontentitem(             @jsonproperty("id") final string id,              @jsonproperty("type") final string type,              @jsonproperty("content") final jsonnode content) {         this.id = id;         this.type = type;         this.content = content;     }      /* logic here */ } 

and can read

objectmapper mapper = new objectmapper(); list<landingpagecontentitem> items =      mapper.readvalue(node, new typereference<list<landingpagecontentitem>>() {}); 

later on can verify if jsonnode of expected type.

if (content.istextual()) {     // content.astext();  } 

Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -