Whimusical
Whimusical

Reputation: 6649

XStream converting issue (different subclasses in the same list)

I am only interested in de-serialization. I have some data that looks like this:

     <Data>
        <Class name='ClassA'>
           <Object name='ObjectA'>
              <Relation name='RelationA'>
                  <toObject name='ObjectB'>
                  <toObject name='ObjectC'>
              </Relation>
           </Object>
           <Object name='ObjectB'>
              <Relation name='RelationB'>
                  <toObject name='ObjectA'>
                  <toObject name='ObjectC'>
              </Relation>
           </Object>
        </Class>
        <Class name='ClassB'>
           <Object name='ObjectC'>
              <Relation name='RelationC'>
                  <toObject name='ObjectB'>
              </Relation>
           </Object>
           <NotUsedRelations>
               <Relation name='RelationD'>
                  <toObject name='ObjectA'>
               </Relation>
           </NotUsedRelations>
        </Class>
    </Data> 

I would like to use Xstream to read it but I am having problems with NotUsedRelations element:

    stream.alias("Data",     Model.class);
    stream.alias("Class",    Model.XMLClass.class);
    stream.alias("Object",   Model.XMLObject.class);
    stream.alias("Relation", Model.XMLRelation.class);
    stream.alias("toObject", Model.XMLObject.class);

Every type in Model extends from an XMLElement abstract class which defines a constructor with Name. Then they all have a

 List<XMLElement> subElements;

private var, and his getters/setters.

The problem is that Class contains Object subelements as well as an implicit collection of NotUsedRelations, but Relations and Objects are both extending XMLElement, so I would like to put them together in the subElements list

I tried different things with stream.addImplicitCollection() and stream.aliasField() but the compiler always complains

A problem is that one type is an implicit collection and the other not, but NotUsedRelations have no real maped class, should be defined as an alias for an implicit collection alone.

Thanks in advance for any help!

Upvotes: 0

Views: 1338

Answers (1)

Geoffrey De Smet
Geoffrey De Smet

Reputation: 27312

Take a look at this question.

Look for the non-annotation way of @XStreamImplicit(itemFieldName = ...) as in:

@XStreamAlias("coins")
public class Coins {
        @XStreamImplicit(itemFieldName="coin")
        List<String> coins = new ArrayList<String>();
}

Also take a look at the non-annotation way of @XStreamInclude to deal with the subclass problem, as in:

@XStreamInclude({
        UnionMoveSelectorConfig.class, CartesianProductMoveSelectorConfig.class,
        ChangeMoveSelectorConfig.class, SwapMoveSelectorConfig.class, PillarSwapMoveSelectorConfig.class,
        SubChainChangeMoveSelectorConfig.class, SubChainSwapMoveSelectorConfig.class,
        MoveListFactoryConfig.class, MoveIteratorFactoryConfig.class
})
public abstract class MoveSelectorConfig

Upvotes: 2

Related Questions