Is there away to configure XSream to not include values when they are equal to their default values?

情到浓时终转凉″ 提交于 2019-12-11 23:18:33

问题


I am using XStream with Annotations to go between my Java objects and XML. I am essentially trying to make the XML as minimal as possible, and one the items I want to reduce is the inclusion of boolean values when not necessary. When a boolean value is false I do not want to include it in my xml, since it's default value is also false.

Is there away to configure XSream to not include values when they are equal to their default values?

Given:

public class Test {

    @XStreamAlias("name")
    @XStreamAsAttribute
    private String name;

    @XStreamAlias("good")
    @XStreamAsAttribute
    private boolean good;

    public Test(String name, boolean good){
        this.name = name;
        this.good = good;
    }

    public static void main(String[] args) {
        XStream stream = new XStream();
        stream.processAnnotations(new Class[] {Test.class});

        Test test1 = new Test("Test", true);
        System.out.println(stream.toXML(test1));
        Test test2 = new Test("Test", false);   
        System.out.println(stream.toXML(test2));    
    }
}

This prints:

<Test name="Test" good="true"/>
<Test name="Test" good="false"/>

I want it to be:

<Test name="Test" good="true"/>
<Test name="Test"/>

Edit:
I noticed BooleanConverter has a method called shouldConvert so I tried overriding that by extending the class. Which did not work, it is never called. The method:

@Override
public boolean shouldConvert(Class type, Object value) {
  System.out.println("Class: "+type+" Value: "+value);
  return (Boolean)value;
}

My variable annotation to set the converter on good

@XStreamConverter(MyBooleanConverter.class)
@XStreamAlias("good")
@XStreamAsAttribute
private boolean good;

回答1:


I was facing the same issue lately and found that the easiest way to avoid XStream instantiating default values for booleans is to make your booleans java.lang.Booleans rather than primitive booleans.

So simply defining your attribute like this should do the trick :

@XStreamAlias("good")
@XStreamAsAttribute
private Boolean good;



回答2:


You have to write your own BooleanConverter overriding the toString(Objec obj) method.

public class MyBooleanConverter extends BooleanConverter{

    @Override
    public String toString(Object obj) {
        if(((Boolean)obj).equals(false)){
            return null;
        }
        return super.toString(obj);
    }   

}

Then set the Annotation to use the converter on the variable that you want:

@XStreamConverter(MyBooleanConverter.class)
@XStreamAlias("good")
@XStreamAsAttribute
private boolean good;

That works but requires you to set add the annotation in XStreamConverter every single time. I would prefer to not have to do that, since I want my converter to be used much more frequently than the default. You can use registerConverter to accomplish that. Here is the complete example:

public class Test {

    @XStreamAlias("name")
    @XStreamAsAttribute
    private String name;

    @XStreamAlias("good")
    @XStreamAsAttribute
    private boolean good;

    public Test(String name, boolean good){
        this.name = name;
        this.good = good;
    }

    public static void main(String[] args) {
        XStream stream = new XStream();
        stream.processAnnotations(new Class[] {Test.class});
        stream.registerConverter(new MyBooleanConverter());

        Test test1 = new Test("Test", true);
        System.out.println(stream.toXML(test1));
        Test test2 = new Test("Test", false);   
        System.out.println(stream.toXML(test2));    
    }
}


来源:https://stackoverflow.com/questions/13293913/is-there-away-to-configure-xsream-to-not-include-values-when-they-are-equal-to-t

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