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>
Jirka
On Mon, Apr 23, 2012 at 7:05 AM, Sven Meier <[email protected]> wrote:
> Hi,
>
> create a custom converter for ColumnPrinter:
>
> http://xstream.codehaus.org/**converter-tutorial.html<http://xstream.codehaus.org/converter-tutorial.html>
>
> Sven
>
>
> On 04/22/2012 11:10 PM, Jirka Hana wrote:
>
>> Hello,
>>
>> I am new to XStream, I got 95% of my task done in nearly no time, but
>> now I am stuck with trying to serialize the following class:
>>
>> class MyClass {
>> private String name;
>> private Pattern pattern;
>> // few more fields
>> private ColumnPrinter<?> printer;
>> }
>>
>> The printer can be an instance of any subclass (possibly anonymous) of
>> the ColumnPrinter interface. All must implement the getId() method and all
>> are registered in a global registry (by calling the method ColumnPrinter<?>
>> getPrinter(String id))
>>
>> I would like to save all the fields the usual way, but for the printer, I
>> would like to save its id only (say as <printer>12</printer>). Then when
>> deserializing the id should be read, and the proper instance should be
>> looked up in the global registry.
>>
>> I am not sure how to do that. Can anybody help?
>>
>> Thanks
>> Jirka
>>
>>
>
> ------------------------------**------------------------------**---------
> To unsubscribe from this list, please visit:
>
>
> http://xircles.codehaus.org/**manage_email<http://xircles.codehaus.org/manage_email>
>
>
>