变量类型更改时的 XStream 反序列化

发布于 2024-10-12 16:32:50 字数 520 浏览 1 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

明媚如初 2024-10-19 16:32:50

因此,根据您的评论,我相信您需要一个自定义转换器。

这是一个示例:

import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;

public class MyClassConverter implements Converter{

    @Override
    public boolean canConvert(Class clazz) 
    {
        return clazz.equals(MyClass.class);
    }

    @Override
    public void marshal(Object value, HierarchicalStreamWriter writer,
            MarshallingContext context) 
    {

    }

    @Override
    public Object unmarshal(HierarchicalStreamReader reader,
            UnmarshallingContext context) 
    {
        // Create MyClass Object
        MyClass myClass = new MyClass();

        // Traverse Tree
        while (reader.hasMoreChildren()) 
        {
            reader.moveDown();
            if ("polygon".equals(reader.getNodeName())) 
            {
                Polygon polygon = (Polygon)context.convertAnother(myClass, Polygon.class);
                myClass.addPolygon(polygon);
            } 
            reader.moveUp();
        }

        // Return MyClass Object
        return myClass;
    }
}

如果您不知道,这里有一个参考指南: 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:

import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;

public class MyClassConverter implements Converter{

    @Override
    public boolean canConvert(Class clazz) 
    {
        return clazz.equals(MyClass.class);
    }

    @Override
    public void marshal(Object value, HierarchicalStreamWriter writer,
            MarshallingContext context) 
    {

    }

    @Override
    public Object unmarshal(HierarchicalStreamReader reader,
            UnmarshallingContext context) 
    {
        // Create MyClass Object
        MyClass myClass = new MyClass();

        // Traverse Tree
        while (reader.hasMoreChildren()) 
        {
            reader.moveDown();
            if ("polygon".equals(reader.getNodeName())) 
            {
                Polygon polygon = (Polygon)context.convertAnother(myClass, Polygon.class);
                myClass.addPolygon(polygon);
            } 
            reader.moveUp();
        }

        // Return MyClass Object
        return myClass;
    }
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文