{"id":"3def861513ca8474","repo":"google/gson","slug":"numeric-values-must-be-finite-but-was-value","errorCode":null,"errorMessage":"Numeric values must be finite, but was \" + value","messagePattern":"Numeric values must be finite, but was \" \\+ value","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/8b8628c65699bc4421696183c62ae0c1b9b281dc/gson/src/main/java/com/google/gson/stream/JsonWriter.java#L563-L599","documentation":"Thrown by JsonWriter.value(float) when the value is NaN or infinite and the writer's strictness is not LENIENT (default is LEGACY_STRICT). JSON per RFC 8259 has no representation for NaN or Infinity, so emitting them would produce invalid JSON that standard parsers reject.","triggerScenarios":"Calling writer.value(Float.NaN) or writer.value(Float.POSITIVE_INFINITY) while strictness is LEGACY_STRICT or STRICT. Common when serializing computed float metrics that divide by zero or come from non-finite math.","commonSituations":"Serializing analytics/aggregated metrics where division by zero yields Infinity; sensor data with NaN sentinels; numerical code that propagates NaN; strict consumers rejecting the output.","solutions":["Sanitize the value before writing: replace NaN/Infinity with null (nullValue()) or a default like 0.","Set writer.setStrictness(Strictness.LENIENT) to emit NaN/Infinity literals (only if consumers accept them).","Filter out non-finite values upstream in your data model.","Use BigDecimal for values that must never become non-finite."],"exampleFix":"// before\nfloat ratio = total / count; // could be NaN or Infinity\nwriter.name(\"ratio\").value(ratio); // throws\n\n// after\nif (Float.isFinite(ratio)) {\n  writer.name(\"ratio\").value(ratio);\n} else {\n  writer.name(\"ratio\").nullValue();\n}","handlingStrategy":"validation","validationCode":"if (Float.isFinite(value)) {\n  writer.value(value);\n} else {\n  writer.nullValue(); // or a sentinel/default\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Guard Float.isFinite(value) before calling value(float) under non-lenient strictness.","Sanitize non-finite values at the source (clamp to 0 or omit the field).","Set Strictness.LENIENT only if all consumers accept NaN/Infinity literals.","Use BigDecimal for numeric fields that must remain finite."],"tags":["gson","jsonwriter","numeric","nan","infinity","validation"],"analyzedSha":"8b8628c65699bc4421696183c62ae0c1b9b281dc","analyzedAt":"2026-08-04T19:12:22.202Z","schemaVersion":2}