XStream 不支持自定义转换器交叉委托? [英] XStream doesn't support custom converter cross-delegation?
问题描述
在我看来,Java XStream 库不支持交叉委托.我的这种信念是否正确?
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/>
假设我们有一个用于node-type-x"节点的转换器和另一个用于node-type-y"节点的转换器.我喜欢在 XStream 中看到的功能将是某种类型的 delegate() 方法,我可以在 node-type-x 转换器中调用它来识别嵌套的节点类型-y 节点并将解组委托给此类节点的转换器并返回结果,以便节点类型 x 转换器可以根据需要处理结果.就目前而言,XStream 似乎要求node-type-x"转换器处理此类节点的所有子节点.
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.
推荐答案
两种简单的方法:
- 使用 XStream 实例注册您的 node-type-y 转换器,并在您的 node-type-x 转换器中调用 marshallingContext.convertAnother(object).
- 不要使用 XStream 实例注册您的转换器,并在您的 node-type-x 转换器中调用 marshallingContext.convertAnother(object, converter),将其传递给转换器您想将其用于传递给它的y".
- Register your node-type-y converter with the XStream instance, and inside your node-type-x converter, call marshallingContext.convertAnother(object).
- 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.
这篇关于XStream 不支持自定义转换器交叉委托?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!