Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

boolean

jaxb

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

like image 914
user20298 Avatar asked Dec 05 '08 12:12

user20298


1 Answers

The adapter class:

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;     } } 

Usage:

@XmlElement( name = "enabled" ) @XmlJavaTypeAdapter( BooleanAdapter.class ) public Boolean getEnabled() {     return enabled; } 
like image 90
mtpettyp Avatar answered Sep 28 '22 04:09

mtpettyp