java - 为什么在反序列化期间从对象的类中删除变量不会引发异常?

标签 java serialization

为什么在反序列化期间从对象的类中删除变量不会引发异常?那么序列化中的“不兼容”变化意味着什么? 我还将类的字段从非静态更改为静态,根据 Java Specifications

不兼容。

最佳答案

Why removing the variable from the object's class during deserialization doesn't throw an Exception?

因为这是不应该的。请参阅Object Versioning Specification, #5.6.1 :

"Deleting fields - If a field is deleted in a class, the stream written will not contain its value. When the stream is read by an earlier class, the value of the field will be set to the default value because no value is available in the stream. However, this default value may adversely impair the ability of the earlier version to fulfill its contract."

"Changing a nonstatic field to static or a nontransient field to transient - When relying on default serialization, this change is equivalent to deleting a field from the class. This version of the class will not write that data to the stream, so it will not be available to be read by earlier versions of the class. As when deleting a field, the field of the earlier version will be initialized to the default value, which can cause the class to fail in unexpected ways."

没有关于抛出异常的内容。

关于java - 为什么在反序列化期间从对象的类中删除变量不会引发异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16954642/

相关文章:

apache-flex - 有没有工具可以将对象状态捕获到磁盘?

Django:使用直通参数序列化具有多对多关系的模型

java - 为什么即使存在默认构造函数,Java 也会报错 "No Valid Constructor"?

java - Netbeans 和 Selenium 入门

java - 创建子类的实例是否会自动创建其父类(super class)对象?

java - 创建 Kafka Producer 并调用方法 send()、flush() 和 close() 的正确顺序是什么?

php - 来自 PHP 的 Python Pickle 反序列化?

java - 如何在 Java 中自动运行 GWT 编译?

java - thymeleaf th :each adding coma between elements

java - 无法存储不可序列化对象的引用