XStream or Simple

こ雲淡風輕ζ 提交于 2019-12-04 09:32:40

问题


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.


回答1:


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:

  • Comparison, JAXB & XStream
  • Comparison, JAXB & Simple



回答2:


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



回答3:


I'd recommend that you take a look at Simple




回答4:


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.




回答5:


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.




回答6:


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




回答7:


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




回答8:


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

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




回答9:


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



来源:https://stackoverflow.com/questions/1558087/xstream-or-simple

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!