How to let JAXB render boolean as 0 and 1, not true and false

前端 未结 5 1614
死守一世寂寞
死守一世寂寞 2020-12-08 19:53

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\"?

5条回答
  •  眼角桃花
    2020-12-08 20:21

    JAXB provides a flexible way to customize your bindings. You simply have to write an XML file that will indicate how you want to bind your XML and Java types. In your case, you could use a declaration, in which you can specify a parseMethod and a printMethod. These methods could be as simple as

    public boolean myParseBool(String s)
    {
        return s.equals("1");
    }
    
    public String myPrintBool(boolean b)
    {
        return b ? "1" : "0";
    }
    

    There might exist easier ways, maybe using DatatypeConverter, but I'm not enough aware of this subject to help you more !

提交回复
热议问题