使用 Simple XML (org.simpleframework.xml) 序列化第三方类

发布于 2024-09-17 08:57:54 字数 2092 浏览 5 评论 0原文

我决定使用 简单 XML 序列化,但遇到了基本问题。我正在尝试将 java.util.UUID 类实例序列化为这个小类中的最终字段:

@Root
public class Identity {
    @Attribute
    private final UUID id;

    public Identity(@Attribute UUID id) {
        this.id = id;
    } 
}

教程展示了如何通过注册转换器来序列化第三方对象,如下所示:

Registry registry = new Registry();
registry.bind(UUID.class, UUIDConverter.class);
Strategy strategy = new RegistryStrategy(registry);
Serializer serializer = new Persister(strategy);

serializer.write( object, stream );

适用于 UUID 的转换器非常简单:

public class UUIDConverter implements Converter<UUID> {
    @Override
    public UUID read(InputNode node) throws Exception {
       return new UUID.fromString(node.getValue());
    }
    @Override
    public void write(OutputNode node, UUID value) throws Exception {
       node.setValue(value.toString());
    }
}

但是这个简单的代码对我来说不起作用,在序列化具有 UUID 字段的对象期间抛出异常不支持类 java.util.UUID 的转换

我尝试过与自定义 Matcher (不在教程中)类似的东西,对我有用:

Serializer serializer = new Persister(new MyMatcher());

serializer.write( object, stream );

并且 Matcher 类如下所示:

public static class MyMatcher implements Matcher {
    @Override
    @SuppressWarnings("unchecked")
    public Transform match(Class type) throws Exception {
        if (type.equals(UUID.class))
            return new UUIDTransform();
        return null;
    }
}

public class UUIDTransform implements Transform<UUID> {
    @Override
    public UUID read(String value) throws Exception {
        return UUID.fromString(value);
    }
    @Override
    public String write(UUID value) throws Exception {
        return value.toString();
    }
}

问题:

  • 自定义匹配器是否始终推荐实践用于流式传输第三方课程?
  • 在什么情况下我可以使用转换器?
  • 有没有更好的简单 XML 教程/示例?

谢谢。

I have decided to use Simple XML serialization and was stucked with basic problem. I am trying to serialize java.util.UUID class instance as final field in this small class:

@Root
public class Identity {
    @Attribute
    private final UUID id;

    public Identity(@Attribute UUID id) {
        this.id = id;
    } 
}

Tutorial shows how to serialize third-party objects by registering converters like this:

Registry registry = new Registry();
registry.bind(UUID.class, UUIDConverter.class);
Strategy strategy = new RegistryStrategy(registry);
Serializer serializer = new Persister(strategy);

serializer.write( object, stream );

appropriate converter for UUID is pretty simple:

public class UUIDConverter implements Converter<UUID> {
    @Override
    public UUID read(InputNode node) throws Exception {
       return new UUID.fromString(node.getValue());
    }
    @Override
    public void write(OutputNode node, UUID value) throws Exception {
       node.setValue(value.toString());
    }
}

But this simple code just didn't work for me, during serialization objects with UUID fields was thrown exception Transform of class java.util.UUID not supported.

I have tried something something similar with custom Matcher (which was not in tutorial) that works for me:

Serializer serializer = new Persister(new MyMatcher());

serializer.write( object, stream );

and Matcher class looks like this:

public static class MyMatcher implements Matcher {
    @Override
    @SuppressWarnings("unchecked")
    public Transform match(Class type) throws Exception {
        if (type.equals(UUID.class))
            return new UUIDTransform();
        return null;
    }
}

public class UUIDTransform implements Transform<UUID> {
    @Override
    public UUID read(String value) throws Exception {
        return UUID.fromString(value);
    }
    @Override
    public String write(UUID value) throws Exception {
        return value.toString();
    }
}

Questions:

  • Is custom Matcher always recommended practice for streaming third-party classes?
  • In which case I can use Converter?
  • Are there any better tutorials/examples for Simple XML out there?

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

始终不够爱げ你 2024-09-24 08:57:54

我必须再次自己回答:-)

来自 Simple XML 项目负责人 Niall Gallagher 的建议,来自支持列表:

“你可以使用转换器或变换。我想说
对于 UUID,使用匹配器进行转换将是最简单的选择。”

因此,我使用 Transform/Matcher 并对它感到满意。这并没有改变事实Converter 对我不起作用:-)

I have to answer by myself again :-)

Advice from Niall Gallagher, project leader of Simple XML, from support-list:

"You could use either a Converter or a Transform. I would say
for a UUID a Transform with a Matcher would be the easiest option."

So, I use Transform<T>/Matcher and satisfied with it. This does not alter the fact that the Converter<T> does not work for me :-)

网名女生简单气质 2024-09-24 08:57:54

我想我有这个问题的答案。

Strategy strategy = new AnnotationStrategy();
Serializer serializer = new Persister(strategy);

应该注册转换器并解决问题。

I think i have the answer to this.

Strategy strategy = new AnnotationStrategy();
Serializer serializer = new Persister(strategy);

should register the converter and solve the problem.

爱,才寂寞 2024-09-24 08:57:54

我知道这有点过时了,但我有机会遇到同样的例外。

实际问题是 @Attribute 注释的使用。如果代替@Attribute
你把@Element放在异常不会出现并且转换器用于序列化。

我想这将取决于您使用的注释,您应该创建转换器或使用标记转换解决方案。虽然我不知道这是否是有意的行为。

I know this is a bit aold but my chance i came to the same exception.

The actual issue is the use of the @Attribute annotation. If instead of @Attribute
you put @Element the exception does not appear and the converter is used for the serialization.

I guess it will then depend on which annotation you used that you should create a Converter or use the Marker-Transform solution. Although i do not know if this is the intendent behaviour.

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