枚举反序列化原理

https://docs.oracle.com/javase/1.5.0/docs/guide/serialization/spec/serial-arch.html#enum

Enum constants are serialized differently than ordinary serializable or externalizable objects. The serialized form of an enum constant consists solely of its name; field values of the constant are not present in the form. To serialize an enum constant, ObjectOutputStream writes the value returned by the enum constant’s name method. To deserialize an enum constant, ObjectInputStream reads the constant name from the stream; the deserialized constant is then obtained by calling the java.lang.Enum.valueOf method, passing the constant’s enum type along with the received constant name as arguments. Like other serializable or externalizable objects, enum constants can function as the targets of back references appearing subsequently in the serialization stream.

枚举常量的序列化方式不同于普通的可序列化或可外部化对象。枚举常量的序列化仅依靠其name组成(所有枚举都会继承Enum类,具体请参考枚举底层原理);该常量的值在表单中不会存在。为了序列化一个enum常量,ObjectOutputStream写入由enum常量的name方法返回的值。为了反序列化enum常量,ObjectInputStream从流中读取常量名称;然后通过调用java.lang.Enum.valueOf获得反序列化的常量。将常量的enum类型和接收到的常量名称作为参数传递。与其他可序列化或可外部化的对象一样,enum常量可以作为随后出现在序列化流中的反向引用的目标。

The process by which enum constants are serialized cannot be customized: any class-specific writeObject, readObject, readObjectNoData, writeReplace, and readResolve methods defined by enum types are ignored during serialization and deserialization. Similarly, any serialPersistentFields or serialVersionUID field declarations are also ignored–all enum types have a fixed serialVersionUID of 0L. Documenting serializable fields and data for enum types is unnecessary, since there is no variation in the type of data sent.

枚举常量序列化的过程不能自定义:枚举类型定义的任何类特定的writeObject、readObject、readObjectNoData、writeReplace和readResolve方法在序列化和反序列化过程中被忽略。类似地,任何serialPersistentFields或serialVersionUID字段声明也会被忽略——所有enum类型都有一个固定的serialVersionUID 0L。为enum类型记录可序列化的字段和数据是不必要的,因为发送的数据类型没有变化。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值