Tencent/APIJSON · error · IllegalArgumentException
Cannot convert value of type " + value.getClass().getName()
Error message
Cannot convert value of type " + value.getClass().getName() + " to boolean
What it means
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.
Source
Thrown at APIJSONORM/src/main/java/apijson/JSON.java:623
}
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;
}
if (value instanceof Boolean) {
return (Boolean) value;
}View on GitHub (pinned to 5284052872)
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.
Example fix
// before
boolean admin = JSON.getBooleanValue(user, "admin"); // value is JSONObject
// after
Object v = user.get("admin");
boolean admin = v instanceof Boolean ? (Boolean) v : Boolean.parseBoolean(String.valueOf(((Map<?,?>) v).get("enabled"))); Defensive patterns
Strategy: type-guard
Validate before calling
Object v = map == null ? null : map.get(key);
boolean convertible = v == null || v instanceof Boolean
|| (v instanceof String && Arrays.asList("true","false").contains(((String) v).toLowerCase()))
|| (v instanceof Number && (((Number) v).intValue() == 0 || ((Number) v).intValue() == 1)); Type guard
public static boolean isBooleanConvertible(Object v) {
return v instanceof Boolean
|| (v instanceof String && Arrays.asList("true","false").contains(((String) v).trim().toLowerCase()))
|| (v instanceof Number && Math.abs(((Number) v).intValue()) <= 1 && ((Number) v).doubleValue() == Math.rint(((Number) v).doubleValue()));
} Try / catch
try { boolean b = JSON.getBooleanValue(map, key); } catch (IllegalArgumentException e) { /* log offending class from e.getMessage(), apply explicit default */ } Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Value for key '" + key + "' is not a Map: " + value.getClass
- Value for key '" + key + "' is not a List: " + value.getClas
- Cannot convert String value '" + value + "' to int: " + e.ge
- Cannot convert value of type " + value.getClass().getName()
- Cannot convert String value '" + value + "' to long: " + e.g
AI-assisted analysis of Tencent/APIJSON@5284052872 (2026-08-14).
Data as JSON: /api/errors/97f313ca946ee503.
Report an issue: GitHub.