如何让 JAXB 将布尔值呈现为 0 和 1,而不是真假 [英] How to let JAXB render boolean as 0 and 1, not true and false
本文介绍了如何让 JAXB 将布尔值呈现为 0 和 1,而不是真假的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有一个简短的问题.有谁知道如何让 JAXB (marshall) 将布尔字段呈现为 1 和 0 而不是打印出true"和false"?
Got a quick question. Does anyone know how to let JAXB (marshall) render boolean fields as 1 and 0 instead of printing out "true" and "false"?
推荐答案
适配器类:
import javax.xml.bind.annotation.adapters.XmlAdapter;
public class BooleanAdapter extends XmlAdapter<Integer, Boolean>
{
@Override
public Boolean unmarshal( Integer s )
{
return s == null ? null : s == 1;
}
@Override
public Integer marshal( Boolean c )
{
return c == null ? null : c ? 1 : 0;
}
}
用法:
@XmlElement( name = "enabled" )
@XmlJavaTypeAdapter( BooleanAdapter.class )
public Boolean getEnabled()
{
return enabled;
}
这篇关于如何让 JAXB 将布尔值呈现为 0 和 1,而不是真假的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文