XStream:如何在自定义转换器中编组/解组列表?
我有以下使用自定义转换器序列化的类(旧版;不可注释):
class Test {
// some other variables
List<SomeType> someTypeList;
}
已经可以使用 SomeType 正常工作的转换器。但是我希望序列化列表,就像用 @XStreamAlias("someTypes") 注释一样。
最后,我期望 someTypeList 具有以下格式:
<someTypes class="list-type">
<someType>
....
</someType>
...
</someTypes>
如何实现 marshal/unmarshal 方法才能获得所需的输出?调用 context.convertAnother(someTypeList) 不会产生预期结果,因为周围的
标记丢失。
I have the following class (legacy; not annotatable) that is serialized with a custom converter:
class Test {
// some other variables
List<SomeType> someTypeList;
}
A properly working converter for SomeType is already available. However I want the list to be serialized as if it was annotated with @XStreamAlias("someTypes").
In the end I expect the following format for someTypeList:
<someTypes class="list-type">
<someType>
....
</someType>
...
</someTypes>
How do I have to implement the marshal/unmarshal method to get the desired output? Calling context.convertAnother(someTypeList) didn't yield the expected result as the surrounding <someTypes>
tag was missing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
明智的做法是获取结构:
看下面的代码。对于您的列表,您需要标记:
现在,根据您内部的内容,您可能需要创建一个自定义转换器。要引用它,您可以像这样进行更改:
然后创建一个转换器类(
YourOwnConverter
),该类将知道如何取消/封送:使用此示例:
http://x-stream.github.io/converter-tutorial.html
You wise to get the structure:
Look at the following code. For your list you need to tag:
Now, depending on what you got inside, you might need to create a custom converter. To refer to that you change a bit like this:
Then create a converter class (
YourOwnConverter
) that would know how to un/marshal:Use this as example:
http://x-stream.github.io/converter-tutorial.html
在配置期间是否有在 xstream 对象上调用的 addImplicitCollection 导致跳过 someTypes 标记?
Is there a addImplicitCollection called on the xstream object during configuration somewhere which causes the someTypes tag to be skipped ?