java标准序列化顺序 [英] java standard serialization order

查看:691
本文介绍了java标准序列化顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to know in which order the attributes of the following example class would be serialized:

public class Example implements Serializable {

   private static final long serialVersionUID = 8845294179690379902L;

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

编辑:

为什么我想知道这个:

我在一个类的文件中有一个序列化字符串没有
readObject()或writeObject()的实现。现在实现已更改(某些属性已消失)
,我想编写一个处理旧序列化类的readObject()方法。

I got a serialized string in a file for a class of mine which has no implementation for readObject() or writeObject(). now that implementation changed ( some properties are gone ) and i want to write a readObject() method that handles the old serialized class.

我只会阅读此属性,但不会将其保存到创建的对象中。

There i would just read this property but wouldnt save it to the created object.

这基本上只适用于遗产我现在使用数据库但需要支持旧的
序列化文件。

This is basically just for legacy i use a database now but need to support the old serialized files.

写这个readObject()我需要流中属性的顺序。

to write this readObject() i need the order of properties that in the stream.

推荐答案

基于对规范的简要阅读。

Based on a brief reading of the spec.


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

  • The fields are written in the order of the field descriptors the class descriptor

字段描述符采用规范顺序其定义如下:

The field descriptors are in "canonical order" which is defined as follows:


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


(我怀疑关于规范顺序的一点不重要。序列化中字段的实际顺序应该可以从实际中恢复同一序列化中类描述符中的字段描述符的顺序。我怀疑指定规范顺序的原因是它影响计算的序列化ID。但我很容易就是w rong about this :-))

参考:

  • Java Object Serialization Specification - Section 4.3 and Section 6.4.1 (the comment on the "nowrclass" production)

这篇关于java标准序列化顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆