变量类型更改时的 XStream 反序列化
我有一个 java 类,看起来像
public class MyClass {
private final String str;
private Polygon polygon; // this polygon is a custom type of mine
}
有一个 xml 文件,其中有一个使用 XStream 写入的 MyClass
实例。
现在 MyClass
已更改,多边形已替换为 List
并且该字段已重命名为 polygons
,我试图不这样做打破反序列化。我想更改polygon
字段的反序列化以基本上读取多边形,然后创建一个新列表并向其中添加单个多边形。该列表将成为新的字段值。
是否可以仅更改这一字段的转换?或者我是否需要为整个类MyClass
编写一个自定义转换器?
谢谢, 杰夫
I have a java class that looks like
public class MyClass {
private final String str;
private Polygon polygon; // this polygon is a custom type of mine
}
I have an xml file that has an instance of MyClass
written to it using XStream.
Now MyClass
has changed and polygon has been replaced with List<Polygon>
and the field has been renamed to polygons
, and I'm trying not to break deserialization. I want to change the deserialization of the polygon
field to basically read the polygon and then just create a new list and add the single polygon to it. The list would then be the new field value.
Is it possible to change the conversion of just this one field? Or do I need to write a custom converter for the whole class MyClass
?
thanks,
Jeff
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因此,根据您的评论,我相信您需要一个自定义转换器。
这是一个示例:
如果您不知道,这里有一个参考指南: http://x -stream.github.io/converter-tutorial.html
现在,剩下要做的就是注册您的转换器,我假设您知道该怎么做。无论如何,需要注意的一个重要但显而易见的事情是“addPolygon”是我用来填充新列表对象的方法。
So based on your comment, I believe you'll need a custom converter.
Here's an example:
In case you're unawares, here's a reference guide: http://x-stream.github.io/converter-tutorial.html
Now, all that's left to do is register your converter, which I'm assuming you know how to do. Anyway, an important, although obvious thing to note is that 'addPolygon' is a method I used to populate your new list object.