java - Using Jackson annotations with inherited class -
i'm developing android app i'm deserializing json jackson annotation api.
it worked until tried include androidactive orm, required pojo inherit model class (https://github.com/pardom/activeandroid/blob/master/src/com/activeandroid/model.java).
my json deserialized in asynctask such :
reader reader = new inputstreamreader(url.openstream()); try { objectmapper mapper = new objectmapper(); mapper.configure(deserializationfeature.fail_on_unknown_properties, false); rootjsonobj = mapper.readvalue(reader, mypojo.class); } catch (jsongenerationexception e) { e.printstacktrace(); }
a quick @ pogo :
@table(name = "rootrecipes") //androidactive annotation public class rootrecipes extends model { @jsonproperty("deleted") //jackson annotation @column(name = "deleted") //androidactive annotation public arraylist<number> deleted; @jsonproperty("meta") @column(name = "meta") public meta meta; @jsonproperty("objects") @column(name = "objects") public arraylist<objects> objects;
the json large more 2mo structure following :
{"deleted": [107981, 107982, 107995, 107999, 108012, 108014], "meta": {"is_anonymous": true, "latest": 1405555349, "limit": 1000, "next": null, "offset": 0, "page": 1, "pages": 1, "previous": null, "total_count": 20}, "objects": [<more objects>]}
the error given me :
com.fasterxml.jackson.databind.jsonmappingexception: instantiation of [simple type, class com.example.app.json.mypojo] value failed: null
as removed inheritance model parsing working normally. can't figure out reason of error.
thanks.
in opinion should created new pojo
classes working json
decoupled android world(i mean, can not have properties, parents come android packages). this:
class jsonrootrecipes { @jsonproperty("deleted") public list<number> deleted; @jsonproperty("meta") public jsonmeta meta; @jsonproperty("objects") public list<object> objects; // getters, setters, tostring } class jsonmeta { @jsonproperty("is_anonymous") private boolean anonymous; // getters, setters, tostring } // pojos decoupled android classes.
now, have create service class able parse json
, convert jsonpojo
classes android pojo
classes. pseudocode:
class jsonservice { public rootrecipes parsejson(json) throws ioexception { objectmapper mapper = new objectmapper(); mapper.disable(deserializationfeature.fail_on_unknown_properties); jsonrootrecipes jsonrootrecipes = mapper.readvalue(json, jsonrootrecipes.class); rootrecipes rootrecipes = null; //convert jsonrootrecipes rootrecipes return rootrecipes; } }
Comments
Post a Comment