java - Jackson ObjectMapper - readValue fails when target object has a Map field -
i guess might have been asked earlier, not find looking for. here goes.
my json looks -
{ "verticals": [ { "name": "vertical1", "icon": "icon1", "children": [] }, { "name": "vertical2", "icon": "icon2", "children": [] } ], "config": { "vertical1": [ { "title": "section1", "icon": "icon1", "prefs": [ { "type": "type1", "opts": [ { "name": "pref_store_key", "value": "filter-prefix-food" } ], "icons": [ "icon3", "icon4" ] }, { "type": "type2", "opts": [ { "name": "pref_store_key", "value": "filter-prefix-cue" } ], "icons": [ "icon5", "icon6" ] } ] } ], "vertical2": [ { "title": "section1", "icon": "icon1", "prefs": [ { "type": "type1", "opts": [ { "name": "pref_store_key", "value": "filter-prefix-food" } ], "icons": [ "icon3", "icon4" ] }, { "type": "type2", "opts": [ { "name": "pref_store_key", "value": "filter-prefix-cue" } ], "icons": [ "icon5", "icon6" ] } ] } ] }
}
my classes -
public class featureconfigdata { private map<string, list<itemconfig>> config = null; private list<vertical> verticals = null; public list<itemconfig> getitemconfig(string vertical) { if (config == null) { return null; } else { return config.get(vertical); } } public list<vertical> getverticals() { return verticals; } public map<string, list<itemconfig>> getconfig() { return config; } public void setconfig(map<string, list<itemconfig>> config) { this.config = config; } } public class itemconfig { private string title = null; private string icon = null; private list<pref> prefs = null; public static class pref { private string type = null; private list<string> icons = null; private list<opt> opts = null; public string gettype() { return type; } public list<string> geticons() { return icons; } public list<opt> getopts() { return opts; } } public static class opt { private string name; private string value; public string getname() { return name; } public string getvalue() { return value; } } public string gettitle() { return title; } public string geticon() { return icon; } public list<pref> getprefs() { return prefs; } }
i de-serializing json string -
objectmapper.configure(deserializationfeature.fail_on_unknown_properties, false) .enabledefaulttyping(objectmapper.defaulttyping.non_final); featureconfigdata config = objectmapper.readvalue(json, featureconfigdata.class);
why config field coming out null in case , not getting populated? need else ensure jackson understands map keys "vertical1", "vertical2" etc?
note, configuration drives actual name/number of verticals. it's not option replace map> class fields vertical1, vertical2 etc.
Comments
Post a Comment