安卓:如果我添加的serialVersionUID老序列化对象会发生什么情况?对象、发生、情况、序列化

2023-09-07 17:20:29 作者:好男儿丶

如果你把一个旧的序列化对象,从未有过的serialVersionUID明确规定,并添加的serialVersionUID到该对象会发生什么情况?在我看来,下一次该应用程序是由最终用户更新,它会尝试从光盘反序列化数据,找出了serialVersionUID的不匹配,覆盖从服务器/ DB新的数据/数据​​不管以后你再罚款。我是正确的这一假设?是否有进一步的问题,我应该警惕在做这个?

 私有类X实现Serializable {...

私有静态最后长的serialVersionUID = 0L;
 

解决方案 如果你给它显示运行的类用serialver 工具,因为它现在是,什么也没有发生相同的值。 如果你给它一个不同的值,现有的串行数据流,包括该类将失败与 InvalidClassException 进一步deserializations。   

在我看来,下一次该应用程序是由最终用户更新,它会尝试序列化从光盘数据

正确的。

  

找出了serialVersionUID的不匹配

只有当它真的不匹配。如果你按照上面的建议,它将匹配。

  

覆盖从服务器/数据库/不管

新数据数据

不正确的。见上面。

  IDEA 工具生成实体类的序列化ID serialVersionUID

和之后,你会感觉良好。

没有。

  

我是正确的这一假设?

没有。

What happens if you take an old serializable object that never had serialVersionUID explicitly specified, and add serialVersionUID to that object? It seems to me that the next time the app was updated by endusers it would try to deserialize data from disc, find out that the serialVersionUID didn't match, overwrite the data with new data from the server/db/whatever and after that you're fine. Am I correct in this assumption? Are there further issues I should be wary of in doing this?

private class X implements serializable {...

private static final long serialVersionUID = 0L;

解决方案

If you give it the same value that is shown by running the serialver tool on the class as it is now, nothing happens. If you give it a different value, further deserializations of existing serial streams including that class will fail with an InvalidClassException.

It seems to me that the next time the app was updated by endusers it would try to deserialize data from disc

Correct.

find out that the serialVersionUID didn't match

Only if it really didn't match. If you follow the advice above, it will match.

overwrite the data with new data from the server/db/whatever

Incorrect. See above.

and after that you're fine.

No.

Am I correct in this assumption?

No.