什么是序列化一回事呢? [英] what is serialization all about?

查看:161
本文介绍了什么是序列化一回事呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

究竟哪里序列化进入了吗?我对'网了解serializtion,我已经知道了。




这是一个接口,如果在一个类实现,也就是说,它可以自动进行序列化,并通过不同的序列化反序列化。




给我一​​个很好的理由,当将一个类必须序列化?假设一旦它的序列化,到底发生了什么?


解决方案

需要序列化每当一个对象需要被持久化或超出其存在的范围内传播。



持久性是保存在某个地方的对象,后来与相同状态加载的能力。例如:




  • 您可能需要在磁盘上的一个对象实例存储为文件的一部分

  • 您可能需要存储在数据库作为一个blob对象(二进制大对象)。



传输是送能力其原始范围有些接收机以外的对象。例如:




  • 您可能需要一个对象的实例发送到远程计算机

  • 你可能需要一个实例发送到同一台机器上的另一个应用程序域或进程。



对于这些,必须有一些可存储,传送,然后串行位表示稍后用于重建原始对象。转动一个对象到该系列位中的该过程被称为序列,而转向的一系列位到原始对象的过程被称为反序列化



对象的序列化形式的实际表示可以根据你的目标是不同的。例如,在C#中,同时具有XML序列化(通过 的XmlSerializer 类)和二进制序列化(通过使用的 的BinaryFormatter 类)。根据您的需求,您甚至可以编写自己的自定义序列做额外的工作,如压缩或加密。如果你需要一个语言和平台无关的序列化格式,你可以尝试谷歌的 Protocol Buffers的目前拥有的用于.NET 支持(我没有使用过这一点)。



XML表示上面提到的是良好的标准格式存储的对象,但它可以是冗长而缓慢根据您的需要。二进制表示节省了空间,但不跨语言和运行时一样便携XML是。最重要一点是,串行器和解串必须了解对方。这可能是一个问题,当你开始向前和向后兼容性和版本引入



潜在的系列化兼容性问题的一个例子:




  • 您释放你的程序的1.0版本,它能够序列化一些对象到一个文件。

  • 的用户做了一些行动,以拯救他的到一个文件中。

  • 您释放你的程序的2.0版本的更新

  • 的用户试图打开1.0版本文件与2.0版程序。



这可能会很麻烦,如果2.0版本有附加属性的版本1.0 美孚没有。你必须显式不支持此方案还是有一些版本的故事与你的序列化。 .NET可以为你做一些这方面。在这种情况下,你可能也有相反的问题:用户可能会尝试打开一个2.0版与你的程序的1.0版本文件

$ B $。 b

我还没有使用这些技术自己,但.NET 2.0和更高版本拥有的版本容错序列同时支持向前和向后兼容性:




  • 公差无关的或意外的数据。这使得将数据发送到旧版本类型的新版本。缺少可选数据

  • 宽容。这使得旧版本将数据发送到较新版本。

  • 序列化回调。这使得智能默认值在数据丢失的情况下设置。



Where exactly does serialization comes into the picture? I read about serializtion on the 'net and I have come to know that

it is an interface that if implements in a class, means that it can be automatically be serialized and deserialized by the different serializers.

Give me a good reason why and when would a class needs to be serialized? Suppose once it's serialized, what happens exactly?

解决方案

Serialization is needed whenever an object needs to be persisted or transmitted beyond the scope of its existence.

Persistence is the ability to save an object somewhere and load it later with the same state. For example:

  • You might need to store an object instance on disk as part of a file.
  • You might need to store an object in a database as a blob (binary large object).

Transmission is the ability to send an object outside of its original scope to some receiver. For example:

  • You might need to transmit an instance of an object to a remote machine.
  • You might need to transmit an instance to another AppDomain or process on the same machine.

For each of these, there must be some serial bit representation that can be stored, communicated, and then later used to reconstitute the original object. The process of turning an object into this series of bits is called "serialization", while the process of turning the series of bits into the original object is called "deserialization".

The actual representation of the object in serialized form can differ depending on what your goals are. For example, in C#, you have both XML serialization (via the XmlSerializer class) and binary serialization (through use of the BinaryFormatter class). Depending on your needs, you can even write your own custom serializer to do additional work such as compression or encryption. If you need a language- and platform-neutral serialization format, you can try Google's Protocol Buffers which now has support for .NET (I have not used this).

The XML representation mentioned above is good for storing an object in a standard format, but it can be verbose and slow depending on your needs. The binary representation saves on space but isn't as portable across languages and runtimes as XML is. The important point is that the serializer and deserializer must understand each other. This can be a problem when you start introducing backward and forward compatibility and versioning.

An example of potential serialization compatibility issues:

  • You release version 1.0 of your program which is able to serialize some Foo object to a file.
  • The user does some action to save his Foo to a file.
  • You release version 2.0 of your program with an updated Foo.
  • The user tries to open the version 1.0 file with your version 2.0 program.

This can be troublesome if the version 2.0 Foo has additional properties that the version 1.0 Foo didn't. You have to either explicitly not support this scenario or have some versioning story with your serialization. .NET can do some of this for you. In this case, you might also have the reverse problem: the user might try to open a version 2.0 Foo file with version 1.0 of your program.

I have not used these techniques myself, but .NET 2.0 and later has support for version tolerant serialization to support both forward and backward compatibility:

  • Tolerance of extraneous or unexpected data. This enables newer versions of the type to send data to older versions.
  • Tolerance of missing optional data. This enables older versions to send data to newer versions.
  • Serialization callbacks. This enables intelligent default value setting in cases where data is missing.

这篇关于什么是序列化一回事呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆