index.php route common home,Is there away to configure XSream to not include values when they are eq...

问题

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:

I want it to be:

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值