Java 什么是序列化?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/633402/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is serialization?
提问by
I am getting started with OOP programming and would like to know what is the meaning of serialization in OOP parlance?
我开始使用 OOP 编程,想知道 OOP 术语中序列化的含义是什么?
回答by Andy White
Check this out, this will give you a good explanation:
看看这个,这会给你一个很好的解释:
http://en.wikipedia.org/wiki/Serialization
http://en.wikipedia.org/wiki/Serialization
I think the most common use of the term serialization has to do with converting a binary object into an XML (or other string) representation so that it can be stored in a database/file or sent across a network in a web service call. Deserialization is the reverse process - converting an XML/string back into an object.
我认为术语序列化最常见的用途是将二进制对象转换为 XML(或其他字符串)表示,以便它可以存储在数据库/文件中或通过网络在 Web 服务调用中发送。反序列化是相反的过程 - 将 XML/字符串转换回对象。
EDIT: Another term you might come across is marshalling/unmarshalling. Marshalling is basically the same concept as serializing, and unmarshalling is the same as deserializing.
编辑:您可能会遇到的另一个术语是编组/解组。编组与序列化基本相同,解组与反序列化相同。
回答by Andrew Barnett
Serialization is the process of turning an object in memory into a stream of bytes so you can do stuff like store it on disk or send it over the network.
序列化是将内存中的对象转换为字节流的过程,因此您可以执行诸如将其存储在磁盘上或通过网络发送之类的操作。
Deserialization is the reverse process: turning a stream of bytes into an object in memory.
反序列化是相反的过程:将字节流转换为内存中的对象。
回答by Mr.ElectroNick
Serialization is when object (a chunk of memory) translated in a form when object's state could be saved in file (as an example).
序列化是当对象(一块内存)以某种形式翻译时,对象的状态可以保存在文件中(例如)。
Just treat it as making cookies - object is a dough, cookie - is a serialized dough.
把它当作做饼干——对象是一个面团,饼干——是一个序列化的面团。
So by "serializing" you can send cookie to your friend.
因此,通过“序列化”,您可以将 cookie 发送给您的朋友。
Something like that :-)
类似的东西:-)
回答by Canavar
serialization is converting an object to storable bit sequence.
序列化正在将对象转换为可存储的位序列。
so you can save this sequence to a file, db or send over network.
因此您可以将此序列保存到文件、数据库或通过网络发送。
later you can deserialize it to the actual object and reuse it whenever you want.
稍后您可以将其反序列化为实际对象并在需要时重新使用它。
Web Servicesand AJAXis the most common example of serialization. The objects serialized before sending the response to the client.
Web 服务和AJAX是最常见的序列化示例。在将响应发送到客户端之前序列化的对象。
回答by Overflown
Serialization is turning data into a linear "string" of bytes.
序列化正在将数据转换为字节的线性“字符串”。
Others have said more or less the same thing, but I stress that computer models require that data fits in the one-dimensionally addressed RAM or persistent storage.
其他人或多或少说了同样的话,但我强调计算机模型要求数据适合一维寻址的 RAM 或持久存储。
Most things that are "data" are inherently serializable (even if you must reduce the abstract model to a linear one); not serializable are say a network connection or a complicated state-based machine like a parser.
大多数“数据”事物本质上是可序列化的(即使您必须将抽象模型简化为线性模型);不可序列化是说网络连接或复杂的基于状态的机器,如解析器。
回答by viswanathan
serialization has to do with converting a binary object into an XML (or other string) representation so that it can be stored in a database/file or sent across a network in a web service call. Deserialization is the reverse process - converting an XML/string back into an object.
序列化与将二进制对象转换为 XML(或其他字符串)表示有关,以便它可以存储在数据库/文件中或在 Web 服务调用中通过网络发送。反序列化是相反的过程 - 将 XML/字符串转换回对象。
回答by Dave Sherohman
Serializationis the process of converting unordered data (such as an object) into a seriesof tokens which can be used later to reconstruct the original data. The serialized form is most often a string of text, but doesn't have to be.
序列化是将无序数据(例如对象)转换为一系列标记的过程,这些标记可以在以后用于重建原始数据。序列化形式通常是一串文本,但并非必须如此。
回答by sriiii
serialization is nothing but transfering the java supported object to file supported form
序列化只不过是将 Java 支持的对象传输到文件支持的形式
(OR)
converting java supported form to network supported form..the main scope of the serialization is nothing but to transfering the data from one layer to the another layer...only serialized objects we can send over the network..
将java支持的形式转换为网络支持的形式..序列化的主要范围不过是将数据从一层传输到另一层......只有我们可以通过网络发送的序列化对象..
回答by David K Hill
When instantiating (constructing) the actual object(the thing) from a class (blueprint) there is a need to save the object (thing) by serializing it (breaking it down to its basic atomic structure) to a space in memory. (Kind of like Star Treks Transporter). You break the thing down into it stream of information that can be transported somewhere and stored. Then when you want to reconstruct the thing you just pull the atomically stored instance back into the object. Different from instaniation.
当从类(蓝图)实例化(构造)实际对象(事物)时,需要通过将对象(事物)序列化(将其分解为其基本原子结构)到内存中的空间来保存该对象(事物)。(有点像星际迷航运输车)。您将事物分解为可以在某处传输和存储的信息流。然后,当您想重建事物时,只需将原子存储的实例拉回对象即可。与实例化不同。
回答by Aniket Thakur
Simply speaking Serializationis a process of converting an Object into stream of bytes so that it can be transferred over a network or stored in a persistent storage.
简单地说,序列化是一个将对象转换为字节流的过程,以便它可以通过网络传输或存储在持久存储中。
Deserialization is the exact opposite - Fetch a stream of bytes from network or persistence storage and convert it back to the Object with the same state.
反序列化正好相反 - 从网络或持久性存储中获取字节流并将其转换回具有相同状态的对象。
The thing to understand is how those stream of bytes are interpreted or manipulated so that we get the exact same Object/ same state. There are various ways to achieve that. Some of them are -
需要理解的是如何解释或操作这些字节流,以便我们获得完全相同的对象/相同的状态。有多种方法可以实现这一目标。他们之中有一些是 -
- XML: Convert Object to XML, transfer it over a network or store it in a file/db. Retrieve it and convert it back to the object with same state. In Java we use JAXB(Java architecture for XML binding) library.(From java 6 it comes bundled with JDK).
- JSON: Same can be done by converting the Object to JSON(JavaScript Object notation). Again there is GSON library that can be used for this.
- Or we can use the Serialization that is provided by the OOP language itself. For example, in Java you can serialize an Object my making it implement
Serializable interface
and writing to Object Stream.
- XML:将对象转换为 XML,通过网络传输或将其存储在文件/数据库中。检索它并将其转换回具有相同状态的对象。在 Java 中,我们使用JAXB(用于 XML 绑定的 Java 架构)库。(从 Java 6 开始,它与 JDK 捆绑在一起)。
- JSON:同样可以通过将对象转换为JSON(JavaScript 对象表示法)来完成。同样有 GSON 库可用于此目的。
- 或者我们可以使用 OOP 语言本身提供的 Serialization。例如,在 Java 中,您可以序列化一个对象,使其实现
Serializable interface
并写入对象流。