Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serialize Boolean to "1" and "0" instead of "true" and "false"

I can't find any method on the Boolean class to serialize a Boolean to "1" and "0" instead of "true" and "false".

Is there any native function to do that ? If not, what is the best way (most optimized way) ?

Update: I indeed mean to produce a String out of a Boolean.

like image 463
Matthieu Napoli Avatar asked Jun 14 '11 08:06

Matthieu Napoli


1 Answers

If you're talking about producing a String from a given Boolean, then no, there is no built-in method that produces "0" or "1", but you can easily write it:

public static String toNumeralString(final Boolean input) {
  if (input == null) {
    return "null";
  } else {
    return input.booleanValue() ? "1" : "0";
  }
}

Depending on your use case, it might be more appropriate to let it throw a NullPointerException if input is null. If that's the case for you, then you can reduce the method to the second return line alone.

like image 195
Joachim Sauer Avatar answered Oct 11 '22 13:10

Joachim Sauer