`
leonzhx
  • 浏览: 793306 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Item 75: Consider using a custom serialized form

阅读更多

1.  Do not accept the default serialized form without first considering whether it is appropriate. Accepting the default serialized form should be a conscious decision that this encoding is reasonable from the standpoint of flexibility, performance, and correctness.

 

2.  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. 

 

3.  The ideal serialized form of an object contains only the logical data represented by the object. It is 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.

 

4.  Even if you decide that the default serialized form is appropriate, you often must provide a readObject method to ensure invariants and security.

 

5.  The presence of the @serial tag tells the Javadoc utility to place this documentation on a special page that documents serialized forms. Like the @serial tag for fields, the @serialData tag for methods tells the Javadoc utility to place this documentation on the serialized forms page.

 

6.  Using the default serialized form when an object’s physical representation differs substantially from its logical data content (a doubly linked list) has four disadvantages:

    1)  It permanently ties the exported API to the current internal representation.

    2)  It can consume excessive space for implementation details.

    3)  It can consume excessive time. (Following the previous link for doubly linked list is not necessary.)

    4)  It can cause stack overflows. The default serialization procedure performs a recursive traversal of the object graph, which can cause stack overflows even for moderately sized object graphs.

 

7.  If all instance fields are transient, it is technically permissible to dispense with invoking defaultWriteObject and defaultReadObject, but it is not recommended. Even if all instance fields are transient, invoking defaultWriteObject affects the serialized form, resulting in greatly enhanced flexibility. The resulting serialized form makes it possible to add nontransient instance fields in a later release while preserving backward and forward compatibility. If an instance is serialized in a later version and deserialized in an earlier version, the added fields will be ignored. Had the earlier version’s readObject method failed to invoke defaultReadObject, the deserialization would fail with a StreamCorruptedException.

 

8.  Before deciding to make a field nontransient, convince yourself that its value is part of the logical state of the object. If you use a custom serialized form, most or all of the instance fields should be labeled transient.

 

9.  If you are using the default serialized form and you have labeled one or more fields transient, remember that these fields will be initialized to their default values when an instance is deserialized: null for object reference fields, zero for numeric primitive fields, and false for boolean fields

 

10.  Whether or not you use the default serialized form, you must impose any synchronization on object serialization that you would impose on any other method that reads the entire state of the object.

 

11.  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:

private static final long serialVersionUID = randomLongValue;

 

12.  If you modify an existing class that lacks a serial version UID, and you want the new version to accept existing serialized instances, you must use the value that was automatically generated for the old version. You can get this number by running the serialver utility on the old version of the class—the one for which serialized instances exist.

 

分享到:
评论

相关推荐

    Effective Java 3rd edition(Effective Java第三版英文原版)附第二版

    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 ...

    serialize-error:将错误序列化为一个普通对象

    序列化错误 将错误序列化/反序列化为普通对象 ...const serialized = serializeError ( error ) console . log ( serialized ) ; //=> {name: 'Error', message: ':unicorn:', stack: 'Error: :unicorn:\n

    Python库 | serialized-redis-interface-0.3.1.tar.gz

    资源分类:Python库 所属语言:Python 资源全名:serialized-redis-interface-0.3.1.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059

    Serial ATA:High Speed Serialized AT Attachment

    ### Serial ATA:High Speed Serialized AT Attachment #### 一、引言 Serial ATA(SATA)是一种计算机总线接口,用于连接主机总线适配器到大规模存储设备,如硬盘驱动器(HDD)和固态驱动器(SSD)。本修订版1.0...

    form-serializer:序列化表单对象并发出它的 Mono 模块

    事件模块名称是可配置的(默认值为serializedForm )。 "miidName" : { "module" : "github/jillix/form-serializer/MODULE_VERSION" , "roles" : [ MONO_ROLES ] , "config" : { "html" : "/path/to/...

    Laravel开发-eloquent-serialized-lob

    "Eloquent-Serialized-Lob" 是一个专门为Laravel5设计的Eloquent扩展,它允许我们序列化LOB(Large Object)类型的字段,比如BLOB或CLOB。LOB字段通常用来存储大块的数据,如图片、文档或长文本。 首先,我们需要...

    Serde 是一个 高效和通用的序列化和反序列化Rust数据结构的框架

    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 IRQ Support for PCI Systems

    ### 串行中断(Serialized IRQ)支持在PCI系统中的应用 #### 一、概述 《串行中断(Serialized IRQ)支持在PCI系统中的应用》这一文档由Compaq Computer Corporation、Cirrus Logic Incorporated、National ...

    lutaml:LutaML(“蜂蜜大战”)

    鲁塔姆功能性lutaml扩展名的入口点。... new ( "example.exp" ) ] ) # will produce Lutaml::LutamlPath::DocumentWrapper object with serialized express repository# example.yaml is an EXPRESS cac

    protobuf-3.8.0 VS2019 C++使用案例

    google::protobuf::io::ArrayInputStream input(serialized_data.data(), serialized_data.size()); if (!parsed_person.ParseFromZeroCopyStream(&input)) { // 处理错误 } // 打印解析的数据 std::cout << "Name...

    SerializedSwift:对当前Codable协议的重大增强,可以更好,更轻松地对JSON进行序列化和反序列化

    序列化的Swift GSON启发了Swift使用@propertyWrappers的JSON解码策略。 特征: 无需编写自己的init(from decoder: Decoder) 无需编写自己的CodingKeys子类 开箱即用地继承和合成 ... @Serialized ( default : 0 )

    GruntExtractCldrData.zip

    Grunt Extract Cldr Data 可抽取 CLDR 数据并且可以将其转换为 JavaScript 可用的数据。... return 'IntlMessageFormat.__addLocaleData(' serialized ');';  }  }  },  }, }) 标签:Grunt

    sparsam:Ruby的快速节俭绑定

    稀疏 Ruby的新Thrift绑定和生成器! 超级基本的例子 请参阅docs文件夹以获取更...irb(main):004:0> serialized = obj.serialize # turn object into string => "\x00" irb(main):005:0> obj2 = Sparsam::Deserializer

    Visual C++ 编程资源大全(英文源码 控件)

    readonlycombo_src.zip Show a disabled dropdown style combobox like a read only edit box.(2KB)<END><br>75,VBLikeCombo.zip Creates a combo box similar to those in Visual Basic.(27KB)<END><br>76,...

    serialized-redis:一种使用redis-py序列化列表和字典的解决方案

    序列化Redis 一种使用redis-py序列化列表和字典的解决方案。 安装 要安装serialized_redis,只需: pip install serialized-redis 更新方法 放 得到 ... get ( 'datas' )) # It will return a dict

    iOS Fuondation Framework Reference

    **Overview**: `NSArchiver` is a class that manages the archiving process for objects, allowing them to be serialized to a stream or file. It is commonly used for saving and restoring application state...

    chrome-unserialize-php-crx插件

    将php-serialized数据转换回用于人类可读的东西。 可以选择var_export或json 将php-serialized数据转换回用于人类可读的东西。 可以选择var_export或json 无法解决循环参考(如果真的需要,请在Github上提出问题) ...

    flatbuffers-java-1.12.0-API文档-中文版.zip

    赠送jar包:flatbuffers-java-1.12.0.jar; 赠送原API文档:flatbuffers-java-1.12.0-javadoc.jar; 赠送源代码:flatbuffers-java-1.12.0-sources.jar; 赠送Maven依赖信息文件:flatbuffers-java-1.12.0.pom;...

    Beginning Microsoft Visual CSharp 2008 Wiley Publishing(english)

    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 ...

    Obi Fluid 3.2

    - 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->...

Global site tag (gtag.js) - Google Analytics