Tencent/APIJSON · error · IllegalArgumentException

Cannot convert String value '" + value + "' to boolean

Error message

Cannot convert String value '" + value + "' to boolean

What it means

Thrown by apijson.JSON.getBoolean(Map, String) (APIJSONORM/src/main/java/apijson/JSON.java:612) when the value is a String whose lowercased form is neither "true" nor "false". The check is exact-match after toLowerCase() — no trimming is done, so padded strings like " true" fail, and no synonyms (yes/no, on/off, 1/0 as text) are accepted.

Source

Thrown at APIJSONORM/src/main/java/apijson/JSON.java:612

	 * @return The boolean value
	 * @throws IllegalArgumentException If value cannot be converted to boolean
	 */
	public static Boolean getBoolean(Map<String, Object> map, String key) throws IllegalArgumentException {
		Object value = map == null || key == null ? null : map.get(key);
		if (value == null) {
			return null;
		}

		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

View on GitHub (pinned to 5284052872)

Solutions

  1. Trim and normalize the string before the call: value.trim().toLowerCase(), and map synonyms (yes/on/1 → true, no/off/0 → false) yourself.
  2. Fix the producer to send real JSON booleans (true/false literals) so the Boolean branch handles it.
  3. For 0/1 sent as numbers, note the Number branch already accepts them — just stop stringifying.
  4. Treat unrecognized/blank text as an absent flag (getBoolean returns null for null) instead of letting it throw.

Example fix

// before
Boolean enabled = JSON.getBoolean(settings, "enabled"); // throws on "on"

// after
Object raw = settings.get("enabled");
Boolean enabled;
if (raw instanceof Boolean) { enabled = (Boolean) raw; }
else if (raw instanceof String) {
    String s = ((String) raw).trim().toLowerCase();
    enabled = s.isEmpty() ? null : ("true|yes|on|1".contains(s) ? Boolean.TRUE : Boolean.FALSE);
} else { enabled = null; }
Defensive patterns

Strategy: validation

Validate before calling

Object v = settings.get("enabled");
if (v instanceof String) {
    String s = ((String) v).trim().toLowerCase();
    if (!s.equals("true") && !s.equals("false")) {
        throw new IllegalArgumentException("'enabled' must be true/false, got: '" + v + "'");
    }
}

Type guard

static boolean isBooleanLike(Object v) {
    if (v instanceof Boolean || v == null) return true;
    if (v instanceof String) { String s = ((String) v).trim().toLowerCase(); return s.equals("true") || s.equals("false"); }
    if (v instanceof Number) { int i = ((Number) v).intValue(); return i == 0 || i == 1; }
    return false;
}

Try / catch

try {
    Boolean enabled = JSON.getBoolean(settings, "enabled");
} catch (IllegalArgumentException e) {
    log.warn("Bad boolean at 'enabled': {}", e.getMessage());
    // normalize yes/no/on/off yourself, or reject with 400
}

Prevention

When it happens

Trigger: Value is " true" or "true\n" (whitespace), "TRUE " (trailing space), "yes", "1", "Y", "on", "enabled", or an empty string; frontend sending tri-state text like "maybe"; enum-ish flags sent as words.

Common situations: HTML form checkboxes posting "on"; configs using Y/N or yes/no; values read from properties/CSV with trailing whitespace or BOM; optional flags sent as "" instead of omitted; localizing flag words.

Related errors


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