类的序列化、演化
我需要序列化一个对象并将其存储在磁盘上。我使用了内置的Java,只要类不发生太大变化,它就可以正常工作。如果我开始在课堂上乱搞,它就会停止工作。
这里有哪些可用选项?基本上,如果我们有更新,我不想破坏所有用户的数据文件。
到目前为止,我已经尝试过序列化为 XML(有同样的问题)。还尝试“手卷”配置/数据文件。基本上将所有内容吐出到 XML 中,加载它,然后根据配置文件创建一个新对象。这似乎运作良好,但需要永远将所有内容转换为此,因为这是大量的手动工作。
还有其他选择吗?
I need to serial an object and store it on disk. I used Java's built in and that works fine, as long as the class doesn't change to much. If I start mucking around with the class it can stop working.
What options are available here? Basically if we have an update I don't want to break all the user's data files.
So far I've tried serialization to XML (has same problems). Also tried to 'hand roll' a config/data file. Basically spit everything out into XML, load it and then create a NEW object based off the config file. This seems to work well, but would take forever to convert everything over to this since it's a lot of manual work.
Any other options?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
和 还有更多内容在此处进行比较
And many more with comparisons here
首先,您需要彻底阅读对象序列化的 版本控制部分规范。
First you need to read thoroughly the Versioning section of the Object Serialization Specification.
我非常相信(经过惨痛的教训)我的序列化对象需要持久代理。 《Effective Java》有一整章讨论实现它们的方法。
我这样做的方法是使用一个内部类,该内部类在其构造函数中接收我的对象并相应地设置其内部字段。随着真实对象的发展,您可以向代理添加新字段,或者创建一个全新的代理(保留旧代理以向后兼容)。
有关详细信息,请参阅本文。
I am a big believer (having learned the hard way) of having persistent proxies for my serialized objects. Effective Java has a whole chapter that talks about ways of implementing them.
The way I do it is to use an inner class that takes my object in it's constructor and sets it's internal fields accordingly. As your real object evolves, you either add new fields to the proxy, or create a completely new proxy (leaving the old one in place for backwards compatibility).
See this article for more info.