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