java标准序列化顺序

2024-04-10

我想知道以下示例类的属性将按什么顺序序列化:

public class Example implements Serializable {

   private static final long serialVersionUID = 8845294179690379902L;

   public int score;
   public String name;
   public Date eventDate;
}

EDIT:

为什么我想知道这个:

我在我的一个类的文件中得到了一个序列化字符串,该类没有实现 readObject() 或 writeObject()。现在实现发生了变化(一些属性消失了) 我想编写一个 readObject() 方法来处理旧的序列化类。

在那里我只会读取这个属性,但不会将其保存到创建的对象中。

这基本上只是为了遗留,我现在使用数据库,但需要支持旧的 序列化文件。

要编写此 readObject() 我需要流中属性的顺序。


基于对规范的简要阅读。

  • 字段按照字段描述符和类描述符的顺序写入

  • 字段描述符采用“规范顺序”,定义如下:

    “原始类型字段的描述符首先按字段名称排序,然后是按字段名称排序的对象类型字段的描述符。名称使用 String.compareTo 排序。”


(I suspect that the bit about the canonical order should not matter. The actual order of the fields in the serialization should be recoverable from the actual order of the field descriptors in the class descriptor in the same serialization. I suspect that the reason a canonical order is specified is that it affects the computed serialization id. But I could easily be wrong about this :-) )


参考:

  • Java 对象序列化规范 http://docs.oracle.com/javase/7/docs/platform/serialization/spec/serialTOC.html- 第 4.3 节和第 6.4.1 节(对“nowrclass”生产的评论)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

java标准序列化顺序 的相关文章

随机推荐