Hi,

Villemos wrote:

> 
> Sorry if this is an answer that show how little I know of XStream; I cant
> find the ListConverter in the XStream API...?

I am sorry, I mixed up some project-specific extensions with the standard 
functionality. So it is not your fault, if you do not find something.

However, the approach stays the same. You'll need a local custom converters 
that handles your lists. You may derive them from the CollectionConverter:

class AliasingCollectionConverter extends CollectionConverter {
  private String alias;
  private Class itemType;
  public AliasingCollectionConverter(Mapper mapper, String alias, Class 
itemType) {
    super(mapper);
    this.alias = alias;
    this.itemType = itemType;
  }
  protected void writeItem(Object item, MarshallingContext context, 
HierarchicalStreamWriter writer) {
    if (item != null) {
      ExtendedHierarchicalStreamWriterHelper.startNode(writer, alias, 
item.getClass());
      context.convertAnother(item);
      writer.endNode();
    } else {
      super.writeItem(item. context, writer);
    }
  }
  protected Object readItem(HierarchicalStreamReader reader, 
UnmarshallingContext context, Object current) {
    if (alias.equals(reader.getNodeName())) {
      return context.convertAnother(current, itemType);
    } else {
      return super.readItem(reader, object, current);
    }
  }
}

then you register two instance locally for your lists:

xstream.registerLocalConverter(Metadata.class, "result", new 
AliasingCollectionConverter(xstream.getMapper(), "item", 
MetadataItem.class));
xstream.registerLocalConverter(MetadataItem.class, "fields", new 
AliasingCollectionConverter(xstream.getMapper(), "item", 
MetadataField.class));

The registration can be done alternatively also with annotations:

public class Metadata {
// ...
@XStreamConverter(value=AliasingCollectionConverter.class, 
types={MetadataItem.class}, strings={"item"})
public List<MetadataItem> result = new ArrayList<MetadataItem>();
// ...
}

public class MetadataItem {
// ...
@XStreamConverter(value=AliasingCollectionConverter.class, 
types={MetadataField.class}, strings={"item"})
public List<MetadataField> fields = new ArrayList<MetadataField>();
// ...
}

I did not cross-check this code, but you get the ides. Hope this helps,
Jörg


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply via email to