Hi Adrian,
Adrian Jackson wrote:
>
> There are methods in XStream for setting up implicit collections, arrays
> and maps that I'm now using. But I want to be able to handle *fields*
> implicitly as well.
That's not possible, since "implicit" means that the surrounding container
is dropped from the XML representation, but for fields is no such container
that can be dropped.
> For example:
>
> public class WidgetHolder {
> private Widget heldWidget;
> }
>
> public abstract class Widget {
> }
>
> public class Doohickey extends Widget {
> }
>
> public class Thingummy extends Widget {
> }
>
> In this case I end up creating XML of the form:
>
> <widgetHolder>
> <heldWidget class="doohickey"/>
> </widgetHolder>
>
> <widgetHolder>
> <heldWidget class="thingummy"/>
> </widgetHolder>
>
> What I want to be able to do, though is specify that any instance of type
> Widget should be stored in the 'heldWidget' field of WidgetHolder, letting
> me write XML in the form:
>
> <widgetHolder>
> <thingummy/>
> </widgetHolder>
>
> <widgetHolder>
> <doohickey/>
> </widgetHolder>
>
> I feel sure this should be possible,
No, because the ReflectionConverter would no longer know, what to do with
these elements at deserialization time.
> but can't quite see how to do it
> without completely taking control of the conversion of widgetHolder (which
> I don't want to do, as its other properties are relatively complex and are
> handled nicely by XStream as it stands). I'm probably missing something
> obvious - any suggestions?
Use a custom converter. However, you may simply derive from the
ReflectionConverter, handle this field on your own before calling super and
declare it to be omitted (by the ReflectionProvider). All the stuff required
by the constructor can be obtained by your XStream instance.
Cheers,
Jörg
---------------------------------------------------------------------
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email