`
cywhoyi
  • 浏览: 412917 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

序列化

    博客分类:
  • JAVA
 
阅读更多

This article explains version control in serialization.

If you serialize an object, then you must have the same class to deserialize it and use the object. If you modify the class then that can cause incompatibility and that can cause deserialization to fail. There are some changes you can make to a class that do not prevent deserialization, but some do.

Changes that affect deserialization

  1. Deleting an Instance Variable.
  2. Changing the type of an Instance Variable.
  3. Changing a non-transient variable to a transient variable.
  4. Moving a class up or down in hierarchy.
  5. Changing an Instance Variable to static.
  6. Changing class from serializable to non-serializable.

Changes that do not affect deserialization

  1. Changing transient variables to non-transient variables
  2. Changing the access level of an Instance Variable
  3. Create a new Instance Variable in a class.

Importance of SerialVersion UID

Each time the object is serialized, it is stamped with a version id of the object's class. This is called a serial version UID. When the object is deserialized, if the class has changed since serialization, then the class could have a different version id and deserialization fails. 
But it can be controlled by the user. If there is any possibility that the class may undergo some changes, then put the serial version UID in the class.

When Java tries to deserialize an object, it compares the serialized object version UID with that of the class version UID that the JVM is using for deserialization. If two numbers do not match, then the JVM assumes that the class is not compatible for deserialization and it throws an exception.

So, the solution is to put the version UID in the class. Since the class may undergo changes, the version UID will remain the same and deserialization can be done easily.

How to get the Version UID for the class

Open a command prompt, got to the folder where your Java file is saved and type "serialver classname".

Example:

Java.jpg

Now paste this version UID into your class.

Java1.jpg

分享到:
评论

相关推荐

    c#序列化和反序列化

    什么叫序列化? 我们都知道对象是暂时保存在内存中的,不能用U盘考走了,有时为了使用介质转移对象,并且把对象的状态保持下来,就需要把对象保存下来,这个过程就叫做序列化,通俗点,就是把人的魂(对象)收伏成一...

    Vbs序列化/反序列化Json基类

    Vbs序列化/反序列化Json基类 可直接使用,bug已被我修复 Example: Dim fso, json, str, o, i Set json = New VbsJson Set fso = WScript.CreateObject("Scripting.Filesystemobject") str = fso.OpenTextFile(...

    java 对象的序列化与反序列化

    java 对象的序列化与反序列化 java 对象的序列化与反序列化

    序列化和反序列化的封装类

    序列化和反序列化的封装类,只要调用就行 序列化和反序列化的封装类 序列化和反序列化的封装类 序列化和反序列化的封装类 序列化和反序列化的封装类

    C#对象序列化与反序列化

    1.对象序列化的介绍 (1).NET支持对象序列化的几种方式 二进制序列化:对象序列化之后是二进制形式的,通过BinaryFormatter类来实现的,这个类位于System.Runtime.Serialization.Formatters.Binary命名空间下。 SOAP...

    Java对象序列化和反序列化工具Xson.zip

    Xson是一个Java对象序列化和反序列化程序。支持Java对象到字节数组的序列化,和从字节数组到Java对象的反序列化。 Maven:  <groupId>com.github.xsonorg</groupId>  <artifactId>xson-core  <version>1.0.1 ...

    C++ JSON 序列化与反序列化

    C++ JSON 序列化与反序列化 相关的博客文章见:http://blog.csdn.net/TragicGuy

    序列化和反序列化dll文件和proto

    序列化和反序列化dll文件和proto

    C#序列化与反序列化(包括复杂xml对象)

    C# xml序列化及反序列化(解析xml为对象及对象转xml保存),方法一反序列化取节点KV值,方法二反序列化取节点标签值,方法三取节点KV值,并增加节点对象数组及节点标签组合反序列化

    json序列化与反序列化处理代码(java版本)

    极好的序列化与发序列化代码。可以处理array集合,数组或者单个对象等的序列化与反序列化。

    FastJson反序列化

    [{"listA":[{"id":"fds","name":"张三"},{"id":"fds","name":"李四"}],"test":"432"}, {"listA":[{"id":"fds","name":"张三"},{"id":"fds","name":"李四"}],...数组里面封装数组的反序列化方法,通过两个bean,进行封装

    C# 数组与序列化

    C# 数组与序列化C# 数组与序列化C# 数组与序列化C# 数组与序列化C# 数组与序列化C# 数组与序列化C# 数组与序列化

    jackson json序列化,反序列化所需jar包

    jackson json序列化 json反序列化 jar包 直接下载拷贝到项目中buildpath即可使用

    delphi_JSON序列化反序列化

    delphi_JSON序列化反序列化

    Unity 中使用Protobuf进行序列化和反序列化的Demo

    Unity 中使用Protobuf进行序列化和反序列化的Demo

    序列化和Zip压缩

    本Dll提供了二进制序列化和Xml序列化,序列化之后,可以对序列化数据,进行压缩。本dll提供了两种压缩方式,GZipStream和DeflateStream,在使用Webservice作为服务端的系统中,提高传输性能是比较关键的,而提高传输...

    序列化窗体所有控件

    反射与序列化示例-序列化窗体所有控件

    protobuf序列化和反序列化技术

    大数据场景下序列化和反序列化技术,谷歌提供技术 protobuf-jetbrains-plugin-0.13.0.zip

    基于VS2017开发的XML序列化和反序列化实例

    序列化和反序列化思路是通过一个类来映射XML,XML里面的元素,都被映射到类的成员变量。 业务处理不直接面对XML,而是面对类进行操作。反序列化是指把XML解析出来的类,序列化是指把类转换成XML字符串。所以我们的...

    java序列化(Serializable)的作用和反序列化.doc

    序列化是干什么的? 简单说就是为了保存在内存中的各种对象的状态(也就是实例变量,不是方法),并且可以把保存的对象状态再读出来。虽然你可以用你自己的各种各样的方法来保 存object states,但是Java给你提供一...

Global site tag (gtag.js) - Google Analytics