MCPcopy Index your code
hub / github.com/Tencent/APIJSON / getBooleanValue

Method getBooleanValue

APIJSONORM/src/main/java/apijson/JSON.java:633–660  ·  view source on GitHub ↗

Get a boolean value from a Map @param map Source map @param key The key @return The boolean value @throws IllegalArgumentException If value cannot be converted to boolean

(Map<String, Object> map, String key)

Source from the content-addressed store, hash-verified

631 * @throws IllegalArgumentException If value cannot be converted to boolean
632 */
633 public static boolean getBooleanValue(Map<String, Object> map, String key) throws IllegalArgumentException {
634 Object value = map == null || key == null ? null : map.get(key);
635 if (value == null) {
636 return false;
637 }
638
639 if (value instanceof Boolean) {
640 return (Boolean) value;
641 }
642
643 if (value instanceof String) {
644 String str = ((String) value).toLowerCase();
645 if (str.equals("true") || str.equals("false")) {
646 return Boolean.parseBoolean(str);
647 }
648 throw new IllegalArgumentException("Cannot convert String value '" + value + "' to boolean");
649 }
650
651 if (value instanceof Number) {
652 int intValue = ((Number) value).intValue();
653 if (intValue == 0 || intValue == 1) {
654 return intValue != 0;
655 }
656 throw new IllegalArgumentException("Cannot convert Number value '" + value + "' to boolean. Only 0 and 1 are supported.");
657 }
658
659 throw new IllegalArgumentException("Cannot convert value of type " + value.getClass().getName() + " to boolean");
660 }
661
662 /**
663 * Get a string value from a Map

Callers

nothing calls this directly

Calls 4

getMethod · 0.45
toLowerCaseMethod · 0.45
equalsMethod · 0.45
getNameMethod · 0.45

Tested by

no test coverage detected