Gets a boolean from an object for the given key. If the key is not present, this returns null. If the value is not a Boolean, throws an exception.
(Map<String, ?> obj, String key)
| 250 | * If the value is not a Boolean, throws an exception. |
| 251 | */ |
| 252 | @Nullable |
| 253 | public static Boolean getBoolean(Map<String, ?> obj, String key) { |
| 254 | assert key != null; |
| 255 | if (!obj.containsKey(key)) { |
| 256 | return null; |
| 257 | } |
| 258 | Object value = obj.get(key); |
| 259 | if (!(value instanceof Boolean)) { |
| 260 | throw new ClassCastException( |
| 261 | String.format("value '%s' for key '%s' in '%s' is not Boolean", value, key, obj)); |
| 262 | } |
| 263 | return (Boolean) value; |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * Casts a list of unchecked JSON values to a list of checked objects in Java type. |