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

  1. If the field is genuinely multi-state, read it as an integer (getIntValue) and interpret the states yourself; do not force it through getBoolean.
  2. If you just need truthiness, apply your own rule before/instead: value != 0, or map only your true-states explicitly.
  3. Fix the producer to send only 0/1 numbers or real JSON booleans for flag fields.
  4. 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

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


AI-assisted analysis of Tencent/APIJSON@5284052872 (2026-08-14). Data as JSON: /api/errors/4915cc30ff67954b. Report an issue: GitHub.