XStream or Simple

前端 未结 9 1267
栀梦
栀梦 2021-02-06 08:05

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

相关标签:
9条回答
  • 2021-02-06 08:39

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

    0 讨论(0)
  • 2021-02-06 08:43

    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

    0 讨论(0)
  • 2021-02-06 08:45

    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.

    0 讨论(0)
提交回复
热议问题