Java:Java5枚举自定义序列化

标签 java serialization enums

为了序列化大型枚举实例,我正在考虑使用“序列化代理”,其中代理对象将是一个非常小的对象,唯一标识相应的枚举实例。但是我发现枚举的任何读/写对象或解析/替换方法都会被忽略。所以我的问题是,如何有效地序列化大型枚举实例?字符串/XML 表示是一种可能性,但我的感觉是这将是一个维护问题。

最佳答案

您正在重新发明 Java 序列化默认已完成的操作:

引自 http://docs.oracle.com/javase/1.5.0/docs/guide/serialization/relnotes15.html :

Support has been added to serialization to handle enumerated types, which are new in version 5.0. The rules for serializing an enum instance differ from those for serializing an "ordinary" serializable object: the serialized form of an enum instance consists only of its enum constant name, along with information identifying its base enum type. Deserialization behavior differs as well--the class information is used to find the appropriate enum class, and the Enum.valueOf method is called with that class and the received constant name in order to obtain the enum constant to return.

关于Java:Java5枚举自定义序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10206093/

相关文章:

java - 如何使用 Spring 正确关闭执行程序服务?

java - Java-使用现有的公钥文件加密字符串

c++ - 在 C++ 中使用枚举进行 for 循环

vb.net - 傻瓜的简单 JSON 反序列化

c++ - Boost 序列化仅适用于 main?继续说 "has no member named ‘serialize’ “当我在其他对象中使用时

c++ - “obj_type”未在此范围内声明,注意 : suggested alternative: enum Value_type

c++ - 将枚举值分配给整数崩溃程序

java - 如何使用 java 8 stream api 更正编写代码?

java - android 在新线程中运行类

java - 序列化 ClassPathXmlApplicationContext