简体   繁体   English

如何使用不同的serialVersionUID反序列化对象

[英]How to deserialize object with different serialVersionUID

I'm aware that serialVersionUID is used for versioning a serializable class, so, how can I load a old serialized object with the new class? 我知道serialVersionUID用于对可序列化的类进行版本控制,因此,如何使用新类加载旧的序列化对象?

The only related question I found is: how to discover serialVersionUID of serialized objects 我发现的唯一相关问题是: 如何发现序列化对象的serialVersionUID

But still, how I would be able to deserialize the old object as an instance of the new updated class? 但是仍然如何将旧对象反序列化为新的更新类的实例?

You can set the serialVersionUID to any value needed (the expected vs actual values are normally seen in the error messages). 您可以将serialVersionUID设置为所需的任何值(通常在错误消息中看到预期值与实际值)。 Put the field like 把领域像

   private static final long serialVersionUID = 1L;

(replace 1 by the old value) and you will be able to read your old data. (用旧值替换1),就可以读取旧数据。

If you need to read both new and old format at the same time and they are actually somewhat different, best seems to give the new version of the class a new name use resolveObject to convert the old format into new if the leagacy class appears in the stream. 如果您需要同时读取新格式和旧格式,并且它们实际上有所不同,则最好给类的新版本起一个新名称,如果在版本中出现了旧版类,则可以使用resolveObject将旧格式转换为新格式。流。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM