Tencent/APIJSON · error · IllegalArgumentException
Cannot convert Number value '" + value + "' to boolean. Only
Error message
Cannot convert Number value '" + value + "' to boolean. Only 0 and 1 are supported.
What it means
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.
Source
Thrown at APIJSONORM/src/main/java/apijson/JSON.java:620
if (value instanceof Boolean) {
return (Boolean) value;
}
if (value instanceof String) {
String str = ((String) value).toLowerCase();
if (str.equals("true") || str.equals("false")) {
return Boolean.parseBoolean(str);
}
throw new IllegalArgumentException("Cannot convert String value '" + value + "' to boolean");
}
if (value instanceof Number) {
int intValue = ((Number) value).intValue();
if (intValue == 0 || intValue == 1) {
return intValue != 0;
}
throw new IllegalArgumentException("Cannot convert Number value '" + value + "' to boolean. Only 0 and 1 are supported.");
}
throw new IllegalArgumentException("Cannot convert value of type " + value.getClass().getName() + " to boolean");
}
/**
* 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
*/
public static boolean getBooleanValue(Map<String, Object> map, String key) throws IllegalArgumentException {
Object value = map == null || key == null ? null : map.get(key);
if (value == null) {
return false;
}
View on GitHub (pinned to 5284052872)
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().
Example fix
// before
Boolean active = JSON.getBoolean(user, "status"); // throws when status == 2
// after
Object raw = user.get("status");
Boolean active = raw instanceof Boolean ? (Boolean) raw
: (raw instanceof Number ? ((Number) raw).intValue() == 1 : null); // 2+ handled as your own state machine, not a boolean Defensive patterns
Strategy: validation
Validate before calling
Object v = user.get("status");
if (v instanceof Number) {
int i = ((Number) v).intValue();
if (i != 0 && i != 1) {
throw new IllegalArgumentException("'status' is multi-state (" + i + "); read it as int, not boolean");
}
} Type guard
static boolean isBooleanNumber(Object v) {
if (v instanceof Boolean || v == null) return true;
if (v instanceof Number) { int i = ((Number) v).intValue(); return i == 0 || i == 1; }
return false;
} Try / catch
try {
Boolean active = JSON.getBoolean(user, "status");
} catch (IllegalArgumentException e) {
log.warn("'status' not 0/1: {}", e.getMessage());
int status = JSON.getIntValue(user, "status");
// interpret multi-state status explicitly
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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".
Related errors
- Value for key '" + key + "' is not a Map: " + value.getClass
- Value for key '" + key + "' is not a List: " + value.getClas
- Cannot convert value of type " + value.getClass().getName()
- Cannot convert value of type " + value.getClass().getName()
- Cannot convert value of type " + value.getClass().getName()
AI-assisted analysis of Tencent/APIJSON@5284052872 (2026-08-14).
Data as JSON: /api/errors/4915cc30ff67954b.
Report an issue: GitHub.