google/gson · error · IllegalArgumentException
Numeric values must be finite, but was " + value
Error message
Numeric values must be finite, but was " + value
What it means
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.
Source
Thrown at gson/src/main/java/com/google/gson/stream/JsonWriter.java:581
out.write(value ? "true" : "false");
return this;
}
/**
* Encodes {@code value}.
*
* @param value a finite value, or if {@link #setStrictness(Strictness) lenient}, also {@link
* Float#isNaN() NaN} or {@link Float#isInfinite() infinity}.
* @return this writer.
* @throws IllegalArgumentException if the value is NaN or Infinity and this writer is not {@link
* #setStrictness(Strictness) lenient}.
* @since 2.9.1
*/
@CanIgnoreReturnValue
public JsonWriter value(float value) throws IOException {
writeDeferredName();
if (strictness != Strictness.LENIENT && (Float.isNaN(value) || Float.isInfinite(value))) {
throw new IllegalArgumentException("Numeric values must be finite, but was " + value);
}
beforeValue();
out.append(Float.toString(value));
return this;
}
/**
* Encodes {@code value}.
*
* @param value a finite value, or if {@link #setStrictness(Strictness) lenient}, also {@link
* Double#isNaN() NaN} or {@link Double#isInfinite() infinity}.
* @return this writer.
* @throws IllegalArgumentException if the value is NaN or Infinity and this writer is not {@link
* #setStrictness(Strictness) lenient}.
*/
@CanIgnoreReturnValue
public JsonWriter value(double value) throws IOException {
writeDeferredName();View on GitHub (pinned to 8b8628c656)
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.
Example fix
// before
float ratio = total / count; // could be NaN or Infinity
writer.name("ratio").value(ratio); // throws
// after
if (Float.isFinite(ratio)) {
writer.name("ratio").value(ratio);
} else {
writer.name("ratio").nullValue();
} Defensive patterns
Strategy: validation
Validate before calling
if (Float.isFinite(value)) {
writer.value(value);
} else {
writer.nullValue(); // or a sentinel/default
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- JSON forbids NaN and infinities: {value}
- value + " is not a valid double value as per JSON specificat
- Invalid style: {style}
- String contains non-ASCII characters: {s}{location}
- Expecting number, got: " + jsonToken + "; at path " + in.get
AI-assisted analysis of google/gson@8b8628c656 (2026-08-04).
Data as JSON: /data/errors/3def861513ca8474.json.
Report an issue: GitHub.