I'm trying to output a small object graph to JSON with XStream. Only output, no de-serializing required.
The objects are very simple:
@XStreamAlias("players")
public class Players {
@XStreamImplicit
private List<Player> players = new ArrayList<Player>();
public Players() {
for (int i = 0; i < 5; ++i) {
players.add(new Player("Player " + i));
}
}
}
@XStreamAlias("player")
public class Player {
private String name;
public Player(String name) {
this.name = name;
}
}
Luckily, the unnecessary list wrapper element is omitted in JSON:
{ players: { player: { name: "Player 4"}}}
But unfortunately, only the last element is printed.
I'm using XStream 1.4.2, intialized like that:
XStream xstream = new XStream(new JsonHierarchicalStreamDriver());
xstream.setMode(XStream.NO_REFERENCES);
xstream.aliasSystemAttribute(null, "class");
xstream.autodetectAnnotations(true);
String out = xstream.toXML(xstramAliasObject);
As far as I can see the setup is similar to this question: XStream Alias of List root elements.
What's wrong with my example?
Thanks in advance! Regards, Michael