{"record":{"id":"a54ff892d6658840","repo":"google/gson","slug":"cannot-parse-value-at-path-in-getpreviouspat","errorCode":null,"errorMessage":"Cannot parse ${value}; at path ${in.getPreviousPath()}","messagePattern":"Cannot parse (.+?); at path (.+?)","errorType":"exception","errorClass":"JsonParseException","httpStatus":null,"severity":"error","filePath":"gson/src/main/java/com/google/gson/ToNumberPolicy.java","lineNumber":93,"sourceCode":"      } else {\n        try {\n          return Long.parseLong(value);\n        } catch (NumberFormatException e) {\n          return parseAsDouble(value, in);\n        }\n      }\n    }\n\n    private Number parseAsDouble(String value, JsonReader in) throws IOException {\n      try {\n        Double d = Double.valueOf(value);\n        if ((d.isInfinite() || d.isNaN()) && !in.isLenient()) {\n          throw new MalformedJsonException(\n              \"JSON forbids NaN and infinities: \" + d + \"; at path \" + in.getPreviousPath());\n        }\n        return d;\n      } catch (NumberFormatException e) {\n        throw new JsonParseException(\n            \"Cannot parse \" + value + \"; at path \" + in.getPreviousPath(), e);\n      }\n    }\n  },\n\n  /**\n   * Using this policy will ensure that numbers will be read as numbers of arbitrary length using\n   * {@link BigDecimal}.\n   */\n  BIG_DECIMAL {\n    @Override\n    public BigDecimal readNumber(JsonReader in) throws IOException {\n      String value = in.nextString();\n      try {\n        return NumberLimits.parseBigDecimal(value);\n      } catch (NumberFormatException e) {\n        throw new JsonParseException(\n            \"Cannot parse \" + value + \"; at path \" + in.getPreviousPath(), e);","sourceCodeStart":75,"sourceCodeEnd":111,"githubUrl":"https://github.com/google/gson/blob/310ac341f2f92a454b229bf21f70d2d18b2b6db7/gson/src/main/java/com/google/gson/ToNumberPolicy.java#L75-L111","documentation":"Thrown by LONG_OR_DOUBLE.parseAsDouble() when Double.valueOf(value) raises NumberFormatException for a JSON number string. JsonReader pre-validates number syntax, so this only occurs for unusual token forms that slip through the lexer but are rejected by Double.valueOf. The message includes the value and JSON path.","triggerScenarios":"Deserializing numbers with ToNumberPolicy.LONG_OR_DOUBLE configured; a JSON numeric token accepted by JsonReader's lexer but not parseable by Double.valueOf (e.g. edge cases in exponent notation, custom JsonReader subclasses emitting non-standard tokens).","commonSituations":"Rare under normal JsonReader usage; occurs with custom JsonReader implementations, malformed streams from non-standard producers, or version differences in number lexing.","solutions":["Switch to ToNumberPolicy.LAZILY_PARSED_NUMBER which defers parsing and avoids the Double.valueOf path","Switch to ToNumberPolicy.DOUBLE which uses JsonReader.nextDouble() (validated at the reader level)","Investigate and fix the malformed numeric token at the source"],"exampleFix":"// before\ngsonBuilder.setNumberToNumberStrategy(ToNumberPolicy.LONG_OR_DOUBLE);\n\n// after\ngsonBuilder.setNumberToNumberStrategy(ToNumberPolicy.LAZILY_PARSED_NUMBER);","handlingStrategy":"fallback","validationCode":"// Pre-validate numeric tokens if using LONG_OR_DOUBLE with a custom source\nString numStr = extractNumber(json, path);\ntry {\n  Double.valueOf(numStr);\n} catch (NumberFormatException e) {\n  // fall back: treat as string or skip\n  return null;\n}","typeGuard":null,"tryCatchPattern":"try {\n  return gson.fromJson(json, type);\n} catch (JsonParseException e) {\n  if (e.getMessage().startsWith(\"Cannot parse\")) {\n    // retry with a more lenient number strategy\n    Gson fallback = new GsonBuilder()\n      .setObjectToNumberStrategy(ToNumberPolicy.LAZILY_PARSED_NUMBER)\n      .create();\n    return fallback.fromJson(json, type);\n  }\n  throw e;\n}","preventionTips":["Prefer ToNumberPolicy.LAZILY_PARSED_NUMBER or DOUBLE over LONG_OR_DOUBLE unless you need Long/Double dispatching","If you control the producer, ensure number tokens are valid Double literals","Wrap deserialization in a fallback that switches number strategy on failure"],"tags":["number-parsing","deserialization","double"],"backgroundTag":null,"analyzedSha":"310ac341f2f92a454b229bf21f70d2d18b2b6db7","analyzedAt":"2026-08-10T02:58:47.455Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}