@XStreamImplicit() for a field which is not a collection

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

@XStreamImplicit() for a field which is not a collection

Geoffrey De Smet
How do I model a singleton field of a supertype of several types with
different element names?


I have these classes:

@XStreamAlias("house")
public class House {

     @XStreamImplicit()
     private List<Animal> animalList;

}

@XStreamInclude({Dog.class, Cat.class})
public abstract class Animal {}

@XStreamAlias("dog")
public class Dog extends Animal {}

@XStreamAlias("cat")
public class Cat extends Animal {}

so I can write:

<house>
   <dog/>
   <dog/>
   <cat/>
</house>

and the animalList of House is filled with 2 dogs and a cat.
This works well.


Now I want to be able to do support an xml like this:
<cage>
   <dog/>
</cage>
or
<cage>
   <cat/>
</cage>

and a class diagram that doesn't use a List<Animal> field, but just a
field Animal:

@XStreamAlias("cage")
public class Cage {

     @XStreamImplicit()
     private Animal animal;

}

But that doesn't work. How can I make that work?
Without changing the XML schema.
Currently I am working around by using an List<Animal> anyway,
and adding validation that it's a singleton list... but that's undesired.



---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: @XStreamImplicit() for a field which is not a collection

Jörg Schaible-2
Hi Geoffrey,

Geoffrey De Smet wrote:

> How do I model a singleton field of a supertype of several types with
> different element names?
>
>
> I have these classes:
>
> @XStreamAlias("house")
> public class House {
>
>      @XStreamImplicit()
>      private List<Animal> animalList;
>
> }
>
> @XStreamInclude({Dog.class, Cat.class})
> public abstract class Animal {}
>
> @XStreamAlias("dog")
> public class Dog extends Animal {}
>
> @XStreamAlias("cat")
> public class Cat extends Animal {}
>
> so I can write:
>
> <house>
>    <dog/>
>    <dog/>
>    <cat/>
> </house>
>
> and the animalList of House is filled with 2 dogs and a cat.
> This works well.
>
>
> Now I want to be able to do support an xml like this:
> <cage>
>    <dog/>
> </cage>
> or
> <cage>
>    <cat/>
> </cage>
>
> and a class diagram that doesn't use a List<Animal> field, but just a
> field Animal:
>
> @XStreamAlias("cage")
> public class Cage {
>
>      @XStreamImplicit()
>      private Animal animal;
>
> }
>
> But that doesn't work.

Implicit declaration is only meaningful for List, Map or Array types.

> How can I make that work?

Not by configuration. The Cage member will alway be named 'animal'. An alias
cannot do, since it maps only the name of the member ... you want to map the
member instance's type.

> Without changing the XML schema.
> Currently I am working around by using an List<Animal> anyway,
> and adding validation that it's a singleton list... but that's undesired.

Create a custom converter for Cage and write the XML tag yourself using the
Animal's real type. If you provide the current Mapper as constructor
argument. you can lookup the aliases for Dog.class and Cat.class. See the
example at the end of the converter-tutorial, how you can pass the
marshalling of the member itself again to XStream.

Cheers,
Jörg



---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email