gpt4 book ai didi

java - 为什么 Jackson 实现 Serializable?

转载 作者:行者123 更新时间:2023-12-05 06:49:38 27 4
gpt4 key购买 nike

built-in serialization of Java objects to their binary forms现在在大多数项目中并不常见,它们倾向于使用 ProtoBufs、XML、JSON 等来进行服务器之间的数据传输。 CORBA 和所有似乎属于 2000 年代左右时代的项目。老实说,我只看到它实现过一次(在十年前的一个 J2EE 项目中),它看起来有点古怪。不过,每当 IntelliJ 提示某些类中缺少 serialVersionUID 时,我仍然会想起整个 Serializable 的事情!如今,这主要发生在我使用 Jackson 处理 JSON 序列化/反序列化时,因为它具有类型注释,表明某些类必须实现 Serializable。通常像

StdDeserializer<T> extends JsonDeserializer<T> implements Serializable, Gettable

(我没有看到 T 需要序列化,只有序列化程序?)

现在,我不明白的是,为什么这些天我们主要处理 JSON 和 XML 时,为什么还要处理二进制 java 对象序列化的概念。为什么 Jackson(和其他"new"图书馆)会选择处理这个问题?

我唯一的猜测是,这与 Jakcson 的客户端序列化的域类无关,但一些高级 JVM 用法可以在 JVM 或类似的东西之间传输使用中的各种对象,这将需要稳定的接口(interface)对于 writeObject()readObject()。但我真的如履薄冰。

最佳答案

我联系了Tatu Saloranta ,Jackson 的维护者,在查看了为 StdSerializer 类引入此内容的提交之后,就这个问题做了详细的回答!出版his answer这里:

The truth is this came as a user request, and as I recall, there aremaybe 2 use cases where this might be useful. Second of them couldsort of qualify for "moving objects between different JVMs". I'd haveto agree that neither of these use cases seems like particularlycommon pattern, fwtw.

First one that was bit more exotic (and may or may not be relevant atthis point) was for Android use case for its thaw/unthaw (or whateverit is called when app goes into background mode, back, wrt XmlMapper(XML-backed ObjectMapper) -- if so, performance was apparently muchbetter if using JDK serialization, compared to re-creating mapperinstance. I don't know if that is still the case or not.

Second, more widely applicable use case would be for platforms likeSpark (or Hadoop, perhaps Flink/Apache Beam), in which a coordinatornode may want to configure mapper in some way, and then send it toworker nodes initialized to particular settings (including alsoregistered modules, which is how serializers/deserializers would berelevant). If so, performance isn't the key but ability to have exactconfiguration to use.

But trying to make/keep (de)serializers JDK serializable is a drag andit is something I am happy I can drop from Jackson 3.x (whenever thatgets released :) ). ObjectMappers are still JDK serializable, butwithout requiring many/most of internal components to be. This is doneby changing the way mappers are constructed and I probably shouldn'tgo too deep into details here. :)

关于java - 为什么 Jackson 实现 Serializable?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66582202/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com