Java 的序列化对象表示

发布于 2024-08-06 11:39:59 字数 128 浏览 9 评论 0原文

我正在寻找 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

江南月 2024-08-13 11:39:59

如果您有两个对象,其所有属性都设置为相同的值,那么它们将以相同的方式序列化。

如果它不能重复,那就没有用了!

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!

一个人练习一个人 2024-08-13 11:39:59

他们总是以相同的方式序列化它。如果不是这种情况,就无法保证另一个程序可以正确地反序列化数据,从而违背了序列化的目的。

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.

妄想挽回 2024-08-13 11:39:59

通常,使用相同的数据运行相同的单线程算法将产生相同的结果。

但是,诸如 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.

粉红×色少女 2024-08-13 11:39:59

我喜欢 @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.

淡淡的优雅 2024-08-13 11:39:59

我正在寻找Java支持的格式
用于序列化对象。

不是愚蠢的,它以某种方式写下了它们。具体如何可以而且可能应该由您决定。一个角色映射到......呃,它涉及到,但不是重新发明轮子,让我们确切地问一下,你需要什么来将一个对象重建到什么状态?

默认序列化序列化
二进制格式的对象。

所以? (再次强调,不要试图变得愚蠢 - 听起来我们需要定义一个可能没有数据概念的问题)

我很好奇是否有两次运行
程序可以序列化同一个对象
不同。

如果您有信息流,您将如何确定对象需要恢复到什么状态?

I'm looking for the format that Java
uses to serialize objects.

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文