Java 的序列化对象表示
我正在寻找 Java 用于序列化对象的格式。默认序列化以二进制格式序列化对象。特别是,我很想知道程序的两次运行是否可以以不同的方式序列化同一对象。
对象应该满足什么条件才能在 Java 默认的序列化/反序列化往返下保持其行为?
I'm looking for the format that Java uses to serialize objects. 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您需要位于 http://java 的 Java 对象序列化规范.sun.com/javase/6/docs/platform/serialization/spec/protocol.html。
You need the Java Object Serialization Specification at http://java.sun.com/javase/6/docs/platform/serialization/spec/protocol.html.
如果您有两个对象,其所有属性都设置为相同的值,那么它们将以相同的方式序列化。
如果它不能重复,那就没有用了!
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.
通常,使用相同的数据运行相同的单线程算法将产生相同的结果。
但是,诸如
HashSet
序列化条目的顺序之类的事情无法保证。事实上,对象在序列化时可能会发生微妙的改变。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. Indeed, an object may be subtly altered when serialised.我喜欢 @Stephen C 的 Object.hashCode() 示例。如果将这种不确定性哈希码序列化,那么当我们反序列化时,哈希码将没有用处。例如,如果我们序列化基于 Object.hashCode() 工作的 HashMap,其反序列化版本的行为将与原始映射不同。也就是说,查找相同的对象会在两个地图中给出不同的结果。
I like @Stephen C's example of 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. That is, looking up the same object would give us different results in the two maps.
如果您不需要二进制文件,则可以使用 JSON (http://www.json.org/example .html) 在 java http://www.json.org/java/
或者 XML http:// /www.developer.com/xml/article.php/1377961/Serializing-Java-Objects-as-XML.htm
If you don't want binary then you can use JSON (http://www.json.org/example.html) in java http://www.json.org/java/
Or XML for that matter http://www.developer.com/xml/article.php/1377961/Serializing-Java-Objects-as-XML.htm
不是愚蠢的,它以某种方式写下了它们。具体如何可以而且可能应该由您决定。一个角色映射到......呃,它涉及到,但不是重新发明轮子,让我们确切地问一下,你需要什么来将一个对象重建到什么状态?
所以? (再次强调,不要试图变得愚蠢 - 听起来我们需要定义一个可能没有数据概念的问题)
如果您有信息流,您将如何确定对象需要恢复到什么状态?
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?
So? ( again, not trying to be inane - sounds like we need to define a problem that may not have data concepted )
If you had a Stream of information, how would you determine what states the object needed to be restored to?