序列化布尔值,QUOT&1 QUOT;和" 0 QUOT;代替"真"和"假" [英] Serialize Boolean to "1" and "0" instead of "true" and "false"

查看:164
本文介绍了序列化布尔值,QUOT&1 QUOT;和" 0 QUOT;代替"真"和"假"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找不到在布尔类的任何方法序列化布尔1和0,而不是真与假。

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.

推荐答案

如果的你在谈论生产字符串从给定布尔,则没有,有产生0或1没有内置的方法,但是你可以很容易地把它写:

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

根据您的使用情况下,可能更适合让它抛出一个 NullPointerException异常如果输入为空。如果这是你的话,那么你可以在方法降低到第二单独回报行。

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.

这篇关于序列化布尔值,QUOT&1 QUOT;和" 0 QUOT;代替"真"和"假"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆