Re: Example of JAXB and HashMap serialization?
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 MapString, String a = new HashMapString, String(); public MapString, String getA() { return this.a; } } Now we need to implement MapAdaptor, which maps the Map to something JAXB understands: public class MapAdaptor extends XmlAdapterMyMap, MapString,String { @Override public MyMap marshal(MapString,String v) throws Exception { MyMap myMap = new MyMap(); ListA aList = myMap.getA(); for ( Map.EntryString,String e : v.entrySet() ) { aList.add(new A(e.getKey(), e.getValue())); } return myMap; } @Override public MapString,String unmarshal(MyMap v) throws Exception { MapString,String map = new HashMapString,String(); for ( A e : v.getA() ) { map.put(e.getKey(), e.getValue()); } return map; } } So that maps MapString, 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 ListA a = new ArrayListA(); public ListA 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
Re: Example of JAXB and HashMap serialization?
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
Re: Example of JAXB and HashMap serialization?
| --| | | 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
Example of JAXB and HashMap serialization?
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
RE: Example of JAXB and HashMap serialization?
Great, thank you for the clarification - I hope 'supposedly' turns into 'definitely' ;-). We can hold out for the maturation of the trunk and some examples to bubble up before diving in. Regards, Kaleb | | From: | | --| |Benson Margulies [EMAIL PROTECTED] | --| | | To:| | --| |cxf-user@incubator.apache.org | --| | | Date: | | --| |11/28/2007 12:55 PM | --| | | Subject: | | --| |RE: Example of JAXB and HashMap serialization? | --| It can't be done well with the JAXB we have in 2.0.x, I am told. Aegis does it nicely. The trunk uses new stuff in JAXB, which supposedly supports it, but I don't have any experience. -Original Message- From: Kaleb Walton [mailto:[EMAIL PROTECTED] Sent: Wednesday, November 28, 2007 10:14 AM To: cxf-user@incubator.apache.org Subject: Example of JAXB and HashMap serialization? 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
RE: Example of JAXB and HashMap serialization?
It can't be done well with the JAXB we have in 2.0.x, I am told. Aegis does it nicely. The trunk uses new stuff in JAXB, which supposedly supports it, but I don't have any experience. -Original Message- From: Kaleb Walton [mailto:[EMAIL PROTECTED] Sent: Wednesday, November 28, 2007 10:14 AM To: cxf-user@incubator.apache.org Subject: Example of JAXB and HashMap serialization? 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
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