java - 当读/写对象方法存在于可序列化中时,为什么要外部化

标签 java serialization deserialization externalizable

我无法消除我的困惑,

使用外部化是因为我们可以使用 read/writeExternal 方法给出我们自己的(更有效的)序列化-反序列化实现。并控制序列化过程。

我们还可以使用 read/writeObject 方法,序列化过程将被覆盖。

那么为什么还需要Externalized?

为什么有人更喜欢读/写外部而不是读/写对象?

最佳答案

只是序列化情况下的定制。

原因在 Externalizable 中给出文档

The writeExternal and readExternal methods of the Externalizable interface are implemented by a class to give the class complete control over the format and contents of the stream for an object and its supertypes. These methods must explicitly coordinate with the supertype to save its state. These methods supersede customized implementations of writeObject and readObject methods. Object Serialization uses the Serializable and Externalizable interfaces. Object persistence mechanisms can use them as well. Each object to be stored is tested for the Externalizable interface. If the object supports Externalizable, the writeExternal method is called. If the object does not support Externalizable and does implement Serializable, the object is saved using ObjectOutputStream. **

关于java - 当读/写对象方法存在于可序列化中时,为什么要外部化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49532046/

相关文章:

java - 在原始类类型上忽略显式方法类型参数;编译器错误?

java - LinkedBlockingQueue 在生产者-消费者场景中具有许多生产者的线程安全性

java - ArrayList.size() 不起作用

java - 如何使用 Jackson 流 API 序列化 map <Integer, Custom object>?

serialization - 如何在 AVRO IDL 中设置 map 的默认值

c# - 将 XML 中的 ebay web 服务响应转换为 C# 对象?

java - 方法重复输出

c# - 文档存储序列化

c# - 从 SQL Server Compact 反序列化会引发 "There is an unclosed literal string."错误

json - 使用 NancyFx 中的对象数组进行模型绑定(bind) POST