{"record":{"id":"3b5cb5b8638bcda7","repo":"google/gson","slug":"numeric-values-must-be-finite-but-was","errorCode":null,"errorMessage":"Numeric values must be finite, but was {}","messagePattern":"Numeric values must be finite, but was (.+?)","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"gson/src/main/java/com/google/gson/stream/JsonWriter.java","lineNumber":581,"sourceCode":"    out.write(value ? \"true\" : \"false\");\n    return this;\n  }\n\n  /**\n   * Encodes {@code value}.\n   *\n   * @param value a finite value, or if {@link #setStrictness(Strictness) lenient}, also {@link\n   *     Float#isNaN() NaN} or {@link Float#isInfinite() infinity}.\n   * @return this writer.\n   * @throws IllegalArgumentException if the value is NaN or Infinity and this writer is not {@link\n   *     #setStrictness(Strictness) lenient}.\n   * @since 2.9.1\n   */\n  @CanIgnoreReturnValue\n  public JsonWriter value(float value) throws IOException {\n    writeDeferredName();\n    if (strictness != Strictness.LENIENT && (Float.isNaN(value) || Float.isInfinite(value))) {\n      throw new IllegalArgumentException(\"Numeric values must be finite, but was \" + value);\n    }\n    beforeValue();\n    out.append(Float.toString(value));\n    return this;\n  }\n\n  /**\n   * Encodes {@code value}.\n   *\n   * @param value a finite value, or if {@link #setStrictness(Strictness) lenient}, also {@link\n   *     Double#isNaN() NaN} or {@link Double#isInfinite() infinity}.\n   * @return this writer.\n   * @throws IllegalArgumentException if the value is NaN or Infinity and this writer is not {@link\n   *     #setStrictness(Strictness) lenient}.\n   */\n  @CanIgnoreReturnValue\n  public JsonWriter value(double value) throws IOException {\n    writeDeferredName();","sourceCodeStart":563,"sourceCodeEnd":599,"githubUrl":"https://github.com/google/gson/blob/310ac341f2f92a454b229bf21f70d2d18b2b6db7/gson/src/main/java/com/google/gson/stream/JsonWriter.java#L563-L599","documentation":"Thrown by JsonWriter.value(float) (JsonWriter.java:578-582) when strictness != LENIENT and the float is NaN or Infinite. RFC 8259 forbids NaN and infinities in JSON, so in the default LEGACY_STRICT (or STRICT) mode the writer refuses to emit them. The message echoes the offending value.","triggerScenarios":"Calling writer.value(Float.NaN) or writer.value(Float.POSITIVE_INFINITY) without enabling lenient mode; computations that produce NaN/Infinity (division by zero, undefined math) being written directly.","commonSituations":"Aggregations or statistical fields that can be NaN when no data exists; floating-point division without guards; migrating data that previously tolerated special values; serializers that pass floats straight through.","solutions":["Guard the value: if (Float.isFinite(v)) writer.value(v) else writer.nullValue() (or omit the field).","If you genuinely need to emit NaN/Infinity, set writer.setStrictness(Strictness.LENIENT).","Sanitize inputs upstream so NaN/Infinity never reaches the writer.","Log when a value is coalesced to null for diagnostics."],"exampleFix":"// before\nwriter.value(ratio); // throws when ratio is NaN\n\n// after\nif (Float.isFinite(ratio)) {\n  writer.value(ratio);\n} else {\n  writer.nullValue();\n}","handlingStrategy":"validation","validationCode":"if (Float.isFinite(v)) {\n  writer.value(v);\n} else {\n  writer.nullValue(); // or omit the field entirely\n}","typeGuard":"static boolean isEmittableFloat(float v) {\n  return Float.isFinite(v);\n}","tryCatchPattern":"try {\n  writer.value(v);\n} catch (IllegalArgumentException e) {\n  writer.nullValue();\n}","preventionTips":["Guard all float outputs with Float.isFinite before writing.","Sanitize computations that can yield NaN/Infinity (averages, ratios).","Enable Strictness.LENIENT only if non-finite emission is intentional."],"tags":["java","gson","jsonwriter","numeric","validation"],"backgroundTag":null,"analyzedSha":"310ac341f2f92a454b229bf21f70d2d18b2b6db7","analyzedAt":"2026-08-10T02:58:47.455Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}