google/gson · error · JsonParseException
Cannot parse ; at path
Error message
Cannot parse ${value}; at path ${in.getPreviousPath()} What it means
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.
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
Example fix
// before gsonBuilder.setNumberToNumberStrategy(ToNumberPolicy.LONG_OR_DOUBLE); // after gsonBuilder.setNumberToNumberStrategy(ToNumberPolicy.LAZILY_PARSED_NUMBER);
Defensive patterns
Strategy: fallback
Validate before calling
// Pre-validate numeric tokens if using LONG_OR_DOUBLE with a custom source
String numStr = extractNumber(json, path);
try {
Double.valueOf(numStr);
} catch (NumberFormatException e) {
// fall back: treat as string or skip
return null;
} Try / catch
try {
return gson.fromJson(json, type);
} catch (JsonParseException e) {
if (e.getMessage().startsWith("Cannot parse")) {
// retry with a more lenient number strategy
Gson fallback = new GsonBuilder()
.setObjectToNumberStrategy(ToNumberPolicy.LAZILY_PARSED_NUMBER)
.create();
return fallback.fromJson(json, type);
}
throw e;
} Prevention
- 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
When it happens
Trigger: 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).
Common situations: Rare under normal JsonReader usage; occurs with custom JsonReader implementations, malformed streams from non-standard producers, or version differences in number lexing.
Related errors
- JSON forbids NaN and infinities
- String contains non-ASCII characters
- Array must have size 1, but has size
- Attempted to deserialize a java.lang.Class. Forgot to…
- Cannot allocate . Usage of JDK sun.misc.Unsafe is enabled…
AI-assisted analysis of google/gson@310ac341f2 (2026-08-10).
Data as JSON: /api/errors/a54ff892d6658840.
Report an issue: GitHub.
Appendix: source
Thrown at gson/src/main/java/com/google/gson/ToNumberPolicy.java:93
} else {
try {
return Long.parseLong(value);
} catch (NumberFormatException e) {
return parseAsDouble(value, in);
}
}
}
private Number parseAsDouble(String value, JsonReader in) throws IOException {
try {
Double d = Double.valueOf(value);
if ((d.isInfinite() || d.isNaN()) && !in.isLenient()) {
throw new MalformedJsonException(
"JSON forbids NaN and infinities: " + d + "; at path " + in.getPreviousPath());
}
return d;
} catch (NumberFormatException e) {
throw new JsonParseException(
"Cannot parse " + value + "; at path " + in.getPreviousPath(), e);
}
}
},
/**
* Using this policy will ensure that numbers will be read as numbers of arbitrary length using
* {@link BigDecimal}.
*/
BIG_DECIMAL {
@Override
public BigDecimal readNumber(JsonReader in) throws IOException {
String value = in.nextString();
try {
return NumberLimits.parseBigDecimal(value);
} catch (NumberFormatException e) {
throw new JsonParseException(
"Cannot parse " + value + "; at path " + in.getPreviousPath(), e);View on GitHub (pinned to 310ac341f2)