Tencent/APIJSON · error · IllegalArgumentException

Cannot convert value of type " + value.getClass().getName()

Error message

Cannot convert value of type " + value.getClass().getName() + " to double

What it means

Thrown by apijson.JSON.getFloat(Map, String) (APIJSONORM/src/main/java/apijson/JSON.java:501) when the value is neither Number nor String (Map, List, Boolean, ...). The message says "to double" although the conversion target is float — a copy-paste artifact; the reported class name is accurate and diagnostic.

Source

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

	public static Float getFloat(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 Number) {
			return ((Number) value).floatValue();
		}

		if (value instanceof String) {
			try {
				return Float.parseFloat((String) value);
			} catch (NumberFormatException e) {
				throw new IllegalArgumentException("Cannot convert String value '" + value + "' to double: " + e.getMessage());
			}
		}

		throw new IllegalArgumentException("Cannot convert value of type " + value.getClass().getName() + " to double");
	}

	/**
	 * Get a double value from a Map
	 * @param map Source map
	 * @param key The key
	 * @return The double value
	 * @throws IllegalArgumentException If value cannot be converted to double
	 */
	public static float getFloatValue(Map<String, Object> map, String key) throws IllegalArgumentException {
		Object value = map == null || key == null ? null : map.get(key);
		if (value == null) {
			return 0;
		}

		if (value instanceof Number) {
			return ((Number) value).floatValue();
		}

View on GitHub (pinned to 5284052872)

Solutions

  1. Trust the class name in the message; log the raw value at that key to confirm the structural mismatch.
  2. Read the scalar from the right key or unwrap the container before converting.
  3. Fix the producer/middleware so only Number or numeric String occupies the key.
  4. Guard with instanceof (Number || String) and handle other types explicitly.

Example fix

// before
Float rate = JSON.getFloat(cfg, "rate"); // throws when "rate" holds {"value": 0.5}

// after
Object raw = cfg.get("rate");
if (raw instanceof Map) { raw = ((Map<?, ?>) raw).get("value"); }
Float rate = raw instanceof Number ? ((Number) raw).floatValue() : null;
Defensive patterns

Strategy: type-guard

Validate before calling

Object v = cfg.get("rate");
if (v != null && !(v instanceof Number) && !(v instanceof String)) {
    throw new IllegalStateException("'rate' must be number or numeric string, got " + v.getClass().getName());
}

Type guard

static boolean isNumberOrString(Object v) {
    return v == null || v instanceof Number || v instanceof String;
}

Try / catch

try {
    Float rate = JSON.getFloat(cfg, "rate");
} catch (IllegalArgumentException e) {
    log.warn("Wrong type at 'rate' (message says 'double' — known wording bug): {}", e.getMessage());
    // unwrap or reject
}

Prevention

When it happens

Trigger: A float field holds a nested object/array after schema drift; a Boolean where a numeric flag was expected; an internal Java object (Date, Enum, byte[]) stored in the map under that key; the wrong key name used.

Common situations: API versioning restructuring scalar fields; middleware attaching objects to shared maps; heterogeneous tenant payloads; test fixtures with wrong literal types; confusion from the "double" wording sending developers to inspect getDouble code paths.

Related errors


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