[Simple-support] Serialise/Deserialise collections
Brought to you by:
niallg
|
From: Nhat N. <phu...@gm...> - 2012-10-27 13:58:02
|
Hi all,
I am using SimpleXML to read/write data to XML from a part of a wider
package of air traffic management and would like to ask for details on how
to serialise and deserialise an odd setup on collections type. I got the
following 2 classes:
*Position.java*
@Root(name="Position")
public class Position {
@Text
private String name;
public Position(@Text String name) {
this.name=name
}
}
*PositionSet.java *(used system-wide and should not be modified in
functionality)
import com.google.common.collect.ForwardingSortedSet;
public class PositionSet extends ForwardingSortedSet<Position> {
private final SortedSet<Position> delegate;
public PositionSet() {
this.delegate = new TreeSet<Position>();
}
public PositionSet(Collection<Position> delegate) {
this.delegate = new TreeSet<Position>(delegate);
}
@Override
protected SortedSet<Position> delegate() {
return delegate;
}
}
I want to produce the following XML:
<Positions>
<Position>A</Position>
<Position>B</Position>
</Positions>
The easiest option is to create another class called Positions.java, load
the positions and then add it to PositionSet. However, I do not want to
pollute the models with redundant codes and would like to use PositionSet
and Position only.
Further from that, there are other similar collections construction in the
code bases that use SortedSet interface. Would it be possible to write a
good Converter for SortedSet that handle different child elements (given
they have respective annotated classes).
Thanks,
Nhat
|