序列化中 readObject/writeObject 的使用 [英] Uses of readObject/writeObject in Serialization
问题描述
我正在阅读这篇文章以了解更多信息关于 Java 序列化过程.当谈到 readObject/writeObject
的使用时,我可以看到两个用例:
I was going through this article to understand more about Java Serialization process. When it comes to uses of readObject/writeObject
I could see two use cases:
- 我们可以使用
writeObject
在字节码被序列化之前对其进行加密.从安全的角度来看,这是一件好事. - 我们可以使用
readObject
来执行任何需要在反序列化后立即执行的特定代码段,当然,从 poin#1 开始,我们甚至可以使用readObject
解密在序列化对象时解密的字节码.
- We can use
writeObject
to encrypt the byte code before it gets serialized. From the security point of view, that's good thing. - we can use
readObject
to execute any specific piece of code that need to execute immediately after deserialization, and off course from poin#1, we can even usereadObject
to decrypt the byte code that was excrypted while serializing the object.
在通过编写自定义 readObject/writeObject 方法序列化/反序列化对象时,您是否遇到过其他任何实际场景?或者,如果您能指出我可以看到 readObject/writeObject 的一些体面和实际用途的任何地方?
Is there any other practical scenario you've come across while serializing/deserializing objects by writing customr readObject/writeObject method? Or If you could point me to any place where I could see some decent and practical uses of readObject/writeObject?
推荐答案
自定义 readObject
方法在您需要在对象反序列化后初始化瞬态(非序列化)字段时也很有用.
Custom readObject
methods are also useful when you need to initialize transient (non-serialized) fields after the object has been deserialized.
顺便说一句,看看 Effective Java,第 11 章(我不确定第二版中的章节/项目编号是什么.).这是一本关于序列化的优秀读物.
BTW, check out Effective Java, Chapter 11 (I'm not sure what the chapter/item number is in the 2nd ed.). It's an excellent read on serialization.
这篇关于序列化中 readObject/writeObject 的使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!