{"id":"018057602e2cf06b","repo":"google/gson","slug":"failed-parsing-s-as-biginteger-at-path","errorCode":null,"errorMessage":"Failed parsing '\" + s + \"' as BigInteger; at path \" + in.getPreviousPath()","messagePattern":"Failed parsing '\" \\+ s \\+ \"' as BigInteger; at path \" \\+ in\\.getPreviousPath\\(\\)","errorType":"exception","errorClass":"JsonSyntaxException","httpStatus":null,"severity":"error","filePath":"gson/src/main/java/com/google/gson/internal/bind/TypeAdapters.java","lineNumber":617,"sourceCode":"        }\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) {\n            in.nextNull();\n            return null;\n          }\n          String s = in.nextString();\n          try {\n            return NumberLimits.parseBigInteger(s);\n          } catch (NumberFormatException e) {\n            throw new JsonSyntaxException(\n                \"Failed parsing '\" + s + \"' as BigInteger; at path \" + in.getPreviousPath(), e);\n          }\n        }\n\n        @Override\n        public void write(JsonWriter out, BigInteger value) throws IOException {\n          out.value(value);\n        }\n      };\n\n  public static final TypeAdapterFactory BIG_INTEGER_FACTORY =\n      newFactory(BigInteger.class, BIG_INTEGER);\n\n  public static final TypeAdapter<LazilyParsedNumber> LAZILY_PARSED_NUMBER =\n      new TypeAdapter<LazilyParsedNumber>() {\n        // Normally users should not be able to access and deserialize LazilyParsedNumber because\n        // it is an internal type, but implement this nonetheless in case there are legit corner\n        // cases where this is possible","sourceCodeStart":599,"sourceCodeEnd":635,"githubUrl":"https://github.com/google/gson/blob/8b8628c65699bc4421696183c62ae0c1b9b281dc/gson/src/main/java/com/google/gson/internal/bind/TypeAdapters.java#L599-L635","documentation":"Gson's BigInteger TypeAdapter reads the JSON token as a string and calls NumberLimits.parseBigInteger. Any value that is not a valid integer literal (decimal digits only, optional leading minus) throws NumberFormatException, which Gson wraps as JsonSyntaxException with the source string and JSON path.","triggerScenarios":"Deserializing into a BigInteger field where the JSON value contains a decimal point, exponent, non-digit characters, thousands separators, or is empty.","commonSituations":"Storing IDs as BigInteger but the producer emits them quoted with formatting, scientific notation from another serializer, decimal values accidentally mapped to an integer field, or empty/null-as-string from a flaky upstream.","solutions":["Fix the JSON to emit a plain integer literal (digits only, optional leading minus).","Strip formatting characters (commas, spaces, '+') from the source string before deserialization.","Register a custom TypeAdapter<BigInteger> that cleans or coerces the input.","If the value legitimately has a fractional part, change the target type to BigDecimal."],"exampleFix":"// before\nBigInteger id = gson.fromJson(\"\\\"1,234,567\\\"\", BigInteger.class);\n\n// after\nGson gson = new GsonBuilder()\n    .registerTypeHierarchyAdapter(BigInteger.class, new JsonDeserializer<BigInteger>() {\n        @Override public BigInteger deserialize(JsonElement j, Type t, JsonDeserializationContext c) {\n            String s = j.getAsString().replaceAll(\"[^0-9-]\", \"\");\n            return new BigInteger(s);\n        }\n    }).create();","handlingStrategy":"validation","validationCode":"boolean isParsableBigInteger(String s) {\n  if (s == null || s.isEmpty()) return false;\n  try { new BigInteger(s); return true; } catch (NumberFormatException e) { return false; }\n}","typeGuard":"static boolean isBigIntegerString(String s) {\n  return s != null && s.matches(\"[+-]?\\\\d+\");\n}","tryCatchPattern":"try {\n  BigInteger v = gson.fromJson(json, BigInteger.class);\n} catch (JsonSyntaxException e) {\n  // record path, fall back to null or sanitize and retry\n}","preventionTips":["Reject/sanitize strings containing separators or decimals before parsing as BigInteger.","Document that BigInteger fields must be plain integer literals in the data contract.","Add unit tests with locale-formatted inputs to catch regressions early."],"tags":["gson","deserialization","big-integer","json","number-parsing"],"analyzedSha":"8b8628c65699bc4421696183c62ae0c1b9b281dc","analyzedAt":"2026-08-04T19:12:22.202Z","schemaVersion":2}