Tencent/APIJSON · error · IllegalArgumentException
Cannot convert value of type " + value.getClass().getName()
Error message
Cannot convert value of type " + value.getClass().getName() + " to long
What it means
Thrown by apijson.JSON.getLongValue(Map, String) (APIJSONORM/src/main/java/apijson/JSON.java:473) when the value at the key is neither null, Number, nor String. The message names the actual runtime class (Map, List, Boolean, ...), which is the primary clue that a structural mismatch, not a parsing problem, occurred.
Source
Thrown at APIJSONORM/src/main/java/apijson/JSON.java:473
public static long getLongValue(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).longValue();
}
if (value instanceof String) {
try {
return Long.parseLong((String) value);
} catch (NumberFormatException e) {
throw new IllegalArgumentException("Cannot convert String value '" + value + "' to long: " + e.getMessage());
}
}
throw new IllegalArgumentException("Cannot convert value of type " + value.getClass().getName() + " to long");
}
/**
* 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 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();
}View on GitHub (pinned to 5284052872)
Solutions
- Log the class from the message and the raw value to confirm the shape mismatch.
- Read the scalar from the correct key or unwrap the container explicitly before converting.
- Restore the producer contract: Number or numeric String only under this key.
- Add a type guard and your own handling for unexpected types instead of letting the library throw.
Example fix
// before
long sid = JSON.getLongValue(session, "sid"); // throws when "sid" holds a Map
// after
Object raw = session.get("sid");
long sid = raw instanceof Number ? ((Number) raw).longValue()
: (raw instanceof String ? Long.parseLong(((String) raw).trim()) : 0); Defensive patterns
Strategy: type-guard
Validate before calling
Object v = session.get("sid");
if (v != null && !(v instanceof Number) && !(v instanceof String)) {
throw new IllegalStateException("'sid' 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 {
long sid = JSON.getLongValue(session, "sid");
} catch (IllegalArgumentException e) {
log.warn("Wrong type at 'sid': {}", e.getMessage());
// unwrap container or fail session lookup
} Prevention
- Keep ID fields scalar across schema versions.
- Validate container-free values at the boundary.
- Log class names on conversion failures to spot drift quickly.
When it happens
Trigger: A long field holding a nested object or array after upstream schema drift; a Boolean stored where a 0/1 flag integer was expected; the wrong key constant used (one bound to a container); an internal Java object leaked into the request map.
Common situations: Backend versioning changes that restructure scalar fields; shared request maps reused across pipeline stages; environment-specific payload shapes (one tenant sends extra wrapping); fixtures built with mismatched types.
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 Number value '" + value + "' to boolean. Only
AI-assisted analysis of Tencent/APIJSON@5284052872 (2026-08-14).
Data as JSON: /api/errors/39f2957d431229ba.
Report an issue: GitHub.