简体   繁体   English

序列化:将字节转换为字节?

[英]Serialization: Converting bytes to bytes?

The object itself is a sequence of bytes and that is how does the machine understand all the data, whether it's object, text, images..etc. 对象本身是一个字节序列,这就是机器如何理解所有数据,无论是对象,文本,图像等。 Could you clear this idea for me why we are converting a sequence of bytes (object) into another byte? 您能否为我清除这个想法,为什么我们要将一个字节序列(对象)转换为另一个字节? Do we restructure the bytes when we do serialization, or make a template that holds this object to give it a special meaning when transmitted over the network? 在进行序列化时,我们是否重组字节,还是制作一个包含该对象的模板以在通过网络传输时赋予它特殊的含义? suppose a certain method, that takes the object from memory as it is, and put that object into an IP datagrams and send it through the network, what issue that may arise? 假设某种方法将对象直接从内存中取出,然后将该对象放入IP数据报中并通过网络发送,可能会发生什么问题?

First: compression. 第一:压缩。

You must understand, that image file on disk and image file rendered from memory - are not the same. 您必须了解,磁盘上的映像文件和从内存呈现的映像文件是不同的。 On disk they (usually, forget about BMP) are compressed. 在磁盘上,它们(通常不用担心BMP)被压缩。 With current network throughput and hdd's capacities, compressing is essential. 利用当前的网络吞吐量和硬盘的容量,压缩是必不可少的。

Second: architecture. 第二:建筑。

Number in memory is just a sequence of bits, yes. 内存中的数字只是一个位序列,是的。 But, what bit-count is counted as number? 但是,什么位数算作数字呢? 8? 8? 16? 16吗 32? 32? 64? 64吗? Any of them. 任何一位。 There are bytes, words, integers, longs, floats (hell, floats!) and another couple of dozens of them. 有字节,单词,整数,长整数,浮点数(地狱,浮点数!),还有另外几十个。 And bitorder also matters, so-called big-endian and little-endian. 位序也很重要,所谓的大端和小端。 So 123456789 on one (x86) machine is not the same number on another machine (x64, for example). 因此,一台(x86)机器上的123456789与另一台机器(例如x64)上的数字不同。

Third: file (read: transmission) format != object-in-memory format. 第三:文件(读取:传输)格式!=内存中对象格式。

Well, there is difference between data structure in file (or network packet), and when object loaded from that file in memory. 嗯,文件(或网络数据包)中的数据结构与从该文件中的对象加载到内存中的时间之间存在差异。 And additionally, object-in-memory structure can differ from program version to version. 此外,不同程序版本的内存对象结构也可能不同。 Loaded-to-memory image in Win 3.1 and, fe, Vista is a hell big difference. 在Win 3.1中,加载到内存的映像以及其他功能,Vista是一个巨大的区别。 Also, structures packing and 4-, 8-, 16-, 32-bit-boundary aligning etc, etc, etc. 同样,结构打包和4位,8位,16位,32位边界对齐等,等等。

The object itself includes many references , which are pointers to where another component of the object happens to exist in memory on this particular machine at this particular moment. 对象本身包含许多引用 ,这些引用是指向该对象在该特定时刻恰好在该计算机上内存中存在该对象的另一个组件的位置的指针 The point of serialization is that it converts objects into bytes that can be read at some other time, possibly on some other machine. 序列化的要点是它将对象转换为可以在其他时间(可能在其他计算机上)读取的字节。

Additionally, object representations in memory are optimized for fast access and modification, not necessarily taking the minimum number of bytes. 此外,内存中的对象表示已针对快速访问和修改进行了优化,而不必占用最少的字节数。 Some serialization protocols, especially for use in RPCs or data storage, optimize for how many bytes have to be transmitted or stored using compression algorithms that make it more difficult to access or modify the properties of the object in exchange for using fewer bytes to do it. 一些序列化协议(尤其是用于RPC或数据存储中的序列化协议)使用压缩算法来优化必须传输或存储多少个字节,从而使访问或修改对象的属性变得更加困难,以换取使用更少的字节来完成操作。

The object itself is a sequence of bytes 对象本身是一个字节序列

No. The object itself isn't just a 'sequence of bytes', unless it contains nothing but primitive data. 否。对象本身只是一个“字节序列”,除非它只包含原始数据。 It can contain 它可以包含

  • references to other objects 引用其他对象
  • those objects may already have been serialized, in which case a back-reference needs to be serialized, not the referenced object all over again 这些对象可能已经被序列化,在这种情况下,需要对反向引用进行序列化,而不是再次对被引用的对象进行序列化
  • those references may be null 这些引用可能为空
  • there may be no object at all, just primitive data 可能根本没有对象,只有原始数据

All these things increase the complexity of the task well beyond the naive notion of just serializing 'a sequence of bytes'. 所有这些事情大大增加了任务的复杂性,远远超出了仅序列化“字节序列”的幼稚概念。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM