1

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
}
Vikram Bodicherla
  • 7,133
  • 4
  • 28
  • 34
  • Exactly why do you need the complexity here? If property was of type `Map`, Jackson would handle it just fine. Is there a requirement for method to expose it as `Object'? – StaxMan Apr 09 '12 at 17:18
  • 1
    @VikramBodicherla: Do you use JAXB annotations with JAXB itself, or you use them with Jackson? If first case, then remove the `jackson` tag. If not... `@XmlAdapter` is not in the [list of supported JAXB annotations for Jackson](http://wiki.fasterxml.com/JacksonJAXBAnnotations#Supported_JAXB_annotations). – dma_k Apr 09 '12 at 20:33
  • StaxMan Unfortunately, we need this to provide for scope in the schema. @dma_k, I use the JAXB annotations for Jackson. And guys, I figured out the issue, thought I still don't have a good solution. I put in a dirty hack which works for now, I'll post it here. – Vikram Bodicherla Apr 10 '12 at 05:43

0 Answers0