使用 Simple XML (org.simpleframework.xml) 序列化第三方类
我决定使用 简单 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我必须再次自己回答:-)
来自 Simple XML 项目负责人 Niall Gallagher 的建议,来自支持列表:
因此,我使用
Transform
/Matcher
并对它感到满意。这并没有改变事实Converter
对我不起作用:-)I have to answer by myself again :-)
Advice from Niall Gallagher, project leader of Simple XML, from support-list:
So, I use
Transform<T>
/Matcher
and satisfied with it. This does not alter the fact that theConverter<T>
does not work for me :-)我想我有这个问题的答案。
应该注册转换器并解决问题。
I think i have the answer to this.
should register the converter and solve the problem.
我知道这有点过时了,但我有机会遇到同样的例外。
实际问题是 @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.