替换序列化数据中的类名 [英] Replacing class name in serialized data

查看:93
本文介绍了替换序列化数据中的类名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在序列化数据流中用com.newPackage.className替换字符串com.oldpackage.className。这个序列化数据从数据库读取并在替换字符串后更新。

I want to replace the String "com.oldpackage.className" with "com.newPackage.className" in a stream of serialized data. This serialized data is read from the DB and updated after replacing the string.

我在做同样的事情时遇到了一些问题。如果您已经猜到了,这是重构练习的一部分。是否有任何库可以帮助我操作序列化数据?如果您还可以对任何预防措施或警告发表评论,那将会有很大帮助。

I am facing some problems while doing the same. If you already guessed it, this is part of a refactoring exercise. Are there any libraries that would help me in manipulating the serialized data? If you can also please comment on any precautions or caveats, it would be of great help.

非常感谢,
克里斯。
PS:旧类和新类都没有声明serialversion ID作为其字段的一部分。

Thanks a lot, Chris. P.S: Both the old class and the new class do not declare a serialversion ID as part of its fields.

推荐答案

我不知道如何做你正在尝试但我可以建议你合法的解决方案。在类路径中实现包含旧包和新包的转换器,并执行以下操作:从DB读取数据,使用旧包对其进行反序列化,在Java中将旧实例转换为new,然后再将新数据存储在DB中。

I do not know how to do what your are trying but I can suggest you "legal" solution. Implement convertor that has both old and new packages in classpath and does the following: reads data from DB, de-serializes it using the old package, converts old instances to new in java and then stores new data in DB again.

此解决方案需要执行一些脏工作,但这是安全的。此外,结果可能在将来重复使用。但我祝你好运,找到只是替换序列化数据中的类名的解决方案。

This solution requires performing some dirty job but it is safe. Moreover the results may be reused in future. But I wish you good luck to find solution that just replaces class name in serialized data.

这篇关于替换序列化数据中的类名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆