Thanks a lot! The interesting thing is that the aliasType prevents printing
the class information, but the "column" string is not used. Instead the
names of the two printer fields are used.
Just in case somebody needs the same thing, here is the code.
public class Column {
private String title;
private boolean filtered;
:
@XStreamConverter(ColumnPrinterConverter.class)
private ColumnPrinter<? super Node<?>> nodePrinter;
@XStreamConverter(ColumnPrinterConverter.class)
private ColumnPrinter<? super Edge> edgePrinter;
:
public static class ColumnPrinterConverter implements Converter {
public void marshal(Object source, HierarchicalStreamWriter writer,
MarshallingContext context) {
ColumnPrinter printer = (ColumnPrinter) source;
writer.setValue(printer.getId());
}
public Object unmarshal(HierarchicalStreamReader reader,
UnmarshallingContext context) {
String id = (String) reader.getValue();
return ColumnPrinters.getDef().getPrinter(id);
}
@Override
public boolean canConvert(Class type) {
return ColumnPrinter.class.isAssignableFrom(type);
}
}
}
And the filters containing Column objects can be serialized using the
following code:
final XStream xstream = new XStream( new StaxDriver());
xstream.autodetectAnnotations(true);
xstream.aliasType("column", ColumnPrinter.class);
String xmlStr = xstream.toXML(filters);
Jirka
On Mon, Apr 23, 2012 at 2:42 PM, Jörg Schaible
<[email protected]>wrote:
> Hi Jirka,
>
> Jirka wrote:
>
> > Hi,
> >
> > thanks for your answer. I tried that (I should have said it), this is my
> > co= de
> >
> > @XStreamAlias("printer")
> > @XStreamConverter(ColumnPrinterConverter.class)
> > private ColumnPrinter<?> printer;
> >
> > :
> >
> > public static class ColumnPrinterConverter implements Converter {
> > public void marshal(Object source, HierarchicalStreamWriter
> > writer, MarshallingContext context) {
> > ColumnPrinter printer =3D (ColumnPrinter) source;
> > writer.setValue(printer.getId());
> > }
> >
> > public Object unmarshal(HierarchicalStreamReader reader,
> > UnmarshallingContext context) {
> > String id =3D (String) reader.getValue();
> > return ColumnPrinters.getDef().getPrinter(id);
> > }
> >
> > public boolean canConvert(Class type) {
> > return ColumnPrinter.class.isAssignableFrom(type);
> > }
> > }
> >
> > However, this still includes the class attribute. So it probably
> > creates instance of that class on unmarshalling (or not?) only to
> > throw it away, which could cause problems if the anonymous classes are
> > reordered.
> >
> > <printer class=3D"org.purl.jh.feat.navigator.ColumnPrinters$6">
> > id19
> > </printer>
>
> In this case you should drop the alias annotation above and use
> xstream.aliasType(...) instead defining an alias for all types assignable
> to
> a specific interface or superclass. It's then in the responsibility for the
> converter to create the real appropriate type, but that's what your
> converter does.
>
> - Jörg
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
> http://xircles.codehaus.org/manage_email
>
>
>