Hello all,
I am trying to render the following attribute:
HashMap<SomeClass, AnyOtherClass> myField;
SomeClass is not my class and is not xml annotated, therefore I created
a type adapter for it, which is included in the package-info.java:
@XmlJavaTypeAdapter(value=SomeClassAdapter.class,type=SomeClass.class)
The adapter contains something like:
public class SomeClassAdapter extends XmlAdapter<String, SomeClass> {
@Override
public String marshal(SomeClass c){
return c.toString();
}
@Override
public SomeClass unmarshal(String s) {
return new SomeClass(s);
}
}
This solution works fine when rendering direct attributes, as well as
lists and arrays:
SomeClass c;
SomeClass[] arr;
List<SomeClass> lst;
But fails when rendering HashMap. The type adapter is not called when
the class is either the key or the value of a HashMap, and therefore the
attribute myField is rendered empty in the resulting XML.
After researching for existing solutions I have tried is to create a
generic parametrizable adapter for the HashMap:
public class HashMapAdapter<K, V> extends
XmlAdapter<ArrayList<MapEntry<K, V> , HashMap<K, V>>
This is the MapEntry class:
@XmlAccessorType(XmlAccessType.FIELD)
public class MapEntry<K, V> {
private K key;
private V value;
private MapEntry(K key, V value) {
this.key = key;
this.value = value;
}
public K getKey() {
return key;
}
public V getValue() {
return value;
}
}
However, this solution also doesn't work. Apparently, the key attribute
in the above class would only call the appropriate type adapter if it is
defined statically of being of that type, that is:
private SomeClass key;
Unfortunately, we have many combinations of different classes with
adapters which can be either key, or value, or both. Therefore declaring
the type statically would force us to declare an HashMap adapter for
each of the possible combinations.
Did somebody else also came across the same problem and was able to find
work around it?
Thanks plenty,
--
Vasco Asturiano
-------------------------
Information Services Dept.
RIPE NCC
http://www.ripe.net