XStream or Simple

前端 未结 9 1270
栀梦
栀梦 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: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

提交回复
热议问题