I have classes like this:
class A{
//pojo
}
class B extends {
// pojo
}
class C{
@XStreamImplicit( itemFieldName="A")
private ArrayList<A> aList = null;
}
The filed aList of class C contains one object type of A.class and one type of B.class.
I can serialize and deserialize the class to this xml:
<c>
<aList>
<a/>
<a/>
</aList>
</c>
But i want xml file to look like this:
<c>
<aList>
<a/>
<b/>
</aList>
</c>
Is it possible? And how can I do it?
This works for me:
@XStreamAlias("a")
public class A {
}
@XStreamAlias("b")
public class B extends A {
}
@XStreamAlias("c")
public class C {
private ArrayList<A> aList = null;
public C() {
aList = new ArrayList<A>();
aList.add(new A());
aList.add(new B());
aList.add(new A());
}
public static void main(String[] args) {
C c = new C();
XStream x = new XStream();
x.processAnnotations(A.class);
x.processAnnotations(B.class);
x.processAnnotations(C.class);
System.out.println(x.toXML(c));
}
}
and produce:
<c>
<aList>
<a/>
<b/>
<a/>
</aList>
</c>
If you dont want to call processAnnotations for every possible class you can write some dynamic code using reflection API and process all classes in loop.