Hi, Thanks for your suggestion.
Though, I'm afraid that in this case the variety of SomeClass1, SomeClass2, etc have no interface or superclass in common. They are of different nature. Also, the classes from AnyOtherClass can be pretty much anything, but they're assumed to be xml annotated, or of primitive types. Thanks, Vasco Sergey Beryozkin wrote: > Is there any chance that all of those classes you deal with > extend/implement some common class/interface, say SomeClass is what > they all extend ? > If it were the case then may be you can do a wildcars extension in > your Map adapter, while in SomeClass adapter you can create an > instance reflectively, using a constructor which accepts String or > valueOf method, etc.. ? > > Cheers, Sergey > > > ----- Original Message ----- From: "Vasco Asturiano" <[email protected]> > To: <[email protected]> > Sent: Friday, December 19, 2008 4:37 PM > Subject: Re: HashMap with XmlJavaTypeAdapter key/value > > >> Hi, >> >> It is JAXWS. Please excuse the omission. :) >> >> Vasco >> >> Sergey Beryozkin wrote: >>> Hi >>> >>> Is it JAXWS or JAXRS ? >>> >>> Cheers, Sergey >>> >>> ----- Original Message ----- From: "Vasco Asturiano" <[email protected]> >>> To: <[email protected]> >>> Sent: Friday, December 19, 2008 3:45 PM >>> Subject: HashMap with XmlJavaTypeAdapter key/value >>> >>> >>>> 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 >>>> >>> >> >> >> -- >> >> Vasco Asturiano >> ------------------------- >> Information Services Dept. >> RIPE NCC >> http://www.ripe.net >> > > -- Vasco Asturiano ------------------------- Information Services Dept. RIPE NCC http://www.ripe.net
