{"id":"1ebe83382dc9d2ab","repo":"apache/kafka","slug":"about-failed-to-parse-number-cause","errorCode":null,"errorMessage":"${about}: failed to parse number: ${cause}","messagePattern":"(.+?): failed to parse number: (.+?)","errorType":"exception","errorClass":"NumberFormatException","httpStatus":null,"severity":"error","filePath":"clients/src/main/java/org/apache/kafka/common/protocol/MessageUtil.java","lineNumber":133,"sourceCode":"            return node.asInt();\n        }\n        if (node.isTextual()) {\n            throw new NumberFormatException(about + \": expected an integer or \" +\n                \"string type, but got \" + node.getNodeType());\n        }\n        String text = node.asText();\n        if (text.startsWith(\"0x\")) {\n            try {\n                return Integer.parseInt(text.substring(2), 16);\n            } catch (NumberFormatException e) {\n                throw new NumberFormatException(about + \": failed to \" +\n                    \"parse hexadecimal number: \" + e.getMessage());\n            }\n        } else {\n            try {\n                return Integer.parseInt(text);\n            } catch (NumberFormatException e) {\n                throw new NumberFormatException(about + \": failed to \" +\n                    \"parse number: \" + e.getMessage());\n            }\n        }\n    }\n\n    public static long jsonNodeToLong(JsonNode node, String about) {\n        if (node.isLong()) {\n            return node.asLong();\n        }\n        if (node.isTextual()) {\n            throw new NumberFormatException(about + \": expected an integer or \" +\n                \"string type, but got \" + node.getNodeType());\n        }\n        String text = node.asText();\n        if (text.startsWith(\"0x\")) {\n            try {\n                return Long.parseLong(text.substring(2), 16);\n            } catch (NumberFormatException e) {","sourceCodeStart":115,"sourceCodeEnd":151,"githubUrl":"https://github.com/apache/kafka/blob/c31c9215e131f8c17e79f8901b48c13ee6aa8e7a/clients/src/main/java/org/apache/kafka/common/protocol/MessageUtil.java#L115-L151","documentation":"Thrown by MessageUtil.jsonNodeToInt when asText() of a non-int, non-textual node yields a string that does not parse as a base-10 integer. Because textual JSON values are rejected at line 118, this fires for other node kinds whose asText() is not integral — most commonly DoubleNode ('3.5'), BooleanNode ('true'), NullNode ('null'), or array/object nodes (whose asText() is empty).","triggerScenarios":"A JSON value such as 3.5, true, null, [] or {} is supplied where a int32 field is expected. node.isInt()==false, node.isTextual()==false, asText() returns '3.5'/'true'/'null'/'' and Integer.parseInt throws — line 133 wraps it as NumberFormatException.","commonSituations":"Passing a float where the schema demands an int (e.g. partition count of 3.0); passing true/false for a numeric flag; missing values that deserialize to null; copy/pasting from a config that used scientific notation; using a JSON array where a single int was expected.","solutions":["Inspect the 'about' prefix and the embedded e.getMessage() to identify both the field and the exact token Jackson emitted via asText().","Change the JSON value to an integer literal of the correct magnitude.","If the source data is genuinely fractional/null/boolean, normalize it upstream (round, default, project) before serializing into the protocol message.","Validate the JSON with the generated Message'schema before submission."],"exampleFix":"// before\n{\"partition_count\": 3.0}\n// after\n{\"partition_count\": 3}","handlingStrategy":"validation","validationCode":"String t = node.asText();\ntry { Integer.parseInt(t); } catch (NumberFormatException ex) { throw new IllegalArgumentException(\"unparseable int: \" + t, ex); }","typeGuard":"node != null && node.isInt() || (node.isTextual() && node.asText().matches(\"-?\\d{1,10}\") && { try { Integer.parseInt(node.asText()); true } catch { false } })","tryCatchPattern":"try { int i = MessageUtil.jsonNodeToInt(node, about); }\ncatch (NumberFormatException e) { /* non-numeric string in int field; quarantine record */ }","preventionTips":["Strip whitespace and locale-specific separators from numeric strings before serialization.","Validate numeric strings against ^-?\\d+$ at ingest.","Prefer native JSON integers over string-encoded numbers."],"tags":["kafka","protocol","json","serialization","parse-error","int32"],"analyzedSha":"c31c9215e131f8c17e79f8901b48c13ee6aa8e7a","analyzedAt":"2026-08-03T12:34:05.770Z","schemaVersion":2}