XStream 或简单

发布于 2024-08-07 11:29:54 字数 439 浏览 2 评论 0原文

我需要决定使用哪一个。我的情况很简单。我需要将简单的 POJO/Bean 转换为 XML,然后再转换回来。没什么特别的。

我正在寻找的一件事是它也应该包括父属性。最好的是它可以在超级类型上工作,超级类型可以只是一个标记接口。

如果有人可以比较这两者的优缺点,以及哪一个缺少什么。我知道 XStream 也支持 JSON,这是一个优点。但是,如果我们把 JSON 放在一边,Simple 乍一看看起来更简单。 Simple 在开发和社区方面的未来是什么?我相信 XStream 非常流行,甚至“XStream”这个词也引起了很多关注。

谢谢。

I need to decide on which one to use. My case is pretty simple. I need to convert a simple POJO/Bean to XML, and then back. Nothing special.

One thing I am looking for is it should include the parent properties as well. Best would be if it can work on super type, which can be just a marker interface.

If anyone can compare these two with cons and pros, and which thing is missing in which one. I know that XStream supports JSON too, thats a plus. But Simple looked simpler in a glance, if we set JSON aside. Whats the future of Simple in terms of development and community? XStream is quite popular I believe, even the word, "XStream", hit many threads on SO.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

萌梦深 2024-08-14 11:29:55

Simple 和 Jaxb 的一个“简单”(双关语)缺点是它们需要先对对象进行注释,然后才能将其序列化为 XML。当您快速想要使用未注释的对象序列化其他人的代码时会发生什么?如果有一天您能看到这种情况发生,XStream 会更合适。 (有时它实际上只是归结为这样的简单要求来驱动您的决策)。

One "simple" (pun intended) disadvantage of Simple and Jaxb is that they require annotating your objects before they can be serialized to XML. What happens the day you quickly want to serialize someone else's code with objects that are not annotated? If you can see that happening one day, XStream is a better fit. (Sometimes it really just boils down to simple requirements like this to drive your decisions).

◇流星雨 2024-08-14 11:29:55

在阅读 stackoverflow 时快速浏览了 simple;作为对 Paul Marshalls 有用帖子的修正,我想我应该提到 Simple 似乎确实支持通过注释进行版本控制 -

http://simple.sourceforge.net/download/stream/doc/tutorial/tutorial.php#version

Was taking a quick look at simple while reading stackoverflow; as an amendment to Paul Marshalls helpful post, I thought i'd mention that Simple does seem to support versioning through annotations-

http://simple.sourceforge.net/download/stream/doc/tutorial/tutorial.php#version

牵你的手,一向走下去 2024-08-14 11:29:55

Simple 比 XStream 慢得多(将对象序列化为 xml)

http:// pronicles.blogspot.com/2011/03/xstream-vs-simple.html

Simple is much slower then XStream(in serialization objects to xml)

http://pronicles.blogspot.com/2011/03/xstream-vs-simple.html

弃爱 2024-08-14 11:29:54

仅通过阅读文档(我面临着与您相同的问题,但还没有尝试过任何一种方法;对此持保留态度):

XSTREAM

  1. 非常非常容易在 Google 上搜索。有关它的示例、论坛帖子和博客文章很容易找到。
  2. 开箱即用。 (当然,可能需要更多调整,但它会立即给您一些东西。)
  3. 将变量转换为属性需要创建一个单独的转换器类,并将其注册到 XStream。 (对于简单的值来说并不难,但是需要一点额外的工作。)
  4. 根本不处理版本控制,除非您添加 XMT(另一个库);如果您的类生成的 XML 发生更改,它根本不会反序列化。 (添加 XMT 后,您可以随心所欲地更改类,并且只要您创建越来越多的增量版本控制函数,就可以让 XStream 很好地处理它。)
  5. 所有调整都需要您编写代码,要么实现您自己的(反序列化函数,或调用 XStream 函数来更改所使用的(反)序列化技术。
  6. 简单的语法注意事项:您需要将反序列化器的输出转换为您的类。

简单

  1. 主页是唯一可靠的信息来源;它列出了大约六篇外部文章,并且有一个邮件列表,但您无法在互联网上找到它。
  2. 需要在代码运行之前对其进行注释。
  3. 使用属性而不是每个属性的 XML 节点可以轻松创建更紧凑的 XML 文件。
  4. 每当类正确但版本不同时,通过不严格的解析来处理版本控制。 (即,如果自上一版本以来添加了两个字段并删除了一个字段,它将忽略删除的字段并且不会抛出异常,但不会设置添加的字段。)与 XStream 一样,它似乎没有一种将数据从一个版本迁移到下一个版本的方法,但与 XStream 不同的是,没有外部库可以介入并处理它。据推测,处理这个问题的方法是使用一些外部函数(也许是类中的“版本”变量?),所以你这样做

    Stuff myRestoredStuff = serializer.read(Stuff.class, 文件);
    myRestoredStuff.sanityCheck();

  5. 常用的(反)序列化调整是通过添加/编辑注释来进行的,但如果您需要做一些模糊的事情,则支持编写自己的(反)序列化函数来覆盖标准方法。

  6. 简单的语法注意事项:您需要将恢复的对象的类传递到反序列化器中(但不需要转换结果)。

Just from reading the documentation (I'm facing down the same problem you are, but haven't tried either way yet; take this with a grain of salt):

XSTREAM

  1. Very, very easy to Google. Examples, forum posts, and blog posts about it are trivial to find.
  2. Works out of the box. (May need more tweaking, of course, but it'll give you something immediately.)
  3. Converting a variable to an attribute requires creating a separate converter class, and registering that with XStream. (It's not hard for simple values, but it is a little extra work.)
  4. Doesn't handle versioning at all, unless you add in XMT (another library); if the XML generated by your class changes, it won't deserialize at all. (Once you add XMT, you can alter your classes however you like, and have XStream handle it fine, as long as you create an increasing line of incremental versioning functions.)
  5. All adjustments require you to write code, either to implement your own (de)serialization functions, or calling XStream functions to alter the (de)serialization techniques used.
  6. Trivial syntax note: you need to cast the output of the deserializer to your class.

SIMPLE

  1. Home page is the only reliable source of information; it lists about a half-dozen external articles, and there's a mailing list, but you can't find it out in the wild Internet.
  2. Requires annotating your code before it works.
  3. It's easy to make a more compact XML file using attributes instead of XML nodes for every property.
  4. Handles versioning by being non-strict in parsing whenever the class is right, but the version is different. (i.e., if you added two fields and removed one since the last version, it'll ignore the removed field and not throw an exception, but won't set the added fields.) Like XStream, it doesn't seem to have a way to migrate data from one version to the next, but unlike XStream, there's no external library to step in and handle it. Presumably, the way to handle this is with some external function (and maybe a "version" variable in your class?), so you do

    Stuff myRestoredStuff = serializer.read(Stuff.class, file);
    myRestoredStuff.sanityCheck();

  5. Commonly-used (de)serializing adjustments are made by adding/editing annotations, but there's support for writing your own (de)serialization functions to override the standard methods if you need to do something woolly.

  6. Trivial syntax note: you need to pass the restored object's class into the deserializer (but you don't need to cast the result).
剩一世无双 2024-08-14 11:29:54

为什么不使用 JAXB 来代替呢?

  • 100% 模式覆盖
  • 庞大的用户群
  • 多种实现(以防万一您在其中遇到错误)
  • 包含在 Java SE 6 中,与 JDK 1.5 兼容
  • JAX-WS(Web 服务)的绑定层
  • JAX-RS(其余)的绑定层
  • 兼容JSON(与 Jettison 等库一起使用时)

有用的资源:

Why not use JAXB instead?

  • 100% schema coverage
  • Huge user base
  • Multiple implementations (in case you hit a bug in one)
  • Included in Java SE 6, compatible with JDK 1.5
  • Binding layer for JAX-WS (Web Services)
  • Binding layer for JAX-RS (Rest)
  • Compatible with JSON (when used with libraries such as Jettison)

Useful resources:

痴情 2024-08-14 11:29:54

我建议您看一下 Simple

I'd recommend that you take a look at Simple

谁的年少不轻狂 2024-08-14 11:29:54

我还建议Simple,看看Simple。 net/download/stream/doc/tutorial/tutorial.php" rel="nofollow noreferrer">教程,在那里自己决定。邮件列表反应非常灵敏,您将始终得到任何疑问的及时答复。

I would also suggest Simple, take a look at the tutorial, there and decide for yourself. The mailing list is very responsive and you will always get a prompt answer to any queries.

梦纸 2024-08-14 11:29:54

到目前为止我还没有使用过 Simple 框架。

根据我使用 Xstream 的经验。它在 XML 上运行良好。但是,对于 JSON,当我尝试序列化包含哈希表列表的 bean 时,结果并不像预期的那么精确。

So far I have never use Simple framework yet.

Based on my experience with Xstream. It worked well on XML. However, for JSON, the result is not as precise as expected when I attempt to serialize a bean that contain a List of Hashtable.

氛圍 2024-08-14 11:29:54

我想我在这里分享这个。
让 XStream 忽略缺失的字段(当您删除属性时):

 XStream xstream = new XStream() {
  @Override
  protected MapperWrapper wrapMapper(MapperWrapper next) {
    return new MapperWrapper(next) {
      @Override
      public boolean shouldSerializeMember(Class definedIn,
              String fieldName) {
        if (definedIn == Object.class) {
          return false;
        }
        return super.shouldSerializeMember(definedIn, fieldName);
      }
    };
  }
};   

这也可以扩展以处理版本和属性重命名。

感谢 Peter Voss:https://pvoss.wordpress.com/2009/01/08/ xstream

Thought I share this here.
To get XStream to ignore missing fields (when you have removed a property):

 XStream xstream = new XStream() {
  @Override
  protected MapperWrapper wrapMapper(MapperWrapper next) {
    return new MapperWrapper(next) {
      @Override
      public boolean shouldSerializeMember(Class definedIn,
              String fieldName) {
        if (definedIn == Object.class) {
          return false;
        }
        return super.shouldSerializeMember(definedIn, fieldName);
      }
    };
  }
};   

This can also be extended to handle versions and property renames.

Credit to Peter Voss: https://pvoss.wordpress.com/2009/01/08/xstream

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文