google/gson · error · JsonSyntaxException
Failed parsing '${s}' as BigDecimal; at path ${path}
Error message
Failed parsing '${s}' as BigDecimal; at path ${path} What it means
The BIG_DECIMAL adapter reads a JSON token as a string and passes it to NumberLimits.parseBigDecimal. If the string is not a valid BigDecimal representation, or exceeds the number-string-length or scale limits enforced by NumberLimits, the NumberFormatException is wrapped in a JsonSyntaxException with the offending value and path.
Source
Thrown at gson/src/main/java/com/google/gson/internal/bind/TypeAdapters.java:593
@Override
public void write(JsonWriter out, String value) throws IOException {
out.value(value);
}
};
public static final TypeAdapter<BigDecimal> BIG_DECIMAL =
new TypeAdapter<BigDecimal>() {
@Override
public BigDecimal read(JsonReader in) throws IOException {
if (in.peek() == JsonToken.NULL) {
in.nextNull();
return null;
}
String s = in.nextString();
try {
return NumberLimits.parseBigDecimal(s);
} catch (NumberFormatException e) {
throw new JsonSyntaxException(
"Failed parsing '" + s + "' as BigDecimal; at path " + in.getPreviousPath(), e);
}
}
@Override
public void write(JsonWriter out, BigDecimal value) throws IOException {
out.value(value);
}
};
public static final TypeAdapterFactory BIG_DECIMAL_FACTORY =
newFactory(BigDecimal.class, BIG_DECIMAL);
public static final TypeAdapter<BigInteger> BIG_INTEGER =
new TypeAdapter<BigInteger>() {
@Override
public BigInteger read(JsonReader in) throws IOException {
if (in.peek() == JsonToken.NULL) {View on GitHub (pinned to 310ac341f2)
Solutions
- Validate that the JSON value is a well-formed decimal number before deserializing into BigDecimal
- If the value can be non-numeric, use String as the field type and convert to BigDecimal separately
- For known oversized inputs, register a custom TypeAdapter that pre-checks or truncates
Example fix
// before
class Product { BigDecimal price; }
gson.fromJson("{\"price\":\"N/A\"}", Product.class); // throws
// after
class Product { String price; // parse to BigDecimal after validation
BigDecimal priceValue() { return new BigDecimal(price); }
} Defensive patterns
Strategy: validation
Validate before calling
// Validate that a string is a parseable BigDecimal before deserializing
public static boolean isValidBigDecimal(String s) {
try {
NumberLimits.parseBigDecimal(s);
return true;
} catch (NumberFormatException e) {
return false;
}
} Try / catch
try {
Product p = gson.fromJson(json, Product.class);
} catch (JsonSyntaxException e) {
if (e.getMessage().contains("Failed parsing") && e.getMessage().contains("BigDecimal")) {
// use a String field and parse manually with error handling
}
} Prevention
- Validate numeric strings are well-formed decimals before deserializing into BigDecimal fields
- Use String as the field type for values that may contain non-numeric text, and convert separately
- Be aware NumberLimits rejects strings longer than 10,000 characters or with scale >= 10,000
When it happens
Trigger: Deserialifying a JSON value like "abc" or "1.2.3" or an excessively long number string into a BigDecimal field.
Common situations: Malformed numeric strings from an API; mixed-type fields that sometimes contain non-numeric text; a value with a scale exceeding 10,000 digits (a NumberLimits guard against DoS via oversized numbers).
Related errors
- Failed parsing '${s}' as Date; at path ${path}
- Expecting number, got: ${jsonToken}; at path ${path}
- Invalid bitset value ${intValue}, expected 0 or 1; at path $
- Lossy conversion from ${intValue} to byte; at path ${path}
- Lossy conversion from ${intValue} to short; at path ${path}
AI-assisted analysis of google/gson@310ac341f2 (2026-08-10).
Data as JSON: /api/errors/1dc2de46477e40a4.
Report an issue: GitHub.