{"record":{"id":"7c358782f252c242","repo":"apple/pkl","slug":"digit","errorCode":null,"errorMessage":"digit","messagePattern":"digit","errorType":"exception","errorClass":"ParseException","httpStatus":null,"severity":"error","filePath":"pkl-core/src/main/java/org/pkl/core/util/json/JsonParser.java","lineNumber":299,"sourceCode":"          if (!isHexDigit()) {\n            throw expected(\"hexadecimal digit\");\n          }\n          hexChars[i] = (char) current;\n        }\n        captureBuffer.append((char) Integer.parseInt(new String(hexChars), 16));\n      }\n      default -> throw expected(\"valid escape sequence\");\n    }\n    read();\n  }\n\n  private void readNumber() throws IOException {\n    handler.startNumber();\n    startCapture();\n    readChar('-');\n    var firstDigit = current;\n    if (!readDigit()) {\n      throw expected(\"digit\");\n    }\n    if (firstDigit != '0') {\n      //noinspection StatementWithEmptyBody\n      while (readDigit()) {}\n    }\n    readFraction();\n    readExponent();\n    handler.endNumber(endCapture());\n  }\n\n  @SuppressWarnings(\"UnusedReturnValue\")\n  private boolean readFraction() throws IOException {\n    if (!readChar('.')) {\n      return false;\n    }\n    if (!readDigit()) {\n      throw expected(\"digit\");\n    }","sourceCodeStart":281,"sourceCodeEnd":317,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-core/src/main/java/org/pkl/core/util/json/JsonParser.java#L281-L317","documentation":"A JSON number must start with at least one decimal digit after the optional minus sign, and leading zeros are not allowed (readDigit's first result is checked; if the first digit is '0', no further digits may follow it). readNumber throws expected(\"digit\") when the character after '-' (or at the number's start) is not a digit — e.g. \"-\", \"-x\", or \".5\" where a number was expected.","triggerScenarios":"parse() on inputs like \"{\\\"n\\\": -}\" (dangling minus), \"{\\\"n\\\": .5}\" (leading dot), or a value position holding a non-number character, where readValue dispatched to readNumber because of '-' or a digit-like start.","commonSituations":"Hand-written JSON with .5 instead of 0.5, template variables left empty (\"-\" with the number interpolated away), NaN/Infinity pasted from JavaScript (not valid JSON numbers), truncated numeric output.","solutions":["Write the number with a leading digit: 0.5 instead of .5, and ensure '-' is followed by digits.","Replace NaN/Infinity with null or strings — they are not valid JSON numbers.","Check templating: if a variable failed to interpolate, the value position may be empty or a lone '-'.","Catch ParseException and use its position to inspect the malformed number token."],"exampleFix":"// before\nString json = \"{\\\"ratio\\\": .5}\"; // no leading digit\n// after\nString json = \"{\\\"ratio\\\": 0.5}\";","handlingStrategy":"validation","validationCode":"// Java: reject numbers lacking a leading digit before parsing\nif (java.util.regex.Pattern.compile(\"(^|[\\\\[,\\\\s:])(-?)(\\\\.\\\\D|[-.]\\\\s*[,}\\\\]])\").matcher(json).find()) {\n  throw new IllegalArgumentException(\"JSON numbers require at least one leading digit (0.5 not .5)\");\n}","typeGuard":null,"tryCatchPattern":"try {\n  parser.parse(json);\n} catch (ParseException e) {\n  if (e.getMessage().contains(\"digit\")) {\n    throw new IllegalArgumentException(\"Malformed JSON number at \" + e.getLocation().line + \":\" + e.getLocation().column + \" — a digit was required\", e);\n  }\n  throw e;\n}","preventionTips":["Write 0.5, not .5; ensure '-' is always followed by digits.","Replace NaN/Infinity with null — they are not valid JSON.","Verify template variables actually interpolated into number positions.","Format numbers with a JSON-aware serializer, not printf without fraction digits."],"tags":["json","parser","number-format","pkl"],"backgroundTag":"json-parse-error","analyzedSha":"f3efcbfc9b60d30053b0536d664948d7aa1b8673","analyzedAt":"2026-09-08T13:10:45.570Z","contentChangedAt":"2026-09-08T13:10:45.570Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}