java - Jackson Json with nested parametric classes -


listing:

import java.util.list;  public class listing<t> {     list<thing<t>> children;      public list<thing<t>> getchildren() {         return children;     }      public void setchildren(list<thing<t>> children) {         this.children = children;     } } 

thing:

public class thing<t> {      private string type;     private t data;      public t getdata() {         return data;     }      public void setdata(t data) {         this.data = data;     }      public string gettype() {         return type;     }      public void settype(string type) {         this.type = type;     } } 

link:

public class link {     private string author;      public string getauthor() {         return author;     }      public void setauthor(string author) {         this.author = author;     } } 

and here's example of serialization , deserialization...

public static void main(string[] args) throws ioexception {     link link1 = new link();     link1.setauthor("johndoe");      link link2 = new link();     link2.setauthor("maryjane");      list<thing<link>> things = new arraylist<thing<link>>();      thing<link> thing1 = new thing();     thing1.setdata(link1);     thing1.settype("t3");      thing<link> thing2 = new thing();     thing2.setdata(link2);     thing2.settype("t3");      things.add(thing1);     things.add(thing2);      listing<link> listing = new listing<link>();     listing.setchildren(things);       thing<listing> thing = new thing<listing>();     thing.settype("listing");     thing.setdata(listing);      file jsonfile = new file("src/testmap.txt");     objectmapper mapper = new objectmapper();     mapper.writevalue(jsonfile, thing);      //string jsonstring = "{\"type\":\"listing\",\"data\":{\"children\":[{\"type\":\"t3\",\"data\":{\"author\":\"johndoe\"}},{\"type\":\"t3\",\"data\":{\"author\":\"maryjane\"}}]}}";     javatype jsontype = mapper.gettypefactory().constructparametrictype(thing.class, listing.class);     thing<listing> readthing = mapper.readvalue(jsonfile, jsontype);   } 

the problem i'm having things contained in listing in sample code above not parametrized link, data field returned object (which linkedhashmap).

i want able this:

list<thing<link>> readlistingchildren = readthing.getdata().getchildren(); string author = readlistingchildren.get(0).getdata().getauthor(); 

my question is, how work using jackson json?

note: there multiple different types of objects contained things, , thing's data member's type defined (or should defined) data object's "type" field, using strings such t1, t2, t3, etc. map different classes.

to achieve serialized string like

{     "data":{         "type":"listing",         "children":[             {                     "data":{                     "type":"t3",                     "author":"johndoe"                 }             },             {                 "data":{                     "type":"t3",                     "author":"maryjane"                 }             }         ]     } } 

and use type information correctly deserialize concrete class may use

@jsontypename("listing") public class listing<t> {     list<thing<t>> children;      public list<thing<t>> getchildren() {         return children;     }      public void setchildren(final list<thing<t>> children) {         this.children = children;     } } 

public class thing<t> {     private t data;      @jsontypeinfo(use = jsontypeinfo.id.name, include = jsontypeinfo.as.property, property = "type")     @jsonsubtypes({       @jsonsubtypes.type(link.class),       @jsonsubtypes.type(listing.class)     })     public t getdata() {         return data;     }      public void setdata(final t data) {         this.data = data;     } } 

@jsontypename("t3") public class link {     private string author;      public string getauthor() {         return author;     }      public void setauthor(final string author) {         this.author = author;     } } 

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -