处理不再存在的枚举值的反序列化 [英] Handling deserialization of enum values that no longer exist

查看:199
本文介绍了处理不再存在的枚举值的反序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含3个值的枚举JJJ:A,B和C.在我的程序的先前版本中,它有一个额外的值:D。我希望能够读入之前创建的序列化对象该程序的版本,但在序列化对象中遇到值为D的JJJ类型变量时会抛出异常。最理想的情况是,我希望能够拦截反序列化过程并告诉它在遇到它时将D'映射到C'。

I have an enum JJJ that has 3 values: A, B, and C. In previous versions of my program it had one additional value: D. I'd like to be able to read in the serialized objects created by previous versions of the program, but an exception is thrown when it encounters a JJJ type variable with value 'D' in the serialized object. Optimally, I'd like to be able to intercept the deserialization process and tell it to just map D's to C's when it encounters them.

根据 http://docs.oracle.com/javase/6/docs/ platform / serialization / spec / serial-arch.html (Enum Constants的序列化),它听起来并不像有一种简单的方法来做到这一点......我知道一种方法是覆盖readObject on包含JJJ类型的成员变量的类,但由于程序的大小和范围,这将是困难和痛苦的(许多可序列化的类具有JJJ类型的成员变量并且重写readObject来处理JJJ字段意味着我有手动处理所有其他字段)。

According to http://docs.oracle.com/javase/6/docs/platform/serialization/spec/serial-arch.html (Serialization of Enum Constants), it doesn't sound like there is a simple way to do this... I know that one approach would be to override readObject on the classes that contain member variables of type JJJ, but this would be difficult and painful due to the size and scope of the program (dozens of serializable classes have member vars of type JJJ and overriding readObject to handle the JJJ field means I'd have to manually handle all of the other fields as well).

我也尝试通过滚动自己的ObjectInputStream子类来解决这个问题,但不幸的是枚举反序列化位I真的需要 得到和覆盖来解决这个问题都是私人或包私人......

I've also attempted to solve this by rolling my own subclass of ObjectInputStream, but unfortunately the enum deserialization bits I really need to get at and override to solve this are all private or package private...

有什么建议吗?

推荐答案

仅供参考,对于那些试图处理此案例的其他可怜人来说,这是我最终实施的解决方案:

FYI, for any other poor souls out there trying to handle this case, this is the solution I ended up implementing:

I为枚举创建了一个特殊的接口,如下所示:

I created a special interface for enums that looks like this:

public interface SerialCompatEnum {
    public String convertOldValue(String oldValue);
}

然后我编写了一个类,它将原始的序列化java对象字节流作为输入,处理它,并将字节流的修改版本写入任意输出流。处理逻辑选取序列化字节流中的任何枚举实例,并使用convertOldValue方法将流中的枚举常量替换为实际存在于当前代码库中的更新字符串。我们首先通过这个处理器运行所有序列化对象,以防止由于缺少枚举常量而导致的完全失败。

I then wrote a class that takes an raw serialized java object byte stream as input, processes it, and writes a modified version of the byte stream to an arbitrary output stream. The processing logic picks up any enum instances in the serialized byte stream and uses the convertOldValue method to replace the enum constant in the stream with an updated string that actually exists in the current code base. We run all of our serialized object through this processor first to prevent total failures caused by missing enum constants.

不幸的是,这不是一个非常干净或简单的解决方案。 :/

Unfortunately, not a very clean or easy solution. :/

这篇关于处理不再存在的枚举值的反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆