{"record":{"id":"4915cc30ff67954b","repo":"Tencent/APIJSON","slug":"cannot-convert-number-value-value-to-boo","errorCode":null,"errorMessage":"Cannot convert Number value '\" + value + \"' to boolean. Only 0 and 1 are supported.","messagePattern":"Cannot convert Number value '\" \\+ value \\+ \"' to boolean\\. Only 0 and 1 are supported\\.","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"APIJSONORM/src/main/java/apijson/JSON.java","lineNumber":620,"sourceCode":"\n\t\tif (value instanceof Boolean) {\n\t\t\treturn (Boolean) value;\n\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","sourceCodeStart":602,"sourceCodeEnd":638,"githubUrl":"https://github.com/Tencent/APIJSON/blob/5284052872898eddc449a58f629e5c8d588b8e22/APIJSONORM/src/main/java/apijson/JSON.java#L602-L638","documentation":"Thrown by apijson.JSON.getBoolean(Map, String) (APIJSONORM/src/main/java/apijson/JSON.java:620) when the value is a Number whose intValue() is neither 0 nor 1. The library deliberately accepts only the two canonical flag integers; note also that non-integral values are truncated first, so 2 (or -1) throws while 0.9 silently becomes false and 1.2 becomes true.","triggerScenarios":"Value is Integer 2 (a tri-state enum), -1 (sentinel for unknown), Long 5 (bitmask flags), or Double 2.0; a status column reusing the numeric field for multiple states; bitmask-encoded flags read as a boolean.","commonSituations":"DB tinyint status columns (0=pending, 1=active, 2=disabled) read straight into a boolean getter; bitmask permission flags; enum-as-int APIs where >1 encodes extra states; sentinel values like -1 for \"no data\".","solutions":["If the field is genuinely multi-state, read it as an integer (getIntValue) and interpret the states yourself; do not force it through getBoolean.","If you just need truthiness, apply your own rule before/instead: value != 0, or map only your true-states explicitly.","Fix the producer to send only 0/1 numbers or real JSON booleans for flag fields.","Watch the truncation pitfall: decide explicitly how 0.5 or 1.2 should behave rather than relying on intValue()."],"exampleFix":"// before\nBoolean active = JSON.getBoolean(user, \"status\"); // throws when status == 2\n\n// after\nObject raw = user.get(\"status\");\nBoolean active = raw instanceof Boolean ? (Boolean) raw\n        : (raw instanceof Number ? ((Number) raw).intValue() == 1 : null); // 2+ handled as your own state machine, not a boolean","handlingStrategy":"validation","validationCode":"Object v = user.get(\"status\");\nif (v instanceof Number) {\n    int i = ((Number) v).intValue();\n    if (i != 0 && i != 1) {\n        throw new IllegalArgumentException(\"'status' is multi-state (\" + i + \"); read it as int, not boolean\");\n    }\n}","typeGuard":"static boolean isBooleanNumber(Object v) {\n    if (v instanceof Boolean || v == null) return true;\n    if (v instanceof Number) { int i = ((Number) v).intValue(); return i == 0 || i == 1; }\n    return false;\n}","tryCatchPattern":"try {\n    Boolean active = JSON.getBoolean(user, \"status\");\n} catch (IllegalArgumentException e) {\n    log.warn(\"'status' not 0/1: {}\", e.getMessage());\n    int status = JSON.getIntValue(user, \"status\");\n    // interpret multi-state status explicitly\n}","preventionTips":["Reserve 0/1 columns for real booleans; model multi-state values as ints with explicit handling.","Never feed bitmask or enum-as-int fields into getBoolean.","Beware truncation: 0.9 → false and 1.2 → true silently; only ints outside 0/1 throw."],"tags":["json","type-conversion","apijson","boolean-parsing","schema-mismatch"],"backgroundTag":null,"analyzedSha":"5284052872898eddc449a58f629e5c8d588b8e22","analyzedAt":"2026-08-14T15:15:29.577Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}