{"record":{"id":"97f313ca946ee503","repo":"Tencent/APIJSON","slug":"cannot-convert-value-of-type-value-getclass-97f313","errorCode":null,"errorMessage":"Cannot convert value of type \" + value.getClass().getName() + \" to boolean","messagePattern":"Cannot convert value of type \" \\+ value\\.getClass\\(\\)\\.getName\\(\\) \\+ \" to boolean","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"APIJSONORM/src/main/java/apijson/JSON.java","lineNumber":623,"sourceCode":"\t\t}\n\n\t\tif (value instanceof String) {\n\t\t\tString str = ((String) value).toLowerCase();\n\t\t\tif (str.equals(\"true\") || str.equals(\"false\")) {\n\t\t\t\treturn Boolean.parseBoolean(str);\n\t\t\t}\n\t\t\tthrow new IllegalArgumentException(\"Cannot convert String value '\" + value + \"' to boolean\");\n\t\t}\n\n\t\tif (value instanceof Number) {\n\t\t\tint intValue = ((Number) value).intValue();\n\t\t\tif (intValue == 0 || intValue == 1) {\n\t\t\t\treturn intValue != 0;\n\t\t\t}\n\t\t\tthrow new IllegalArgumentException(\"Cannot convert Number value '\" + value + \"' to boolean. Only 0 and 1 are supported.\");\n\t\t}\n\n\t\tthrow new IllegalArgumentException(\"Cannot convert value of type \" + value.getClass().getName() + \" to boolean\");\n\t}\n\n\t/**\n\t * Get a boolean value from a Map\n\t * @param map Source map\n\t * @param key The key\n\t * @return The boolean value\n\t * @throws IllegalArgumentException If value cannot be converted to boolean\n\t */\n\tpublic static boolean getBooleanValue(Map<String, Object> map, String key) throws IllegalArgumentException {\n\t\tObject value = map == null || key == null ? null : map.get(key);\n\t\tif (value == null) {\n\t\t\treturn false;\n\t\t}\n\n\t\tif (value instanceof Boolean) {\n\t\t\treturn (Boolean) value;\n\t\t}","sourceCodeStart":605,"sourceCodeEnd":641,"githubUrl":"https://github.com/Tencent/APIJSON/blob/5284052872898eddc449a58f629e5c8d588b8e22/APIJSONORM/src/main/java/apijson/JSON.java#L605-L641","documentation":"Thrown by the boolean-conversion helper in apijson.JSON when the map value stored under the key is neither Boolean, nor the strings \"true\"/\"false\", nor the numbers 0/1. The library refuses to guess a boolean meaning for any other JVM type (e.g. Date, array, JSONObject) and fails fast with IllegalArgumentException. The class name in the message tells you exactly which type was found.","triggerScenarios":"Calling JSON.getBooleanValue(map, key) (or the internal toBoolean conversion) where map.get(key) returns a non-Boolean/String/Number object, e.g. a nested JSONObject, a java.util.Date, a List, or a custom POJO.","commonSituations":"A JSON field that used to hold true/false or 0/1 was changed to hold an object or array; deserializing into Map<String,Object> produced JSONObject/JSONArray instead of primitives; passing a POJO where a raw value was expected.","solutions":["Inspect the reported class name in the message and fix the producer so the field is a real boolean (or 0/1).","If the value is a nested JSON object, extract the inner boolean field explicitly before calling getBooleanValue.","Pre-normalize the value yourself: convert known types (e.g. \"Y\"/\"N\", enums) to Boolean before passing the map to APIJSON.","As a last resort wrap the call in try/catch and apply your own default, but never silence it blindly."],"exampleFix":"// before\nboolean admin = JSON.getBooleanValue(user, \"admin\"); // value is JSONObject\n\n// after\nObject v = user.get(\"admin\");\nboolean admin = v instanceof Boolean ? (Boolean) v : Boolean.parseBoolean(String.valueOf(((Map<?,?>) v).get(\"enabled\")));","handlingStrategy":"type-guard","validationCode":"Object v = map == null ? null : map.get(key);\nboolean convertible = v == null || v instanceof Boolean\n    || (v instanceof String && Arrays.asList(\"true\",\"false\").contains(((String) v).toLowerCase()))\n    || (v instanceof Number && (((Number) v).intValue() == 0 || ((Number) v).intValue() == 1));","typeGuard":"public static boolean isBooleanConvertible(Object v) {\n    return v instanceof Boolean\n        || (v instanceof String && Arrays.asList(\"true\",\"false\").contains(((String) v).trim().toLowerCase()))\n        || (v instanceof Number && Math.abs(((Number) v).intValue()) <= 1 && ((Number) v).doubleValue() == Math.rint(((Number) v).doubleValue()));\n}","tryCatchPattern":"try { boolean b = JSON.getBooleanValue(map, key); } catch (IllegalArgumentException e) { /* log offending class from e.getMessage(), apply explicit default */ }","preventionTips":["Keep boolean fields as real JSON booleans in API contracts.","Validate payloads against a schema (required type boolean) before passing maps to APIJSON.","Never store objects/arrays under a field consumed as a flag."],"tags":["type-conversion","boolean","apijson","validation"],"backgroundTag":null,"analyzedSha":"5284052872898eddc449a58f629e5c8d588b8e22","analyzedAt":"2026-08-14T15:15:29.577Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}