{"record":{"id":"1dc2de46477e40a4","repo":"google/gson","slug":"failed-parsing-s-as-bigdecimal-at-path-pat","errorCode":null,"errorMessage":"Failed parsing '${s}' as BigDecimal; at path ${path}","messagePattern":"Failed parsing '(.+?)' as BigDecimal; at path (.+?)","errorType":"exception","errorClass":"JsonSyntaxException","httpStatus":null,"severity":"error","filePath":"gson/src/main/java/com/google/gson/internal/bind/TypeAdapters.java","lineNumber":593,"sourceCode":"        @Override\n        public void write(JsonWriter out, String value) throws IOException {\n          out.value(value);\n        }\n      };\n\n  public static final TypeAdapter<BigDecimal> BIG_DECIMAL =\n      new TypeAdapter<BigDecimal>() {\n        @Override\n        public BigDecimal read(JsonReader in) throws IOException {\n          if (in.peek() == JsonToken.NULL) {\n            in.nextNull();\n            return null;\n          }\n          String s = in.nextString();\n          try {\n            return NumberLimits.parseBigDecimal(s);\n          } catch (NumberFormatException e) {\n            throw new JsonSyntaxException(\n                \"Failed parsing '\" + s + \"' as BigDecimal; at path \" + in.getPreviousPath(), e);\n          }\n        }\n\n        @Override\n        public void write(JsonWriter out, BigDecimal value) throws IOException {\n          out.value(value);\n        }\n      };\n\n  public static final TypeAdapterFactory BIG_DECIMAL_FACTORY =\n      newFactory(BigDecimal.class, BIG_DECIMAL);\n\n  public static final TypeAdapter<BigInteger> BIG_INTEGER =\n      new TypeAdapter<BigInteger>() {\n        @Override\n        public BigInteger read(JsonReader in) throws IOException {\n          if (in.peek() == JsonToken.NULL) {","sourceCodeStart":575,"sourceCodeEnd":611,"githubUrl":"https://github.com/google/gson/blob/310ac341f2f92a454b229bf21f70d2d18b2b6db7/gson/src/main/java/com/google/gson/internal/bind/TypeAdapters.java#L575-L611","documentation":"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.","triggerScenarios":"Deserialifying a JSON value like \"abc\" or \"1.2.3\" or an excessively long number string into a BigDecimal field.","commonSituations":"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).","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"],"exampleFix":"// before\nclass Product { BigDecimal price; }\ngson.fromJson(\"{\\\"price\\\":\\\"N/A\\\"}\", Product.class); // throws\n\n// after\nclass Product { String price; // parse to BigDecimal after validation\n  BigDecimal priceValue() { return new BigDecimal(price); }\n}","handlingStrategy":"validation","validationCode":"// Validate that a string is a parseable BigDecimal before deserializing\npublic static boolean isValidBigDecimal(String s) {\n    try {\n        NumberLimits.parseBigDecimal(s);\n        return true;\n    } catch (NumberFormatException e) {\n        return false;\n    }\n}","typeGuard":null,"tryCatchPattern":"try {\n    Product p = gson.fromJson(json, Product.class);\n} catch (JsonSyntaxException e) {\n    if (e.getMessage().contains(\"Failed parsing\") && e.getMessage().contains(\"BigDecimal\")) {\n        // use a String field and parse manually with error handling\n    }\n}","preventionTips":["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"],"tags":["deserialization","bigdecimal","parsing","numeric"],"backgroundTag":null,"analyzedSha":"310ac341f2f92a454b229bf21f70d2d18b2b6db7","analyzedAt":"2026-08-10T02:58:47.455Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}