java - Jackson read json in generic List -


i'm using jackson in order read json messages. 1 of values i' trying parse list , value contains type of data in list. structure 've created in java.

public class message<t> {    private timestamp time;    private restaction action;    private string type;    private list<t> data; } 

through class.forname(); can class represents data in list. question how can read list.

if need map incoming json list can this

string jsonstring = ...; //your incoming json string objectmapper mapper = new objectmapper(); class<?> clz = class.forname(yourtypestring); javatype type = mapper.gettypefactory().constructcollectiontype(list.class, clz); list <t> result = mapper.readvalue(jsonstring, type); 

edit

something this, completly untested , never done

public message<t> deserialize(jsonparser jsonparser, deserializationcontext arg1)     throws ioexception, jsonprocessingexception {     objectmapper mapper = new objectmapper();     objectcodec oc = jsonparser.getcodec();     jsonnode node = oc.readtree(jsonparser);      jsonnode timestamp = node.get("time");     timestamp time = mapper.readvalue(timestamp, timestamp.class);     jsonnode restaction = node.get("action");     restaction action = mapper.readvalue(restaction, restaction.class);     string type = node.get("type").gettextvalue();     class<?> clz = class.forname(type);     jsonnode list = node.get("data");     javatype listtype = mapper.gettypefactory().constructcollectiontype(list.class,   clz);     list <t> data = mapper.readvalue(list, listtype);      message<t> message = new message<t>;     message.settime(time);     message.setaction(action);     message.settype(type);     message.setdata(data);      return message; } 

Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

php - joomla get content in onBeforeCompileHead function -