为了序列化大型枚举实例,我正在考虑使用“序列化代理”,其中代理对象将是一个非常小的对象,唯一标识相应的枚举实例。但是我发现枚举的任何读/写对象或解析/替换方法都会被忽略。所以我的问题是,如何有效地序列化大型枚举实例?字符串/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/