java - Jackson Deserialization: How to map specific properties to getters and setters as well as loading all the properties into map of a same POJO? -
i need deserialize json string pojo class following scenario:
- some basic json properties should mapped getters , setters fields in pojo class.
- and again json properties should loaded map filed of same pojo class.
for example have following json string:
"entry":[ { "id": "1", "name": "type", "type": "general", "updated": "tue, 12 aug 2014 05:24:01.397z", "summary": { "lang": "en", "value": "testing content" } }, { "id": "1", "name": "type", "type": "general", "updated": "tue, 12 aug 2014 05:24:01.397z", "summary": { "lang": "en", "value": "testing content" } } ]
now need deserialize above json string following pojo class:
public class entrytype { private string id; private string name; private string type; private string updated; private map<string,object> alljsonproperties; public string getid() { return id; } public void setid(string id) { this.id = id; } public string getname() { return name; } public void setname(string name) { this.name = name; } public string gettype() { return type; } public void settype(string type) { this.type = type; } public string getupdated() { return updated; } public void setupdated(string updated) { this.updated = updated; } public map<string, object> getalljsonproperties() { return alljsonproperties; } public void setalljsonproperties(map<string, object> alljsonproperties) { this.alljsonproperties = alljsonproperties; } }
i able map id,name,type,updated appropriate pojo class fields. don not know how load id,name,type,updated,summary properties again map(alljsonproperties) field. can 1 guide me how work out this?.
note: use jackson api version 2.2.2
you can consider deserialising object twice. firstly, create instance using constructor map parameter marked @jsoncreator
annotation. secondly, populate newly created instance json values using objectmapper.readerforupdating
method.
here example:
public class jacksonforupdate { public static class bean { private string name; private map<string, object> properties; public void setname(string name) { this.name = name; } @jsoncreator public bean(map<string, object> properties) { this.properties = properties; } @override public string tostring() { return "bean{" + "name='" + name + '\'' + ", properties=" + properties + '}'; } } public static void main(string[] args) throws ioexception { objectmapper mapper = new objectmapper(); final string json = "{\"name\":\"value\"}"; bean object = mapper.readvalue(json, bean.class); system.out.println(mapper.readerforupdating(object).readvalue(json)); } }
output:
bean{name='value', properties={name=value}}
update: solution above wont work deserialisation of array of objects. 1 possible workaround construct object out of jsonnode
, set properties map using objectmapper.convertvalue
conversion, , populate field value before.
note objectmapper
needs accessible inside bean class can done using the jackson value injection.
here example:
public class jacksonforupdate2 { public static class bean { private string name; private map<string, object> properties; public void setname(string name) { this.name = name; } @jsoncreator public bean(jsonnode node, @jacksoninject objectmapper mapper) throws ioexception { this.properties = mapper.convertvalue(node, map.class); mapper.readerforupdating(this).readvalue(node); } @override public string tostring() { return "bean{" + "name='" + name + '\'' + ", properties=" + properties + '}'; } } public static void main(string[] args) throws ioexception { objectmapper mapper = new objectmapper(); // make mapper available bean mapper.setinjectablevalues(new injectablevalues.std().addvalue(objectmapper.class, mapper)); final string json = "[{\"name\":\"value\"},{\"name\":\"value2\"}]"; system.out.println( mapper.readvalue(json, new typereference<list<bean>>() {})); } }
output:
[bean{name='value', properties={name=value}}, bean{name='value2', properties={name=value2}}]
this doesn't longer elegant though.
Comments
Post a Comment