简体   繁体   English

Java的序列化对象表示

[英]Java's Representation of Serialized Objects

I'm looking for the format that Java uses to serialize objects. 我正在寻找Java用于序列化对象的格式。 The default serialization serializes the object in a binary format. 默认序列化以二进制格式序列化对象。 In particular, I'm curious to know if two runs of a program can serialize the same object differently. 特别是,我很想知道程序的两次运行是否可以不同地序列化同一个对象。

What condition should an object satisfy so that the object maintains its behavior under Java's default serialization/deserialization round-trip? 对象应该满足什么条件,以便对象在Java的默认序列化/反序列化往返中保持其行为?

If you have two objects with all properties set to identical values, then they will be serialized the same way. 如果有两个对象,所有属性都设置为相同的值,那么它们将以相同的方式序列化。

If it weren't repeatable, then it wouldn't be useful! 如果它不可重复,那么它就没用了!

They will always serialize it the same way. 它们总是以相同的方式序列化它。 If this wasn't the case, there would be no guarantee that another program could de-serialize the data correctly, defeating the purpose of serialization. 如果不是这种情况,则不能保证另一个程序可以正确地反序列化数据,从而破坏了序列化的目的。

Typically running the same single-threaded algorithm with the same data will result in the same result. 通常,使用相同数据运行相同的单线程算法将产生相同的结果。

However, things such as the order with which a HashSet serialises entries is not guaranteed. 但是,诸如HashSet序列化条目的顺序之类的事情无法保证。 Indeed, an object may be subtly altered when serialised. 实际上,在序列化时可以巧妙地改变对象。

I like @Stephen C's example of Object.hashCode(). 我喜欢@Stephen C的Object.hashCode()示例。 If such nondeterministic hash codes are serialized, then when we deserialize, the hash codes will be of no use. 如果这样的非确定性哈希码被序列化,那么当我们反序列化时,哈希码将没有用。 For example, if we serialize a HashMap that works based on Object.hashCode(), its deserialized version would behave differently than the original map. 例如,如果我们序列化基于Object.hashCode()工作的HashMap,则其反序列化版本的行为将与原始地图不同。 That is, looking up the same object would give us different results in the two maps. 也就是说,查找同一个对象会在两个地图中给出不同的结果。

If you don't want binary then you can use JSON ( http://www.json.org/example.html ) in java http://www.json.org/java/ 如果您不想要二进制文件,那么您可以在java http://www.json.org/java/中使用JSON( http://www.json.org/example.html

Or XML for that matter http://www.developer.com/xml/article.php/1377961/Serializing-Java-Objects-as-XML.htm 或者就此而言的XML http://www.developer.com/xml/article.php/1377961/Serializing-Java-Objects-as-XML.htm

I'm looking for the format that Java uses to serialize objects. 我正在寻找Java用于序列化对象的格式。

Not to be inane, it writes them somehow. 不要失意,它会以某种方式写出来。 How exactly that is can and probably should be determined by you. 究竟是怎样的,可能应该由你决定。 A Character maps to .... uh, it gets involved but rather than re-inventing the wheel let us ask exactly what do you need to have available to reconstruct an object to what state? 一个角色映射到......呃,它参与了但是我们不是要重新发明轮子,而是让我们确切地问你需要什么才能将一个物体重建到一个状态?

The default serialization serializes the object in a binary format. 默认序列化以二进制格式序列化对象。

So? 所以? ( again, not trying to be inane - sounds like we need to define a problem that may not have data concepted ) (再次,不要试图失败 - 听起来我们需要定义一个可能没有数据隐藏的问题)

I'm curious to know if two runs of a program can serialize the same object differently. 我很想知道程序的两次运行是否可以不同地序列化同一个对象。

If you had a Stream of information, how would you determine what states the object needed to be restored to? 如果您有信息流,您将如何确定需要恢复对象的状态?

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

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