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

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

问题描述

我目前正在开发一个需要持久保存任何类型对象的项目(我们没有任何控制权的实现),因此可以在之后恢复这些对象。



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



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

我们已尝试使用XMLEncoder类(将对象转换为XML),但我们发现缺少功能(例如,不支持Enums)。



最后,我们还尝试了JAXB,但这会强制我们的用户注释他们的类。



有什么好的选择吗?

解决方案

你最容易做的事情仍然是使用序列化,IMO,但更多地考虑序列化形式的课程(你真的应该去做)。例如:


  1. 明确定义SerialUID。

  2. 在适当的位置定义您自己的序列化表格。

序列化表格是API类的一部分,应仔细考虑其设计。



我不会详细介绍很多细节,因为我所说的几乎所有内容都来自Effective Java。我会转而推荐你,特别是有关序列化的章节。它会警告您遇到的所有问题,并为问题提供正确的解决方案:



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






话虽如此,如果您仍在考虑采用非序列化方法,这里有几个:



XML编组



许多人指出是一个选项,但我认为你仍会遇到向后兼容性相同的问题。但是,使用XML编组时,您可能会立即捕获这些内容,因为某些框架可能会在初始化期间为您进行一些检查。



转换为/来自YAML



这是我一直在想的一个想法,但我真的很喜欢YAML格式(至少作为自定义toString()格式)。但实际上,唯一不同的是你要编组YAML而不是XML。唯一的好处是YAML比XML更具人性化。同样的限制也适用。


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.

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

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, ...).

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).

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

Any good alternative?

解决方案

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. Explicitly define the SerialUID.
  2. Define your own serialized form where appropriate.

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

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 marshalling

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.

Conversion to/from YAML

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天全站免登陆