Android: what happens if I add serialVersionUID to old serializable objects?

喜夏-厌秋 提交于 2019-12-02 12:57:59

问题


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;

回答1:


  1. If you give it the same value that is shown by running the serialver tool on the class as it is now, nothing happens.
  2. 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.



来源:https://stackoverflow.com/questions/32069590/android-what-happens-if-i-add-serialversionuid-to-old-serializable-objects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!