back to listing index

Re: Example of JAXB and HashMap serialization?

[web search]
Original source (www.mail-archive.com)
Clipped on: 2012-08-07

Re: Example of JAXB and HashMap serialization?

Kaleb Walton
Thu, 13 Dec 2007 06:24:59 -0800

Chris,

Thank you for the detailed example! I'll definitely look into this
implementation when the need arises in the future (right now we're getting
by without them, but I know we'll need them at some point). Glad we're not
the only ones who thought this was an important missing piece.

Regards,
Kaleb


|------------>
| From:      |
|------------>
  
>--------------------------------------------------------------------------------------------------------------------------------------------------|
  |Chris McClelland <[EMAIL PROTECTED]>                                         
                                                                  |
  
>--------------------------------------------------------------------------------------------------------------------------------------------------|
|------------>
| To:        |
|------------>
  
>--------------------------------------------------------------------------------------------------------------------------------------------------|
  |cxf-user@incubator.apache.org                                                
                                                                     |
  
>--------------------------------------------------------------------------------------------------------------------------------------------------|
|------------>
| Date:      |
|------------>
  
>--------------------------------------------------------------------------------------------------------------------------------------------------|
  |11/29/2007 07:07 PM                                                          
                                                                     |
  
>--------------------------------------------------------------------------------------------------------------------------------------------------|
|------------>
| Subject:   |
|------------>
  
>--------------------------------------------------------------------------------------------------------------------------------------------------|
  |Re: Example of JAXB and HashMap serialization?                               
                                                                     |
  
>--------------------------------------------------------------------------------------------------------------------------------------------------|




Kaleb,

This is an interesting question, and one which I have encountered twice
in the last week. I came up with a solution which works using
@XmlAdaptor (wait, don't write it off just yet...)

First a caveat: this only works if you represent the map as a *wrapped*
list of {k,v} pairs. Probably the simplest XML representation would be:

<M xmlns="http://...";>
  <MyMap>
    <A value="1" key="A"/>
    <A value="3" key="C"/>
    <A value="2" key="B"/>
  </MyMap>
</M>

By wrapped, I mean the model must be something like M=(A+), and not
M=(A+, B). I'm not saying the latter is impossible, only that I couldn't
get it to work in the time I had had on this.

So, let's look at class 'M', which is the class containing the map:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "M")
public class M {
    @XmlElement(name = "MyMap", required = true)
    @XmlJavaTypeAdapter(MapAdaptor.class)
    private final Map<String, String> a = new HashMap<String, String>();
    public Map<String, String> getA() {
        return this.a;
    }
}

Now we need to implement MapAdaptor, which maps the Map to something
JAXB understands:

public class MapAdaptor extends XmlAdapter<MyMap, Map<String,String>> {

    @Override
    public MyMap marshal(Map<String,String> v) throws Exception {
        MyMap myMap = new MyMap();
        List<A> aList = myMap.getA();
        for ( Map.Entry<String,String> e : v.entrySet() ) {
            aList.add(new A(e.getKey(), e.getValue()));
        }
        return myMap;
    }

    @Override
    public Map<String,String> unmarshal(MyMap v) throws Exception {
        Map<String,String> map = new HashMap<String,String>();
        for ( A e : v.getA() ) {
            map.put(e.getKey(), e.getValue());
        }
        return map;
    }
}

So that maps Map<String, String> to a new class, MyMap, which might look
like this:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "MyMap")
public class MyMap {
    @XmlElement(name = "A", required = true)
    private final List<A> a = new ArrayList<A>();
    public List<A> getA() {
        return this.a;
    }
}

And finally, each item in the list is represented by a new class A:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "A")
public class A {

    @XmlAttribute(name = "key", required = true)
    private final String key;
    @XmlAttribute(name = "value", required = true)
    private final String value;

    public A(String key, String value) {
        this.key = key;
        this.value = value;
    }

    public A() {
        this.key = null;
        this.value = null;
    }

    public String getKey() {
        return key;
    }

    public String getValue() {
        return value;
    }
}

This is a functional, albeit circuitous and slightly naïve (all maps are
Map<String,String>) solution to your problem. So where does that leave
us? JAXBIntros supports all these annotations except
@XmlJavaTypeAdapter. But adding support for it would be fairly trivial,
and if you are happy with the overall picture of what I describe above,
then I'll happily add support for @XmlJavaTypeAdapter to JAXBIntros and
ask the JBoss guys to commit it. Then you'd be able to get the above
scenario working even if you did not have the source to the M class.

- Chris




Kaleb Walton wrote:
> Glen,
>
> Thanks for the response. It is a JAXB specific question and yes I did
> google for both of those but they turn up pages related to an XmlAdapter
> which doesn't seem like the right solution. In addition we don't use
> annotations (we use the InlineAnnotationsReader from JBoss) so I'm not
sure
> how I'd configure the XmlAdapter anyways. I was looking for a simpler
> solution if one was available - just feeling out the CXF group to see if
> anyone else has solved this with JAXB+CXF.
>
> Regards,
> Kaleb
>
>
> |------------>
> | From:      |
> |------------>
>
>--------------------------------------------------------------------------------------------------------------------------------------------------|

>   |Glen Mazza <[EMAIL PROTECTED]>
|
>
>--------------------------------------------------------------------------------------------------------------------------------------------------|

> |------------>
> | To:        |
> |------------>
>
>--------------------------------------------------------------------------------------------------------------------------------------------------|

>   |cxf-user@incubator.apache.org
|
>
>--------------------------------------------------------------------------------------------------------------------------------------------------|

> |------------>
> | Date:      |
> |------------>
>
>--------------------------------------------------------------------------------------------------------------------------------------------------|

>   |11/28/2007 11:28 PM
|
>
>--------------------------------------------------------------------------------------------------------------------------------------------------|

> |------------>
> | Subject:   |
> |------------>
>
>--------------------------------------------------------------------------------------------------------------------------------------------------|

>   |Re: Example of JAXB and HashMap serialization?
|
>
>--------------------------------------------------------------------------------------------------------------------------------------------------|

>
>
>
>
>
> Are you sure this is a CXF/JAXB-specific question instead of a more
> generic JAXB question?  (CXF's JAXB is the same as GlassFish Metro's,
> for example.)  Does googling "JAXB" and "HashMap" get you anything
> useful?
>
> Glen
>
> Am Mittwoch, den 28.11.2007, 10:13 -0500 schrieb Kaleb Walton:
>> I'm writing documentation for how to write services and got to a point
>> where I have to say "Do not use HashMap's in any of your services as CXF
> +
>> JAXB does not support serializing HashMaps". I'd rather not have to say
>> that - are there any examples out there of how to get HashMaps to
> serialize
>> using CXF + JAXB?
>>
>> I've seen the XmlAdapter stuff out there but that doesn't suit my needs
> for
>> two reasons:
>>
>> 1) We don't use annotations
>> 2) We want to use HashMaps, not pseudo-maps
>>
>> I may be misunderstanding the solutions pointed out in the articles, if
> so
>> please help me see the light!
>>
>> Regards,
>> Kaleb
>
>

  • Reply via email to