Search code examples
javaxstream

XStream doesn't support custom converter cross-delegation?


It appears to me that the Java XStream library does not support cross-delegation. Am I correct in this belief?

So, I can explain what I mean, consider the following example:

<node-type-x>
  <node-type-y>
     <a/>
     <b/>
  </node-type-y>
<node-type-y>
  <c/>
  <d/>

Let's say we have a converter for "node-type-x" nodes and another converter for "node-type-y" nodes. The functionality I would like to see in XStream would be sometype of delegate() method which I could call within the node-type-x converter that would identify nested node-type-y nodes and delegate unmarshalling to the converter for such nodes and return the result so that the node-type-x converter can process the result as needed. As it stands, XStream seems to require that the converter for "node-type-x" handle processing of all children of such nodes.


Solution

  • Two simple ways to do this:

    1. Register your node-type-y converter with the XStream instance, and inside your node-type-x converter, call marshallingContext.convertAnother(object).
    2. Don't register your converter with the XStream instance, and inside your node-type-x converter, call marshallingContext.convertAnother(object, converter), passing it the converter you'd like to use for the "y" that you passed it.