XStream or Simple

2019-03-16 02:57发布

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.

9条回答
smile是对你的礼貌
2楼-- · 2019-03-16 03:46

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:

查看更多
地球回转人心会变
3楼-- · 2019-03-16 03:47

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.

查看更多
老娘就宠你
4楼-- · 2019-03-16 03:47

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

查看更多
登录 后发表回答