使用 XMLEncoder 序列化 UUID

发布于 2024-12-28 11:33:29 字数 697 浏览 1 评论 0原文

我正在使用 XMLEncoder 将对象图写入 XML 文件。 除了 UUID 属性(在我的 JavaBean 中其名称为 id)之外,效果很好 我知道我需要一个 PersistenceDelegate 来完成它。我写了以下一个:

class UuidPersistenceDelegate extends PersistenceDelegate {
    protected Expression instantiate(Object oldInstance, Encoder out) {
        UUID id = (UUID) oldInstance;
        return new Expression(oldInstance, id.getClass(), "fromString", new Object[]{ "id" } );
    }
}

并将其设置为编码器:

encoder.setPersistenceDelegate(UUID.class, new UuidPersistenceDelegate());

运行时调用encoder.writeObject(...)时出现以下异常:

java.lang.IllegalArgumentException:无效的UUID字符串:id

在 有人知道如何让它发挥作用吗?

I'm using XMLEncoder to write an object graph to a XML file.
That works fine, except for the UUID property (which has the name id in my JavaBean)
I know that I need a PersistenceDelegate to get it done. I wrote the following one:

class UuidPersistenceDelegate extends PersistenceDelegate {
    protected Expression instantiate(Object oldInstance, Encoder out) {
        UUID id = (UUID) oldInstance;
        return new Expression(oldInstance, id.getClass(), "fromString", new Object[]{ "id" } );
    }
}

And set it to the Encoder:

encoder.setPersistenceDelegate(UUID.class, new UuidPersistenceDelegate());

During runtime I get the following exception when calling encoder.writeObject(...):

java.lang.IllegalArgumentException: Invalid UUID string: id

Does anyone know how to get this to work?

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

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

发布评论

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

评论(2

对你再特殊 2025-01-04 11:33:29

我还没有看到有人真正正确地回答这个问题并且确实有效:

public class UUIDPersistenceDelegate extends PersistenceDelegate {
private HashSet<UUID> hashesWritten = new HashSet<UUID>();

public Expression instantiate(Object oldInstance, Encoder out) {
    UUID id = (UUID) oldInstance;
    hashesWritten.add(id);
    return new Expression(oldInstance, UUID.class, "fromString", new Object[]{ id.toString() } );
}

protected boolean mutatesTo(Object oldInstance, Object newInstance) {
    return hashesWritten.contains(oldInstance);
}

}

I haven't seen anyone actually answer this properly and that actually works:

public class UUIDPersistenceDelegate extends PersistenceDelegate {
private HashSet<UUID> hashesWritten = new HashSet<UUID>();

public Expression instantiate(Object oldInstance, Encoder out) {
    UUID id = (UUID) oldInstance;
    hashesWritten.add(id);
    return new Expression(oldInstance, UUID.class, "fromString", new Object[]{ id.toString() } );
}

protected boolean mutatesTo(Object oldInstance, Object newInstance) {
    return hashesWritten.contains(oldInstance);
}

}

罪歌 2025-01-04 11:33:29

欢迎来到SO。您非常接近您的解决方案,这是您的代码的一个小问题。您正在将字符串“id”传递到您的参数参数中,我很确定您不想这样做。试试这个:

protected Expression instantiate(Object oldInstance, Encoder out) {
    UUID id = (UUID) oldInstance;
    return new Expression(oldInstance, UUID.class, "fromString", new Object[]{ id.toString() } );
}

输出的 XML 不太漂亮,但至少你会摆脱错误。

Welcome to SO. You are very close to your solution, one minor problem with your code. You are passing in the String "id" into your arguments parameter, which I'm pretty sure you do not want to do. Try this instead:

protected Expression instantiate(Object oldInstance, Encoder out) {
    UUID id = (UUID) oldInstance;
    return new Expression(oldInstance, UUID.class, "fromString", new Object[]{ id.toString() } );
}

The outputted XML is not pretty, but at least you will get rid of your error.

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