{"record":{"id":"9cda0ac2d5c58372","repo":"apple/pkl","slug":"valid-escape-sequence","errorCode":null,"errorMessage":"valid escape sequence","messagePattern":"valid escape sequence","errorType":"exception","errorClass":"ParseException","httpStatus":null,"severity":"error","filePath":"pkl-core/src/main/java/org/pkl/core/util/json/JsonParser.java","lineNumber":288,"sourceCode":"    switch (current) {\n      case '\"', '/', '\\\\' -> captureBuffer.append((char) current);\n      case 'b' -> captureBuffer.append('\\b');\n      case 'f' -> captureBuffer.append('\\f');\n      case 'n' -> captureBuffer.append('\\n');\n      case 'r' -> captureBuffer.append('\\r');\n      case 't' -> captureBuffer.append('\\t');\n      case 'u' -> {\n        var hexChars = new char[4];\n        for (var i = 0; i < 4; i++) {\n          read();\n          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();","sourceCodeStart":270,"sourceCodeEnd":306,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-core/src/main/java/org/pkl/core/util/json/JsonParser.java#L270-L306","documentation":"JSON strings only allow a fixed set of escapes: \\\" \\\\ \\/ \\b \\f \\n \\r \\t and \\uXXXX. Any other character after a backslash hits the default branch of readEscape and throws expected(\"valid escape sequence\"). This rejects escapes borrowed from other languages, such as \\x41, \\0, or \\' (escaped single quote).","triggerScenarios":"parse() on strings containing \\' (common when a tool escapes single quotes), \\xNN hex escapes, \\0, \\e, or backslash-escaped characters invented by custom serializers.","commonSituations":"Output from non-JSON serializers (shell/SQL string escaping reused for JSON), string escaping performed twice or with the wrong escaping function, hand-written config copying escaping habits from other languages.","solutions":["Replace the unsupported escape with a valid one: use \\' -> ', \\x41 -> A or \\u0041, \\0 -> \\u0000.","Re-serialize the data with a real JSON encoder instead of reusing another language's escaping routine.","Search the input for backslashes and audit each escape against the JSON-allowed set.","Catch ParseException and use its position to fix the exact offending escape."],"exampleFix":"// before\nString json = \"{\\\"quote\\\": \\\\\"it\\\\\\\\'s\\\\\\\\\"}\"; // \\' is not a JSON escape\n// after\nString json = \"{\\\"quote\\\": \\\\\"it's\\\\\\\\\"}\"; // ' needs no escaping","handlingStrategy":"validation","validationCode":"// Java: detect escapes outside the JSON-allowed set\nif (java.util.regex.Pattern.compile(\"\\\\\\\\(?!\\\"|\\\\\\\\|/|b|f|n|r|t|u)\").matcher(json).find()) {\n  throw new IllegalArgumentException(\"Invalid JSON escape sequence found\");\n}","typeGuard":null,"tryCatchPattern":"try {\n  parser.parse(json);\n} catch (ParseException e) {\n  if (e.getMessage().contains(\"valid escape sequence\")) {\n    throw new IllegalArgumentException(\"Unsupported escape in JSON string at \" + e.getLocation().line + \":\" + e.getLocation().column + \" — only \\\" \\\\ / b f n r t u are allowed\", e);\n  }\n  throw e;\n}","preventionTips":["Only use \\\" \\\\ / \\b \\f \\n \\r \\t \\uXXXX in JSON strings.","Never reuse shell/SQL/JS escaping routines for JSON.","Escape once, with the correct JSON encoder for your language.","Audit hand-written config for \\' or \\x escapes copied from other languages."],"tags":["json","parser","string-escaping","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-17T15:17:12.973Z"}