Hey Jörg,
thanks for the quick answer. I wrote a unit test and it worked for me as
well. Now, I digged into it and found the reason for this strange
behavior. I had another class that was used prior to the array container
for primitives annotated with @XStreamAlias("short").
So, I assume that this changed the way, how short primitives were
handled in general. I changed it to @XStreamAlias("shortprimitive") and
it worked.
Thanks again,
Dirk
On Wed, 2013-10-23 at 20:05 +0200, Jörg Schaible wrote:
> Hi Dirk,
>
> Dirk Schnelle-Walka wrote:
>
> > Hey there,
> >
> > currently, I am evaluating XStream and ran into some problems with it. I
> > serialized the following data container:
> >
> > package data;
> >
> > @XStreamAlias("shortarray")
> > public class ShortArrayContainer {
> > @XStreamAsAttribute
> > public short[] values;
> > }
> >
> > This serializes into something like (using stream.toXML(container,
> > out)):
> >
> > <shortarray>
> > <values>
> > <short>16383</short>
> > <short>16384</short>
> > <short>16385</short>
> > <short>16386</short>
> > <short>16387</short>
> > </values>
> > </shortarray>
> >
> > stream is initialized with
> > stream = new XStream(new DomDriver("UTF-8"));
> > stream.autodetectAnnotations(true);
> >
> > Unfortunately, I get a ConversionException, when I try to deserialize
> > with stream.fromXML(in):
> >
> > com.thoughtworks.xstream.converters.ConversionException: array element
> > type mismatch : array element type mismatch
> > ---- Debugging information ----
> > message : array element type mismatch
> > cause-exception : java.lang.IllegalArgumentException
> > cause-message : array element type mismatch
> > class : [Ljava.lang.Short;
> > required-type : [Ljava.lang.Short;
> > converter-type :
> > com.thoughtworks.xstream.converters.collections.ArrayConverter
> > path : /shortobjectarray/values
> > class[1] : data.ShortObjectArrayContainer
> > converter-type[1] :
> > com.thoughtworks.xstream.converters.reflection.ReflectionConverter
> > version : 1.4.5
> > -------------------------------
> >
> > Interestingly, I observed no problems when I am doing this with byte or
> > boolean arrays.
> >
> > I already tried playing around with Short vs short and List instead of
> > arrays without much success.
>
> ================== %< =======================
> @XStreamAlias("shortarray")
> public static class ShortArrayContainer {
> public short[] values;
>
> @Override
> public int hashCode() {
> return Arrays.hashCode(values);
> }
>
> @Override
> public boolean equals(Object obj) {
> return Arrays.equals(values, ((ShortArrayContainer)obj).values);
> }
> }
>
> public void testShortArray() {
> XStream xstream = new XStream();
> xstream.processAnnotations(ShortArrayContainer.class);
> ShortArrayContainer container = new ShortArrayContainer();
> container.values = new short[]{ 42, 4711, 16383, 16384, 16385 };
> String xml = ""
> + "<shortarray>\n"
> + " <values>\n"
> + " <short>42</short>\n"
> + " <short>4711</short>\n"
> + " <short>16383</short>\n"
> + " <short>16384</short>\n"
> + " <short>16385</short>\n"
> + " </values>\n"
> + "</shortarray>";
> assertEquals(container, xstream.fromXML(xml));
> }
> ================== %< =======================
>
> Works for me. You will have to provide an actually failing unit test.
>
> Two comments (both are also in the FAQ):
>
> 1/ Auto-detection cannot work for the root object at deserialization:
> http://xstream.codehaus.org/annotations-tutorial.html#AutoDetect
>
> 2/ @XStreamAsAttribute is simple ignored if the converter implementation is
> not a SingleValueConverter, i.e. the object to convert (here a short array)
> cannot be represented by the converter as a single string - a precondition
> for an XML attribute.
>
> Cheers,
> Jörg
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
> http://xircles.codehaus.org/manage_email
>
>
---------------------------------------------------------------------
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email