{"record":{"id":"db6e41cff7859759","repo":"apache/dubbo","slug":"value-s-for-key-s-is-not-a-long-integer","errorCode":null,"errorMessage":"value '%s' for key '%s' is not a long integer","messagePattern":"value '(.+?)' for key '(.+?)' is not a long integer","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"dubbo-common/src/main/java/org/apache/dubbo/common/json/impl/AbstractJsonUtilImpl.java","lineNumber":193,"sourceCode":"        assert obj != null;\n        assert key != null;\n        if (!obj.containsKey(key)) {\n            return null;\n        }\n        Object value = obj.get(key);\n        if (value instanceof Double) {\n            Double d = (Double) value;\n            long l = d.longValue();\n            if (l != d) {\n                throw new ClassCastException(\"Number expected to be long: \" + d);\n            }\n            return l;\n        }\n        if (value instanceof String) {\n            try {\n                return Long.parseLong((String) value);\n            } catch (NumberFormatException e) {\n                throw new IllegalArgumentException(\n                        String.format(\"value '%s' for key '%s' is not a long integer\", value, key));\n            }\n        }\n        throw new IllegalArgumentException(String.format(\"value '%s' for key '%s' is not a long integer\", value, key));\n    }\n\n    /**\n     * Gets a string from an object for the given key.  If the key is not present, this returns null.\n     * If the value is not a String, throws an exception.\n     */\n    @Override\n    public String getString(Map<String, ?> obj, String key) {\n        assert obj != null;\n        assert key != null;\n        if (!obj.containsKey(key)) {\n            return null;\n        }\n        Object value = obj.get(key);","sourceCodeStart":175,"sourceCodeEnd":211,"githubUrl":"https://github.com/apache/dubbo/blob/3a3043227f5571d25eb2889de5bca22f2914843b/dubbo-common/src/main/java/org/apache/dubbo/common/json/impl/AbstractJsonUtilImpl.java#L175-L211","documentation":"Thrown by AbstractJsonUtilImpl.getNumberAsLong(Map,String) when obj contains key, the value is a String, but Long.parseLong throws NumberFormatException. This is the string-parse failure path for the long accessor; the IllegalArgumentException names the value and key. A true Double value is handled separately (error 194 for fractional, success for whole); non-Double/non-String values hit error 196.","triggerScenarios":"jsonUtil.getNumberAsLong(obj, key) where obj.get(key) is a String not parseable as a base-10 long — e.g. \"1.5\", \"abc\", \"\", \"1_000\", hex/scientific notation, or a decimal point where an integer was expected.","commonSituations":"A decimal string (\"42.0\") where a long ID was expected; thousands separators; locale formatting; a value within double range but written with a decimal portion; free-text where a number was expected.","solutions":["If the value may be a decimal string, parse as Double then cast/round: (long) Double.parseDouble(s).","Sanitize the string (trim, strip separators) before relying on it.","If the field is genuinely fractional, use getNumberAsDouble instead.","Align the producer to emit plain integer strings or JSON numbers."],"exampleFix":"// before\nLong ts = jsonUtil.getNumberAsLong(obj, \"ts\"); // \"1700000000.0\"\n// after - tolerate decimal strings\nObject raw = obj.get(\"ts\");\nLong ts = raw instanceof String ? (long) Double.parseDouble((String) raw) : jsonUtil.getNumberAsLong(obj, \"ts\");","handlingStrategy":"validation","validationCode":"Object raw = obj.get(key);\nif (raw instanceof String) {\n    String s = (String) raw;\n    try {\n        Long.parseLong(s); // pre-check\n    } catch (NumberFormatException e) {\n        try { return (long) Double.parseDouble(s); }\n        catch (NumberFormatException ignored) { return null; }\n    }\n}\nreturn jsonUtil.getNumberAsLong(obj, key);","typeGuard":"private static boolean isParsableLong(Object value) {\n    if (value instanceof Double) return value.equals(Math.rint((Double) value));\n    if (value instanceof String) {\n        try { Long.parseLong((String) value); return true; }\n        catch (NumberFormatException e) { return false; }\n    }\n    return false;\n}","tryCatchPattern":"try {\n    return jsonUtil.getNumberAsLong(obj, key);\n} catch (IllegalArgumentException e) {\n    if (e.getMessage() != null && e.getMessage().contains(\"is not a long integer\")) {\n        Object raw = obj.get(key);\n        if (raw instanceof String) return (long) Double.parseDouble((String) raw);\n    }\n    throw e;\n}","preventionTips":["Strip separators and trim before parsing long strings.","If the value may be a decimal string, parse as Double then cast to long.","Align the producer to emit plain integer strings or JSON numbers for long fields."],"tags":["json","number-parsing","argument","jsonutil","dubbo-common"],"backgroundTag":null,"analyzedSha":"3a3043227f5571d25eb2889de5bca22f2914843b","analyzedAt":"2026-08-14T00:43:19.853Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}