I have a class Foo which has a variable Map
bar. But as a requirement, I need to be able to represent it as an Object.
So I use a GenericXmlAdapter which in turn does an instanceof check and invokes a Map adapter. A MapAdapter converts the Map into a MapElementList which is just a List of the elements of the Map.
The code falls through as required i.e. the GenericXmlAdapter is invoked and it returns the MapElementList.
But then I get the infamous class MapElementList nor any of its super class is known to this context error
Partial headway: The MapAdapter is not referenced in Foo or any other class which needs to be serialized. So the @XmlSeeAlso in the MapAdapter will not help. I need to add it elsewhere. Where? is the question. Adding it in the GenericXmlAdapter is not helping.
A quick run through the code
class Foo{
private Object bar;
@XmlJavaAdapter(GenericAdapter.class)
@XmlElement(name="bar")
public Object getBar(){
return bar;
}
}
The following GenericAdapter is invoked by Jackson as expected
class GenericAdapter extends XmlAdapter<Object,Object>{
public Object marshall(Object v){
final XmlAdapter adapter = getAppropriateAdapter(v);
return adapter.marshall(v);
}
public Object unmarshall(Object v){
final XmlAdapter adapter = getAppropriateAdapter(v);
return adapter.unmarshall(v);
}
public XmlAdapter getAppropriateAdater(Object v){
if(v instanceof Map){
return MapAdapter();
}
}
}
The following MapAdapter is being invoked by the GenericAdapter as expected
@XmlSeeAlso({ MapElement.class, MapElementList.class })
class MapAdapter<K,V> extends XmlAdapter<MapElementList<K,V>, Map<K,V>>{
public MapElementList<K,V> marshall(Map<K,V> map){
return new MapElementList(map);
}
public Map<K,V> unmarshall(MapElementList list){
...
}
}
The following MapElementList is a list representation of a Map
class MapElementList<K,V>{
@XmlAnyElement
private ArrayList<MapElement<K,V> list;
//Getters and setters
}
The following is the MapElement finally
class MapElement<K,V>{
@XmlAnyElement
private K key;
@XmlAnyElement
private V value;
//Getters and setters WITHOUT any annotations
}