序列化 ENUM 单例

发布于 2024-08-19 10:09:14 字数 865 浏览 8 评论 0原文

我正在尝试将 ENUM 单例实例(如 Joshua Bloch 在他的《Effective Java》一书中所述)序列化到一个文件。 ENUM 实例是一个简单的 JavaBean,如下所示:

public enum ElvisFan implements Serializable{
  INSTANCE;
  private int totalSongsListened;

private ElvisFan(){
     totalSongsListened=0;
}

public void set(int v){
     totalSongsListened=v;
}

public int get(){
     return totalSongsListened;
}

}
}

我在程序中成功地使用了这个枚举,但是当我使用 Snakeyaml 将此枚举写入文件时,我只有 !!com.chown.ElvisFan 'INSTANCE'< /code> 在我的 test.yaml 文件中。这就是我正在做的:

Yaml yaml = new Yaml();
yaml.dump(ElvisFan.INSTANCE, new FileWriter("test.yml");

我也尝试过,但没有任何运气:

JavaBeanDumper dumper = new JavaBeanDumper();
dumper.dump(ElvisFan.INSTANCE, new FileWriter("test.yml");

有人可以指导我吗?谢谢!

[已编辑]

代码更正。

I'm trying to serialize an ENUM singleton instance (as described by Joshua Bloch in his book Effective Java) to a file. The ENUM instance is a simple JavaBean as this:

public enum ElvisFan implements Serializable{
  INSTANCE;
  private int totalSongsListened;

private ElvisFan(){
     totalSongsListened=0;
}

public void set(int v){
     totalSongsListened=v;
}

public int get(){
     return totalSongsListened;
}

}
}

I'm successfully using this enum all over my program but when I write this enum to a file using snakeyaml, I just have !!com.chown.ElvisFan 'INSTANCE' in my test.yaml file. This is what I'm doing:

Yaml yaml = new Yaml();
yaml.dump(ElvisFan.INSTANCE, new FileWriter("test.yml");

I also tried this without any luck:

JavaBeanDumper dumper = new JavaBeanDumper();
dumper.dump(ElvisFan.INSTANCE, new FileWriter("test.yml");

Can someone please guide me on this. Thanks!

[Edited]

Code correction.

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

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

发布评论

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

评论(2

生死何惧 2024-08-26 10:09:14

单例没有任何意义。可序列化的单例就更没有意义了。根据定义,只有一个单例。因此,当您反序列化单例时,您将不会获得新对象。您将获得具有相同旧数据的相同旧实例。

枚举序列化是经过特殊处理的。它们由名称和类型表示。没有保存其他状态,因为如前所述,这没有任何意义。

我建议修改您的代码以避免可变的静态。

枚举不应该具有可变状态。使用单个实例序列化枚举在它们实现其他类(例如Comparator)或用作键等的情况下是有意义的。

Singletons don't reakky make any sense. Serialisable singletons make even less sense. There is by definition only one singleton. So when you deserialise a singleton you are not going to get a new object. You will get the same old instance with the same old data.

Enums serialisation is handled specially. They are represented by name and type. No other state is saved, because as previously stated that doesn't make any sense.

I suggest modifying your code to avoid mutable statics.

Enums should not have mutable state. Serialising enums with a single instance can make sense where they implement some other class, such as Comparator, or are use as a key or somesuch.

冰之心 2024-08-26 10:09:14

SnakeYAML 将 List 视为序列而不是映射,即使 List 也有 getter (getFirst())。
为什么您期望 enum 被序列化为 map ?

您必须提供一个自定义 Representer 来将这种不寻常的枚举转储为 JavaBean。
测试包含许多如何编写自定义表示器/构造器的示例。

SnakeYAML treats a List as a sequence not as a mapping even though List has also getters (getFirst()).
Why do you expect enum to be serialized as map ?

You have to provide a custom Representer to dump such an unusual enum as a JavaBean.
Tests contain a lot of examples how to write a custom Representer/Constructor.

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