哪个是 Java 序列化的最佳替代方案? [英] Which is the best alternative for Java Serialization?

查看:26
本文介绍了哪个是 Java 序列化的最佳替代方案?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个项目,该项目需要保留任何类型的对象(我们无法控制其实现),以便之后可以恢复这些对象.

I'm currently working on a project which needs to persist any kind of object (of which implementation we don't have any control) so these objects could be recovered afterwards.

我们无法实现 ORM,因为我们无法在开发时限制我们库的用户.

We can't implement an ORM because we can't restrict the users of our library at development time.

我们的第一个选择是使用 Java 默认序列化对其进行序列化,但是当用户开始传递同一对象的不同版本(属性更改类型、名称等)时,我们在恢复对象时遇到了很多麻烦.

Our first alternative was to serialize it with the Java default serialization but we had a lot of trouble recovering the objects when the users started to pass different versions of the same object (attributes changed types, names, ...).

我们尝试过使用 XMLEncoder 类(将对象转换为 XML),但我们发现它缺乏功能(例如不支持枚举).

We have tried with the XMLEncoder class (transforms an object into a XML), but we have found that there is a lack of functionality (doesn't support Enums for example).

最后,我们也尝试了 JAXB,但这迫使我们的用户注释他们的类.

Finally, we also tried JAXB but this impose our users to annotate their classes.

有什么好的选择吗?

推荐答案

对你来说,最简单的事情仍然是使用序列化,IMO,但更多地考虑类的序列化形式(你真的应该这样做)反正).例如:

The easiest thing for you to do is still to use serialization, IMO, but put more thought into the serialized form of the classes (which you really ought to do anyway). For instance:

  1. 明确定义 SerialUID.
  2. 在适当的地方定义您自己的序列化表单.

序列化形式是类 API 的一部分,在设计时应仔细考虑.

The serialized form is part of the class' API and careful thought should be put into its design.

我不会详细介绍,因为我所说的几乎所有内容都来自 Effective Java.相反,我会向您推荐它,特别是关于序列化的章节.它会警告您遇到的所有问题,并为问题提供适当的解决方案:

I won't go into a lot of details, since pretty much everything I have said comes from Effective Java. I'll instead, refer you to it, specifically the chapters about Serialization. It warns you about all the problems you're running into, and provides proper solutions to the problem:

http://www.amazon.com/Effective-Java-2nd-Joshua-Bloch/dp/0321356683

话虽如此,如果您仍在考虑非序列化方法,这里有一些:

With that said, if you're still considering a non-serialization approach, here are a couple:

XML 编组

正如许多人指出的那样,这是一种选择,但我认为您仍然会遇到与向后兼容性相同的问题.但是,通过 XML 编组,您有望立即捕获这些信息,因为某些框架可能会在初始化期间为您做一些检查.

As many has pointed out is an option, but I think you'll still run into the same problems with backward compatibility. However, with XML marshalling, you'll hopefully catch these right away, since some frameworks may do some checks for you during initialization.

与 YAML 之间的转换

这是我一直在玩弄的一个想法,但我真的很喜欢 YAML 格式(至少作为自定义 toString() 格式).但实际上,对您来说唯一的区别是您将编组到 YAML 而不是 XML.唯一的好处是 YAML 比 XML 更具人类可读性.同样的限制也适用.

This is an idea I have been toying with, but I really liked the YAML format (at least as a custom toString() format). But really, the only difference for you is that you'd be marshalling to YAML instead of XML. The only benefit is that that YAML is slightly more human readable than XML. The same restrictions apply.

这篇关于哪个是 Java 序列化的最佳替代方案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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