Search code examples
javaarraysjsonxstream

Using xstream to parse object with array


I've been using stream for a number of other restful calls and all has been working well. However I now have an Object with two variables, one of which is an array of objects.

public class BuildQueue
{
    public String                           playerId;
    public BuildQueueItem[]                 buildQueueItems;
}

Within BuildQueueItem I have the following...

public class BuildQueueItem
{
    public Technology               tech;
    public long                     buildNextTurn;
    public long                     techLevel;
    public long                     numberOf;
}

I'll leave the tech object out of here for now as it's not getting that far.

Here is the json being parsed...

{
  "BuildQueue": {
    "buildQueueItems": [
      {
        "buildNextTurn": 1,
        "numberOf": 0,
        "tech": {
          "price": 100,
          "productionUnitsIncreasePerTechLevel": 0,
          "uuid": "59697db4-b95b-47a4-b5ba-2d0dd821e243",
          "workerUnitsIncreasePerTechLevel": 2
        },
        "techLevel": 0
      },
      {
        "buildNextTurn": 1,
        "numberOf": 1,
        "tech": {
          "price": 50,
          "productionUnitsIncreasePerTechLevel": 0,
          "uuid": "dd832f7d-b21b-42c6-9fbe-5d6e4d5f80a0",
          "workerUnitsIncreasePerTechLevel": 0
        },
        "techLevel": 0
      }
    ],
    "playerId": "G:917864420"
  }
}

Here is the code I'm using to do the parsing..

xstreamReader.alias ("BuildQueue", BuildQueue.class);
xstreamReader.alias ("BuildQueueItem", BuildQueueItem.class);

BuildQueue queue = (BuildQueue)xstreamReader.fromXML (bodyData);

Finally here is the error...

com.thoughtworks.xstream.converters.ConversionException: buildNextTurn : buildNextTurn
---- Debugging information ----
message             : buildNextTurn
cause-exception     : com.thoughtworks.xstream.mapper.CannotResolveClassException
cause-message       : buildNextTurn
class               : [Lcom.etepstudios.dataobjects.BuildQueueItem;
required-type       : [Lcom.etepstudios.dataobjects.BuildQueueItem;
converter-type      : com.thoughtworks.xstream.converters.collections.ArrayConverter
path                : /BuildQueue/buildQueueItems/buildNextTurn
line number         : -1
class[1]            : com.etepstudios.dataobjects.BuildQueue
converter-type[1]   : com.thoughtworks.xstream.converters.reflection.ReflectionConverter
version             : 1.4.7
-------------------------------

So it seems to understand that it should be a BuildQueueItem object. But for some reason it can't figure out what type of class buildNextTurn should be. This is really the first variable it is trying to parse.

I've tried numerous ways of doing this including using examples I found of using ArrayList. But none of those worked either.

I've been able to do arrays before by creating the json but that didn't have the array embedded in another object.

Any thoughts?


Solution

  • You can use addImplicitArray method available in XStream. So you may try like this:

    xstreamReader.alias ("BuildQueue", BuildQueue.class);
    xstreamReader.addImplicitArray(BuildQueue.class,"buildQueueItems");
    xstreamReader.alias ("buildQueueItems", BuildQueueItem.class);
    

    Make sure you are using the XStream version 1.4.7 onwards because addImplicitArray method available from this version