2

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

Community
  • 1
  • 1
Michael
  • 291
  • 1
  • 3
  • 13

1 Answers1

0

I'm using the Same Library and but getting entire message..

{"players": {
  "player": {
    "name": "Player 0"
  },
  "player": {
    "name": "Player 1"
  },
  "player": {
    "name": "Player 2"
  },
  "player": {
    "name": "Player 3"
  },
  "player": {
    "name": "Player 4"
  }
}}

Here is the Code..

public static void main(String[] args) {
        Players xstramAliasObject = new Players();
        XStream xstream = new XStream(new JsonHierarchicalStreamDriver());
        xstream.setMode(XStream.NO_REFERENCES);
        xstream.aliasSystemAttribute(null, "class");
        xstream.autodetectAnnotations(true);
        System.out.println("--- "+xstream.toXML(xstramAliasObject));
    }
Santosh Giri
  • 109
  • 12