{"record":{"id":"5635c080b1116bc7","repo":"google/gson","slug":"json-forbids-nan-and-infinities-d-at-path-i","errorCode":null,"errorMessage":"JSON forbids NaN and infinities: ${d}; at path ${in.getPreviousPath()}","messagePattern":"JSON forbids NaN and infinities: (.+?); at path (.+?)","errorType":"exception","errorClass":"MalformedJsonException","httpStatus":null,"severity":"error","filePath":"gson/src/main/java/com/google/gson/ToNumberPolicy.java","lineNumber":88,"sourceCode":"    @Override\n    public Number readNumber(JsonReader in) throws IOException, JsonParseException {\n      String value = in.nextString();\n      if (value.indexOf('.') >= 0) {\n        return parseAsDouble(value, in);\n      } 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();","sourceCodeStart":70,"sourceCodeEnd":106,"githubUrl":"https://github.com/google/gson/blob/310ac341f2f92a454b229bf21f70d2d18b2b6db7/gson/src/main/java/com/google/gson/ToNumberPolicy.java#L70-L106","documentation":"Thrown by the LONG_OR_DOUBLE ToNumberStrategy when a JSON numeric token parses to Double.NaN or Double.POSITIVE/NEGATIVE_INFINITY and the JsonReader is NOT in lenient mode. Standard JSON forbids NaN and infinities, so Gson rejects them in strict/legacy mode. The message includes the offending value and the JSON path.","triggerScenarios":"Deserializing numbers using ToNumberPolicy.LONG_OR_DOUBLE (set via GsonBuilder.setObjectToNumberStrategy or setNumberToNumberStrategy) where the JSON contains the literals NaN, Infinity, or -Infinity, and the reader strictness is not LENIENT.","commonSituations":"Non-JSON-compliant APIs or legacy systems that emit NaN/Infinity; switching the number strategy to LONG_OR_DOUBLE without checking source data; scientific/financial feeds using non-standard number tokens.","solutions":["Switch to ToNumberPolicy.LAZILY_PARSED_NUMBER or ToNumberPolicy.DOUBLE which handle special values without this guard","Set the JsonReader to Strictness.LENIENT if the source legitimately uses NaN/Infinity","Fix the data source to emit valid JSON numbers (null for NaN/Infinity, or string representations)"],"exampleFix":"// before\ngsonBuilder.setObjectToNumberStrategy(ToNumberPolicy.LONG_OR_DOUBLE);\n\n// after\ngsonBuilder.setObjectToNumberStrategy(ToNumberPolicy.LAZILY_PARSED_NUMBER);","handlingStrategy":"validation","validationCode":"// Check for NaN/Infinity literals before deserializing with LONG_OR_DOUBLE\nString raw = jsonNode.get(\"value\").getAsString();\nif (\"NaN\".equals(raw) || \"Infinity\".equals(raw) || \"-Infinity\".equals(raw)) {\n  // decide: coerce to null, use Double, or reject\n  return null;\n}","typeGuard":null,"tryCatchPattern":"try {\n  return gson.fromJson(json, type);\n} catch (MalformedJsonException e) {\n  if (e.getMessage().contains(\"forbids NaN\")) {\n    // switch to a lenient reader or LAZILY_PARSED_NUMBER strategy\n    return parseLenient(json, type);\n  }\n  throw e;\n}","preventionTips":["If your data source emits NaN/Infinity, use ToNumberPolicy.DOUBLE or LAZILY_PARSED_NUMBER instead of LONG_OR_DOUBLE","Set JsonReader strictness to LENIENT if non-standard number literals are expected","Validate number fields from untrusted sources before deserialization"],"tags":["number-parsing","json-strictness","nan-infinity","deserialization"],"backgroundTag":null,"analyzedSha":"310ac341f2f92a454b229bf21f70d2d18b2b6db7","analyzedAt":"2026-08-10T02:58:47.455Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}