{"record":{"id":"39f2957d431229ba","repo":"Tencent/APIJSON","slug":"cannot-convert-value-of-type-value-getclass-39f295","errorCode":null,"errorMessage":"Cannot convert value of type \" + value.getClass().getName() + \" to long","messagePattern":"Cannot convert value of type \" \\+ value\\.getClass\\(\\)\\.getName\\(\\) \\+ \" to long","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"APIJSONORM/src/main/java/apijson/JSON.java","lineNumber":473,"sourceCode":"\tpublic static long getLongValue(Map<String, Object> map, String key) throws IllegalArgumentException {\n\t\tObject value = map == null || key == null ? null : map.get(key);\n\t\tif (value == null) {\n\t\t\treturn 0;\n\t\t}\n\n\t\tif (value instanceof Number) {\n\t\t\treturn ((Number) value).longValue();\n\t\t}\n\n\t\tif (value instanceof String) {\n\t\t\ttry {\n\t\t\t\treturn Long.parseLong((String) value);\n\t\t\t} catch (NumberFormatException e) {\n\t\t\t\tthrow new IllegalArgumentException(\"Cannot convert String value '\" + value + \"' to long: \" + e.getMessage());\n\t\t\t}\n\t\t}\n\n\t\tthrow new IllegalArgumentException(\"Cannot convert value of type \" + value.getClass().getName() + \" to long\");\n\t}\n\n\t/**\n\t * Get a double value from a Map\n\t * @param map Source map\n\t * @param key The key\n\t * @return The double value\n\t * @throws IllegalArgumentException If value cannot be converted to double\n\t */\n\tpublic static Float getFloat(Map<String, Object> map, String key) throws IllegalArgumentException {\n\t\tObject value = map == null || key == null ? null : map.get(key);\n\t\tif (value == null) {\n\t\t\treturn null;\n\t\t}\n\n\t\tif (value instanceof Number) {\n\t\t\treturn ((Number) value).floatValue();\n\t\t}","sourceCodeStart":455,"sourceCodeEnd":491,"githubUrl":"https://github.com/Tencent/APIJSON/blob/5284052872898eddc449a58f629e5c8d588b8e22/APIJSONORM/src/main/java/apijson/JSON.java#L455-L491","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nlong sid = JSON.getLongValue(session, \"sid\"); // throws when \"sid\" holds a Map\n\n// after\nObject raw = session.get(\"sid\");\nlong sid = raw instanceof Number ? ((Number) raw).longValue()\n        : (raw instanceof String ? Long.parseLong(((String) raw).trim()) : 0);","handlingStrategy":"type-guard","validationCode":"Object v = session.get(\"sid\");\nif (v != null && !(v instanceof Number) && !(v instanceof String)) {\n    throw new IllegalStateException(\"'sid' must be number or numeric string, got \" + v.getClass().getName());\n}","typeGuard":"static boolean isNumberOrString(Object v) {\n    return v == null || v instanceof Number || v instanceof String;\n}","tryCatchPattern":"try {\n    long sid = JSON.getLongValue(session, \"sid\");\n} catch (IllegalArgumentException e) {\n    log.warn(\"Wrong type at 'sid': {}\", e.getMessage());\n    // unwrap container or fail session lookup\n}","preventionTips":["Keep ID fields scalar across schema versions.","Validate container-free values at the boundary.","Log class names on conversion failures to spot drift quickly."],"tags":["json","type-conversion","apijson","schema-mismatch"],"backgroundTag":null,"analyzedSha":"5284052872898eddc449a58f629e5c8d588b8e22","analyzedAt":"2026-08-14T15:15:29.577Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}