在Java中的序列化类:在更改字段名称或getter/setter名称后的兼容性

huangapple go评论86阅读模式
英文:

Class Serialization in Java: compatibility after changing field name or getter/setter name

问题

在Java中,如果我们修改一个可序列化类的成员的名称(仅名称)和/或其getter或setter,是否会导致兼容性错误?

英文:

Suppose we have a Serializable class in Java and we change the name (only the name) of a member of the class and/or its getter or setter, can it cause compatibility errors?

答案1

得分: 2

Renaming a field is equivalent to deleting which falls under the section Incompatible Changes in the specifications.

> 5.6.1 Incompatible Changes
不兼容的类更改是指那些无法保证互操作性的更改。在演化类时可能发生的不兼容更改包括:

>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.
然而,此默认值可能会对早期版本履行其合同的能力产生不利影响。

英文:

Renaming a field is equivalent to deleting which falls under the section Incompatible Changes in the specifications.

> 5.6.1 Incompatible Changes
Incompatible changes to classes are those changes for which the guarantee of interoperability cannot be maintained. The incompatible changes that may occur while evolving a class are:

>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.

huangapple
  • 本文由 发表于 2020年8月11日 19:38:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/63357351.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定