gpt4 book ai didi

java - 序列化工具如何在反序列化过程中跳过未知字段?

转载 作者:行者123 更新时间:2023-11-30 08:14:49 27 4
gpt4 key购买 nike

序列化工具(即hessian)如何反序列化具有相同serialVersionUID的不同版本的类?在大多数情况下,它可以跳过那些未知(在类加载器中找不到)字段并保持兼容。但上次,我尝试附加一个新字段 Map<String, Object> ,将一些未知物体放入 map 中,然后它抛出 ClassNotFoundException

  • 为什么不能像其他 map 一样跳过 map ?
  • 这是否是与工具的实现或序列化机制相关的问题?

最佳答案

这取决于工具本身。 serialVersionUID 旨在供 Java 的内置序列化器 (ObjectOutputStream) 使用,据我阅读 Hessian 源代码可知,Hessian 不使用该序列化器。

特别是对于 Hessian,我能找到的提到这些变化的最佳来源是 this email :

At least for Hessian, it's best to think of versioning as a set of types of changes that can be handled.

Specifically Hessian can manage the following kinds of changes: 1) if you add or drop a field, the side that doesn't understand the field will ignore it. 2) some field type changes are possible, if Hessian can convert (e.g. int to long) 3) there's some flexibility on map(bean) types, depending on how much information Hessian has (which is a reason to prefer concrete types.)

So, if the sender sends an untyped map {"field1", 10} and the target is known to be MyValue { int field1; }, then Hessian can map the fields.

But it cannot manage things like: 1) field name changes (the data will be dropped). 2) class name changes where the target is underdefined, like Object field1. If you send a MyValue2 as the new field1, when the previous version was MyValue1, Hessian can't make that automatic transition. (But as with #3 above, a "MyValue2 field1" would give Hessian enough information to translate.) 3) class splits, e.g. creating a subclass and pushing some fields into it.
4) map to list or list to map changes.

基本上,我不认为 Hessian 打算支持 map 中的未知类型。

关于java - 序列化工具如何在反序列化过程中跳过未知字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29813683/

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