{"record":{"id":"46877358475c9f4b","repo":"Tencent/APIJSON","slug":"cannot-convert-string-value-value-to-lon","errorCode":null,"errorMessage":"Cannot convert String value '\" + value + \"' to long: \" + e.getMessage()","messagePattern":"Cannot convert String value '\" \\+ value \\+ \"' to long: \" \\+ e\\.getMessage\\(\\)","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"APIJSONORM/src/main/java/apijson/JSON.java","lineNumber":469,"sourceCode":"\t * @param key The key\n\t * @return The long value\n\t * @throws IllegalArgumentException If value cannot be converted to long\n\t */\n\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}","sourceCodeStart":451,"sourceCodeEnd":487,"githubUrl":"https://github.com/Tencent/APIJSON/blob/5284052872898eddc449a58f629e5c8d588b8e22/APIJSONORM/src/main/java/apijson/JSON.java#L451-L487","documentation":"Thrown by apijson.JSON.getLongValue(Map, String) (APIJSONORM/src/main/java/apijson/JSON.java:469) when the value is a String that Long.parseLong cannot parse. Unlike the getLong overload, the message correctly says \"to long\". Accepted strings are exact integer text within long range — no whitespace, decimals, separators, or empty string.","triggerScenarios":"Value is \"1710000000000.0\", \" 1710000000000 \", \"\", \"1,710,000,000,000\", or a >19-digit string that overflows long.","commonSituations":"Millisecond timestamps and snowflake IDs serialized as strings by JS frontends (Number precision loss); locale-formatted large numbers; blank strings sent instead of omitted fields; config files with quoted numbers and stray spaces.","solutions":["Trim and normalize the string (strip separators; BigDecimal-parse decimal text) before relying on getLongValue.","Send bare JSON numbers from the producer so the Number branch is taken.","Treat blank strings as missing and skip the call — getLongValue returns 0 for null values.","For strings that may exceed long range, keep the value as String; do not force it into a long."],"exampleFix":"// before\nlong ts = JSON.getLongValue(payload, \"timestamp\"); // throws on \"1710000000000.0\"\n\n// after\nObject raw = payload.get(\"timestamp\");\nlong ts = raw instanceof Number ? ((Number) raw).longValue()\n        : (raw instanceof String && !((String) raw).isBlank() ? new java.math.BigDecimal(((String) raw).trim()).longValue() : 0);","handlingStrategy":"validation","validationCode":"Object v = payload.get(\"timestamp\");\nif (v instanceof String) {\n    String s = ((String) v).trim();\n    if (!s.matches(\"[+-]?\\\\d+\")) throw new IllegalArgumentException(\"'timestamp' not integer text: \" + s);\n    if (new java.math.BigInteger(s).bitLength() > 63) throw new IllegalArgumentException(\"'timestamp' exceeds long: \" + s);\n}","typeGuard":"static boolean isParsableLong(Object v) {\n    if (v instanceof Number) return true;\n    if (v instanceof String) { String s = ((String) v).trim(); return s.matches(\"[+-]?\\\\d+\") && new java.math.BigInteger(s).bitLength() <= 63; }\n    return false;\n}","tryCatchPattern":"try {\n    long ts = JSON.getLongValue(payload, \"timestamp\");\n} catch (IllegalArgumentException e) {\n    log.warn(\"Bad long at 'timestamp': {}\", e.getMessage());\n    // default to 0 or reject the payload\n}","preventionTips":["Serialize timestamps and snowflake IDs as bare numbers or clean digit strings.","Validate digit-only, 64-bit-range strings at the boundary.","Watch for decimal text like \"123.0\" from JS producers — normalize before parsing."],"tags":["json","type-conversion","apijson","number-parsing"],"backgroundTag":null,"analyzedSha":"5284052872898eddc449a58f629e5c8d588b8e22","analyzedAt":"2026-08-14T15:15:29.577Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}