枚舉反序列化原理
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類型記錄可序列化的字段和數據是不必要的,因為發送的數據類型沒有變化。

浙公網安備 33010602011771號