Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Original close reason(s) were not resolved
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(9)
关于连载的小故事
经过多年的努力,地球的科学家们开发出了一种可以帮助他们日常工作的机器人。但这个机器人的功能比火星科学家开发的机器人要少。
两颗行星的科学家开会后,决定火星将把他们的机器人送到地球。但出现了问题。向地球发送 100 个机器人的成本为 1 亿美元。旅行大约需要60天。
最后,火星科学家决定与地球科学家分享他们的秘密。这个秘密与类/机器人的结构有关。地球科学家在地球上开发了相同的结构。火星科学家序列化每个机器人的数据并将其发送到地球。地球科学家反序列化数据并将其相应地输入到每个机器人中。
这个过程节省了他们传输大量数据的时间。
一些机器人被用于火星上的一些防御工作。因此,他们的科学家在将数据发送到地球之前,将这些机器人的一些关键特性标记为“瞬态”。请注意,当对象反序列化时,瞬态属性将设置为 null(如果是引用)或默认值(如果是基本类型)。
地球科学家注意到的另一点是,火星科学家要求他们创建一些静态变量来保存有关环境的详细信息。这些细节被一些机器人使用。但火星科学家并没有透露这些细节。因为地球的环境和火星的环境不同。
尽管了解机器人类结构并拥有序列化数据,地球科学家仍无法反序列化使机器人工作的数据。
火星的科学家们正在等待全额付款。付款完成后,火星科学家与地球科学家共享序列版本 UID。地球科学家将其设置为机器人级别,一切开始工作。
更新
虽然在序列化的帮助下,他们能够使用信号而不是实际的宇宙飞船来发送数据,但他们意识到发送大量数据仍然是一个挑战。序列化使该过程更便宜、更快,但仍然很慢。因此,不同的科学家提出了不同的想法来减少数据大小。一些科学家建议压缩数据,另一些科学家建议使用不同的机制来表示它,以便可以将其反序列化回来。其中一些想法是 XML、JSON, msgpack, msgpack, 尼姆
Short story about serialization
After many years of hard work, Earth's scientists developed a robot who can help them in daily work. But this robot had fewer features than the robots developed by the scientists from planet Mars.
After a meeting between both planets' scientists, it is decided that Mars will send their robots to Earth. But a problem occurred. The cost of sending 100 robots to Earth was $100 million. And it takes around 60 days of traveling.
Finally, Mars' scientists decided to share their secret with Earth's scientists. This secret was about the structure of class/robot. Earth's scientists developed the same structure on Earth itself. Mars' scientists serialized the data of each robot and sent it to earth. Earth's scientists deserialized the data and fed it into each robot accordingly.
This process saved them time in communicating a massive amount of data.
Some of the robots were being used in some defensive work on Mars. So their scientists marked some crucial properties of those robots as transient before sending their data to Earth. Note that the transient property is set to null (in case of reference) or to the default value (in case of the primitive type) when the object gets deserialized.
One more point noticed by Earth's scientists is that Mars' scientists asked them to create some static variables to keep details about the environment. These details are used by some robots. But Mars' scientists don't share these details. Because Earth's environment was different from Mars' environment.
Even though knowing about the robot class structure and having serialized data Earth's scientist were not able to deserialize the data which can make robots working.
Mars' scientists were waiting for the complete payment. Once the payment was done Mars' scientists shared the serialversionUID with Earth's scientists. Earth's scientist set it to robot class and everything started working.
Update
Though with the help of serialization, they became able to send data using signals instead of actual spaceship, they realized that sending large set of data was still a challenge. Serialization make the process cheaper and faster but it was still slow. Hence the different scientists came up with different ideas to reduce the data size. Some scientists suggested to compress the data and some suggested to use different mechanism to represent it so it can be deserialized back. Some of the ideas are XML, JSON, msgpack, Nimn
当需要通过网络发送数据或存储在文件中时,通常会使用序列化。我所说的数据是指对象而不是文本。
现在的问题是您的网络基础设施和硬盘是理解位和字节但不理解 JAVA 对象的硬件组件。
序列化是将 Java 对象的值/状态转换为字节,以便通过网络发送或保存它。
这类似于您的语音通过 PSTN 电话线传输的方式。
Serialization is usually used When the need arises to send your data over network or stored in files. By data I mean objects and not text.
Now the problem is your Network infrastructure and your Hard disk are hardware components that understand bits and bytes but not JAVA objects.
Serialization is the translation of your Java object's values/states to bytes to send it over network or save it.
This is analogous to how your voice is transmitted over PSTN telephone lines.
Java 序列化(具体而言,Serialized和 Exernalizable 接口)允许您阅读/自动或手动从磁盘或从网络写入任意复杂的 Java 对象。 XML 和 JSON 是文本格式,而 Java 序列化是二进制格式。 (序列化也是简单读/写数据的一般概念,但由于问题是关于Java的,我假设你指的是内置序列化系统,即Serialized/Exernalizable)
“实现Serialized”相对于XML/的优点JSON
一开始,你几乎可以免费获得序列化。您无需对对象进行太多更改即可让序列化机制使用它。另一个优点是,因为它是二进制格式,所以它比文本格式紧凑得多,因此可能会使用更少的空间(这有利于节省网络带宽或节省磁盘上的存储空间)。
通过 XML/JSON“实现可序列化”的缺点
内置 Java 序列化的缺点是,如果您对对象进行更改,则使不同的序列化格式兼容确实是一场重大噩梦。此外,虽然您可以手动编辑 XML 和 JSON,但无法编辑序列化的 Java 对象(如果不将其读入 Java)。出于同样的原因,调试 XML 和 JSON 通常比二进制格式更容易,因为 XML 和 JSON 是人类可读的。 Java 内置序列化机制的另一个缺点是您无法(轻松)对其他编程语言的数据进行序列化/反序列化。
读取/写入数据的替代技术
除了 Java 的内置序列化之外,还有其他序列化技术可以为您提供两全其美的优点:紧凑的二进制格式、语言互操作、易于版本兼容性以及通常还可以轻松以可读格式转储二进制数据的调试工具。例如,Google 的开源 协议缓冲区 和 MessagePack 是序列化库/格式的示例,可让您读取/写入紧凑的二进制数据并轻松维护版本兼容性。与内置 Java 序列化相比,这些库的最大缺点是它们涉及用于序列化的普通旧数据对象(而不是功能更齐全的 Java 对象,这些对象也具有与其相关的行为);然而,这个缺点实际上是一个优点,因为将存储信息的数据模型与包装或派生它们的对象分开实际上是一种很好的编程实践,并且更容易支持多种格式。
用法
由于您询问的是需求,而不仅仅是定义,因此有许多用例:
只需保存数据以供以后使用。例如,假设您正在编写一款视频游戏。你的程序不会永远运行;即使它永远不会崩溃(希望是这种情况),您的用户可能会在某个时候退出程序,或者操作系统可能会终止程序以节省资源(例如,在 Android 上,用户不与之交互的后台进程经常会出现)并被操作系统故意杀死以回收 RAM 等系统资源)。为了确保用户不会从头开始,而是可以从原来的位置或最近的保存点恢复,您需要将游戏的状态写入持久存储(即硬盘驱动器,用户的 Google 云端硬盘帐户等)。为此,您需要将内存中表示游戏状态的数据结构转换为可以写入磁盘(或保存数据的任何系统)的原始字节。
从远程服务器检索信息。让我们继续游戏示例...假设您正在创建一个在线多人游戏,或者您希望能够在游戏中提供新的关卡或项目,而无需用户更新其应用程序。为此,您需要从服务器计算机(您将其用作各种设备上安装的应用程序的所有副本的联系点)传递有关在线玩家的信息或有关新级别/项目的信息到应用程序的各个副本。服务器和应用程序都需要这些数据结构的某种内存表示(例如其他玩家的位置、新关卡的结构、新项目的描述/图像等),但要传输信息从服务器到设备上的应用程序,通信系统由原始字节组成,因此有必要有一种方法将数据转换为原始字节,并从原始字节转换回有意义的内存中数据结构。
几乎两个不同进程/应用程序之间或应用程序与某些存储系统之间的任何通信都需要某种序列化机制。
Java serialization (and, specifically, the Serializable and Exernalizable interfaces) allows you to read/write arbitrarily complicated Java objects, automatically or manually from/to disk or from/to the network. Whereas XML and JSON are textual formats, Java serialization is a binary format. (Serialization is also a general concept of simply reading/writing data, but since the question is about Java, I assume you are referring to the builtin serialization system, i.e. Serializable/Exernalizable)
Advantages of "implements Serializable" over XML/JSON
At first, you get serialization pretty much for free. You don't need to make very many changes to your objects in order to let the serialization mechanism work with it. Another advantage is that, because it is a binary format, it is much more compact than the textual format, and so will probably use less space (which is good for conserving network bandwidth or for conserving storage space on disk).
Disadvantages "implements Serializable" over XML/JSON
The disadvantage to builtin Java serialization is that, if you make changes to your object, making the different serialization formats compatible can really be a major nightmare. Also, whereas you can manually edit XML and JSON, you cannot edit a serialized Java object (without reading it into Java). For the same reasons, it is often easier to debug XML and JSON than binary formats, because XML and JSON are human-readable. Another disadvantage with Java's builtin serialization mechanism is that you cannot (easily) serialize/deserialize the data from another programming language.
Alternative techniques for reading/writing data
There are alternative serialization techniques other than Java's builtin serialization that give you the best of both worlds: compact binary formats, language interoperation, ease of version compatibility, and often debugging tools as well that make it easy to dump the binary data in readable format. For example, Google's opensource protocol buffers and MessagePack are examples of serialization libraries/formats that let you read/write compact binary data and easily maintain version compatibility. The biggest downside of these libraries over builtin Java serialization is that they involve plain-old-data objects for serialization (as opposed to more fully-featured Java objects that also have behavior associated with them); however, this disadvantage is, in fact, an advantage as separating the data model to which/from which the information is stored from the objects that wrap or are derived from them is actually a good programming practice and makes it easier to support multiple formats.
Usage
Since you asked for the need, not merely the definition, there are a number of use cases:
Simply saving your data for use later. For example, let's say you are writing a video game. Your program will not run forever; even if it never crashes (which is hopefully the case), your user will probably exit the program at some point or the operating system may kill the program to save resources (e.g. on Android, background processes that the user is not interacting with are frequently and intentionally killed by the OS to reclaim system resources like RAM). In order to ensure that the user doesn't start from the very beginning and can instead resume from where they were or from the most recent save point, you will want to write the state of the game to persistent storage (i.e. the hard drive, the user's Google Drive account, etc.). In order to do this, you need to translate the data structures in memory that represent the state of the game to raw bytes that you can write to disk (or to whatever system you are saving the data).
Retrieving information from a remote server. Let's continue with the game example... suppose you are creating an online multiplayer game or that you want to make it possible to provide new levels or items in the game without the user updating their app. To do this, you would want the information about the online player or the information about the new levels/items to be communicated from a server computer (which you use as the point of contact for all the copies of the app installed on various devices) to the individual copies of the app. Both the server and app need some sort of in-memory representation of these data structures (e.g. the location of other players, the structure of a new level, the description/image for a new item, etc.), but to transfer the information from the server to the app on the device, the communication system consists of raw bytes, and so it is necessary to have a way to convert the data to raw bytes and from raw bytes back to a meaningful in-memory data structure.
Pretty much any communication between two different processes/apps or between an app and some storage system is a case where some sort of serialization mechanism is required.
当您想要将对象的状态保存到文件中或通过网络发送它时,您需要将其转换为一系列字节。这称为序列化。
Java 有一个内置机制,其他选项包括 XML 或 JSON。
需要此功能的示例:缓存对象、进行远程方法调用、将对象图保存到磁盘。
When you want to save an object's state into a file or send it over the network, you need to transform it into a series of bytes. This is called serialization.
Java has a built-in mechanism for that, other options include XML or JSON.
Examples when you need this: Caching objects, making remote method calls, saving an object graph to disk.
并且还可以使用序列化来实现对象克隆
And you can also implement object cloning using serialization
序列化通常指的是对象到一系列位的转换。这是java中必不可少的一个,因为java主要用于基于网络的应用程序,我的意思是使数据可以通过网络使用
Serialization generally referred as conversion of Object in to series of bits. it's an essential one in java since java mainly meant for web-based app's i mean to make the data to be available across the network
当从一个类向另一个类发送数据出现问题时,就需要序列化。
另一个类位于不同的位置或硬盘上。即在分布式系统中,
序列化的反向操作称为反序列化。
String 类和所有包装类默认都实现了可序列化接口。
Serialized 接口也是标记接口,它为您的类提供了序列化的能力。因此,如果我们想通过网络发送对象的状态,我们应该实现一个可序列化的接口
Serialization is needed when there is a problem in sending data from one class to another.
Where the other class is on a different location or Hard Disk. i.e. in Distributed Systems
The reverse operation of serialisation is called deserialization
The String Class and all wrapper classes implement serializable interface by default
Serializable interface is also marker interface which provides the capability of Serialization to your class. So, we should implement a serializable interface if we want to send the state of an object over the network