Generally speaking, you should accept the default serialized form only if it is largely identical to the encoding that you would choose if you were designing a custom serialized form
The default serialized form of an object is a reasonably efficient encoding of the physical representation of the object graph rooted at the object. In other words, it describes the data contained in the object and in every object that is reachable from this object. It also describes the topology by which all of these objects are interlinked. The ideal serialized form of an object contains only the logical data represented by the object. It's independent of the physical representation.
The default serialized form is likely to be appropriate if an object's physical representation is identical to its logical content.
The instance fields in Name precisely mirror this logical content.
Even if you decide that the default serialized form is appropriate, you often must provide a readObject method to ensure invariants and security.
The presence of the @serial tag tells the Javadoc utility to place this documentation on a special page that documents serialized forms.
Logically speaking, this class represents a sequence of strings. Physically, it represents the sequence as a doubly linked list. If you accept the default serialized form, the serialized form will painstakingly mirror every entry in the linked list and all the links between the entries, in both directions.
Using the default serialized form when an object's physical representation differs substantially from its logical data content has four disadvantages:
1. It permanently ties the exported API to the current internal representation.
2. It can consume excessive space.
3. It can consume excessive time.
4. It can cause stack overflows.
Here is a revised version of StringList containing writeObject and readObject methods implementing this serialized form. As a reminder, the transient modifier indicates that an instance field is to be omitted from a class's default serialized form.
Note that the first thing writeObject does is to invoke defaultWriteObject, and the first thing readObject does is to invoke defaultReadObject, even though all of the StringList's field are transient.
If all fields are transient, it's technically permissible to dispense with invoking defaultWriteObject and defaultReadObject, but it's not recommended
Consider the case of a hash table. The physical representation is a sequence of hash buckets containing key-value entries. The bucket that an entry resides in is a function of the hash code of its key, which is not, in general, guaranteed to be the same from JVM implementation to JVM implementation. In fact, it isn't even guaranteed to be the same from run to run. Therefore, accepting the default serialized form for a hash table would constitue a serious bug. Serializing and deserializing the hash table could yield an object whose invariants were seriously corrupt.
Whether or not you use the default serialized form, every instance field that is not labeled transient will be serialized when the defaultWriteObject method is invoked. Therefore, every instance filed that can be made transiant should be mode so.
Before deciding to make a field nontransient, convince yourself that its value is part of the logical state of the object
Regardless of what serialized form you choose, declare an explicit serial version UID in every serializable class you write. This eliminates the serial version UID as a potential source of incompatibility. There is also a small performance benefit. If no serial versino UID is provided, an expensive computation is required to generate one at runtime.
You can generate the value by running the serialver utility on the class, but it's also fine to pick a number out of thin air.
If you ever want to make a new version of a class that is incompatible with existing versions, merely change the value in the serial version UID declaration. This will cause attempts to deserialize serialized instances of previous versions to fail with InvalidClassException
To summary, when you have decided that a class should be serialable, think hard about what the serialized form should be. Use the default serialized form only if it is a reasonable description of the logical state of the object. Otherwise, design a custom serialized form that aptly describes the object.
You should allocate as much time to designing the serialized form of a class as you allocate the designing its exported methods
Just as you cannot eliminate exported methods from future versions, you cannot eliminate fields from the serialized from; they must be preserved forever to ensure serialization compatibility. Choosing the wrong serialized form can have a permanent, negative impact on the complexity and performance of a class.
The default serialized form of an object is a reasonably efficient encoding of the physical representation of the object graph rooted at the object. In other words, it describes the data contained in the object and in every object that is reachable from this object. It also describes the topology by which all of these objects are interlinked. The ideal serialized form of an object contains only the logical data represented by the object. It's independent of the physical representation.
The default serialized form is likely to be appropriate if an object's physical representation is identical to its logical content.
The instance fields in Name precisely mirror this logical content.
Even if you decide that the default serialized form is appropriate, you often must provide a readObject method to ensure invariants and security.
The presence of the @serial tag tells the Javadoc utility to place this documentation on a special page that documents serialized forms.
Logically speaking, this class represents a sequence of strings. Physically, it represents the sequence as a doubly linked list. If you accept the default serialized form, the serialized form will painstakingly mirror every entry in the linked list and all the links between the entries, in both directions.
Using the default serialized form when an object's physical representation differs substantially from its logical data content has four disadvantages:
1. It permanently ties the exported API to the current internal representation.
2. It can consume excessive space.
3. It can consume excessive time.
4. It can cause stack overflows.
Here is a revised version of StringList containing writeObject and readObject methods implementing this serialized form. As a reminder, the transient modifier indicates that an instance field is to be omitted from a class's default serialized form.
Note that the first thing writeObject does is to invoke defaultWriteObject, and the first thing readObject does is to invoke defaultReadObject, even though all of the StringList's field are transient.
If all fields are transient, it's technically permissible to dispense with invoking defaultWriteObject and defaultReadObject, but it's not recommended
Consider the case of a hash table. The physical representation is a sequence of hash buckets containing key-value entries. The bucket that an entry resides in is a function of the hash code of its key, which is not, in general, guaranteed to be the same from JVM implementation to JVM implementation. In fact, it isn't even guaranteed to be the same from run to run. Therefore, accepting the default serialized form for a hash table would constitue a serious bug. Serializing and deserializing the hash table could yield an object whose invariants were seriously corrupt.
Whether or not you use the default serialized form, every instance field that is not labeled transient will be serialized when the defaultWriteObject method is invoked. Therefore, every instance filed that can be made transiant should be mode so.
Before deciding to make a field nontransient, convince yourself that its value is part of the logical state of the object
Regardless of what serialized form you choose, declare an explicit serial version UID in every serializable class you write. This eliminates the serial version UID as a potential source of incompatibility. There is also a small performance benefit. If no serial versino UID is provided, an expensive computation is required to generate one at runtime.
You can generate the value by running the serialver utility on the class, but it's also fine to pick a number out of thin air.
If you ever want to make a new version of a class that is incompatible with existing versions, merely change the value in the serial version UID declaration. This will cause attempts to deserialize serialized instances of previous versions to fail with InvalidClassException
To summary, when you have decided that a class should be serialable, think hard about what the serialized form should be. Use the default serialized form only if it is a reasonable description of the logical state of the object. Otherwise, design a custom serialized form that aptly describes the object.
You should allocate as much time to designing the serialized form of a class as you allocate the designing its exported methods
Just as you cannot eliminate exported methods from future versions, you cannot eliminate fields from the serialized from; they must be preserved forever to ensure serialization compatibility. Choosing the wrong serialized form can have a permanent, negative impact on the complexity and performance of a class.
发表评论
-
Consider serialization proxies instead of serialized instances
2012-10-02 16:32 607The serialization proxy pattern ... -
Write readObject methods defensively
2012-10-02 14:59 656The problem is that the readObj ... -
Prefer executors and tasks to threads
2012-09-30 15:52 838By using executor service, you ... -
Item 61: Throw exceptions appropriate to the abstraction
2012-09-28 21:27 675Higher layers should catch lowe ... -
Favor the use of standard exceptions
2012-09-28 21:19 1One of the attributes that most ...
相关推荐
Item 87: Consider using a custom serialized form Item 88: Write readObject methods defensively Item 89: For instance control, prefer enum types to readResolve Item 90: Consider serialization proxies ...
"Eloquent-Serialized-Lob" 是一个专门为Laravel5设计的Eloquent扩展,它允许我们序列化LOB(Large Object)类型的字段,比如BLOB或CLOB。LOB字段通常用来存储大块的数据,如图片、文档或长文本。 首先,我们需要...
### 串行中断(Serialized IRQ)支持在PCI系统中的应用 #### 一、概述 《串行中断(Serialized IRQ)支持在PCI系统中的应用》这一文档由Compaq Computer Corporation、Cirrus Logic Incorporated、National ...
事件模块名称是可配置的(默认值为serializedForm )。 "miidName" : { "module" : "github/jillix/form-serializer/MODULE_VERSION" , "roles" : [ MONO_ROLES ] , "config" : { ...
in class that provides a 'hot' look button using the _TrackMouseEvent function(17KB)<END><br>52,CLedButton_src.zip A button that looks like a LED.(24KB)<END><br>53,EllipticalButtons.zip A class ...
Timeline Custom Chart Type - Looking for a way to view event data in the form of a timeline? Dundas Chart for .NET V7.0 gives you the ability to do this. Other New Features in V7.0 - Additional new ...
从移植到 CoffeeScript,并修复了错误。 ...下载 1836B (100%) 1147B (62%) 1009B (54%) 用法 < form action ="" method =" get " >...var serialized = serialize ( form ) ; // fullname=James Bond&
- Hassle-free particle resolution adjust using a single parameter. - Supports high density ratios in multiphase simulations. - Simulation state gets serialized -> save your fluids mid-simulation->...
Unity是世界上最受欢迎的游戏开发引擎之一,它被广泛用于创建3D和2D游戏,以及许多交互式内容。在本文中,我们将深入探讨“Unity建筑物生长切片动画效果”这一主题,这是一种创新的视觉技巧,可以为游戏或模拟场景...
### Serial ATA:High Speed Serialized AT Attachment #### 一、引言 Serial ATA(SATA)是一种计算机总线接口,用于连接主机总线适配器到大规模存储设备,如硬盘驱动器(HDD)和固态驱动器(SSD)。本修订版1.0...
资源分类:Python库 所属语言:Python 资源全名:serialized-redis-interface-0.3.1.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
- Hassle-free particle resolution adjust using a single parameter. - Supports high density ratios in multiphase simulations. - Simulation state gets serialized -> save your fluids mid-simulation->...
《Attacking JAVA Serialized Communication》是关于Java序列化通信安全性的深度探讨。在Java编程中,序列化是一个常用的技术,它允许对象的状态被保存以便在不同的时间或不同的系统之间进行传输和恢复。然而,这个...
序列化Redis 一种使用redis-py序列化列表和字典的解决方案。 安装 要安装serialized_redis,只需: pip install serialized-redis 更新方法 放 得到 ... get ( 'datas' )) # It will return a dict
Everything is automatically serialized using the Kryo serialization library. Data is stored by an implementation of hashmaps on disk. QuickStart To use it with gradle (using jitpack.io): Add it in ...
use serde::{Deserialize, Serialize};...("serialized = {}", serialized); // Convert the JSON string back to a Point. let deserialized: Point = serde_json::from_str(&serialized).unwrap(); // Prints
Serialized Objects 842 Monitoring the File Structure 847 Summary 855 Exercises 855 Chapter 25: XML 856 XML Documents 856 Using XML in Your Application 865 Summary 884 ...
TYPEERROR: In order to allow non-dict objects to be serialized set the safe parmeter to False 解决: return JsonResponse({“name”: “tom”}, safe=False) 增加safe=false,使其接受列表 补充知识:...
or add a few lines of code to your existing class, and everything serializable shall be serialized. Yes, even polymorphic types! Odin serialized prefabs are deprecated in 2018.3+ due to
Java中实现深拷贝的两种方式—clone() & Serialized 在Java中,实现深拷贝是一种常见的需求,特别是在对象之间存在多层次关系的情况下。深拷贝可以帮助我们在对象之间保持独立性,并避免了对象之间的耦合关系。在...