code_junkie

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

Nov 12th, 2011
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.61 KB | None | 0 0
  1. public boolean myParseBool(String s)
  2. {
  3. return s.equals("1");
  4. }
  5.  
  6. public String myPrintBool(boolean b)
  7. {
  8. return b ? "1" : "0";
  9. }
  10.  
  11. import javax.xml.bind.annotation.adapters.XmlAdapter;
  12.  
  13. public class BooleanAdapter extends XmlAdapter<Integer, Boolean>
  14. {
  15. @Override
  16. public Boolean unmarshal( Integer s )
  17. {
  18. return s == null ? null : s == 1;
  19. }
  20.  
  21. @Override
  22. public Integer marshal( Boolean c )
  23. {
  24. return c == null ? null : c ? 1 : 0;
  25. }
  26. }
  27.  
  28. @XmlElement( name = "enabled" )
  29. @XmlJavaTypeAdapter( BooleanAdapter.class )
  30. public Boolean getEnabled()
  31. {
  32. return enabled;
  33. }
Add Comment
Please, Sign In to add comment