java - Generic Map<String, Object> JSON marshalling and unmarshalling with Moxy -
is there way marshal map<string, object>
moxy json result uses natural constructs of json?
is, keys strings , possible values following rules apply (possibly not complete set):
number
(e.g.,integer
) becomes json number (or string if it's big)string
becomes json stringset
, array,iterable
become json array- and
map<string, object>
, same rules recursively applied - any other object marshalled in natural moxy way
there example how marshal map<string, integer>
, other specific maps using @xmlvariablenode
(see http://blog.bdoughan.com/2013/06/moxys-xmlvariablenode-using-maps-key-as.html?m=1), unable extend idea point subtypes can inserted value.
note moxy should able unmarshal json original map.
jackson capable of doing default.
i've tried want jaxb ri (xml):
@xmlrootelement public class foo { @xmljavatypeadapter(mapadapter.class) public map<string, object> map; } public class mapadapter extends xmladapter<mapentry[], map<string, object>> { @override public map<string, object> unmarshal(mapentry[] v) throws exception { map<string, object> map = new hashmap<>(); (mapentry me : v) map.put(me.key, me.value); return map; } @override public mapentry[] marshal(map<string, object> v) throws exception { mapentry[] mes = new mapentry[v.size()]; int = 0; (map.entry<string, object> entry : v.entryset()) mes[i++] = new mapentry(entry.getkey(), entry.getvalue()); return mes; } } public class mapentry { public string key; public object value; public mapentry() {} public mapentry(string key, object value) { this.key = key; this.value = value; } }
unfortunately moxy has bug ( https://bugs.eclipse.org/bugs/show_bug.cgi?id=465014 ) , not able handle this.
if need moxy have use approach described in blaise's blog:
@xmlrootelement public class moxyfoo { @xmljavatypeadapter(moxymapadapter.class) public map<string, object> map = new hashmap<>(); } public class moxymapadapter extends xmladapter<moxymapadapter.adaptedmap, map<string, object>> { @override public adaptedmap marshal(map<string, object> map) throws exception { adaptedmap adaptedmap = new adaptedmap(); (entry<string, object> entry : map.entryset()) { adaptedentry adaptedentry = new adaptedentry(); adaptedentry.key = entry.getkey(); adaptedentry.value = entry.getvalue(); adaptedmap.entries.add(adaptedentry); } return adaptedmap; } @override public map<string, object> unmarshal(adaptedmap adaptedmap) throws exception { list<adaptedentry> adaptedentries = adaptedmap.entries; map<string, object> map = new hashmap<>(adaptedentries.size()); (adaptedentry adaptedentry : adaptedentries) { map.put(adaptedentry.key, adaptedentry.value); } return map; } public static class adaptedmap { @xmlvariablenode("key") list<adaptedentry> entries = new arraylist<>(); } public static class adaptedentry { @xmltransient public string key; @xmlvalue public object value; } }
it's working fine xml, not json. json marshalling working now. i've filled bug fix unmarshalling: https://bugs.eclipse.org/bugs/show_bug.cgi?id=465016 .