google/gson · error · IllegalArgumentException
JSON forbids NaN and infinities
Error message
JSON forbids NaN and infinities: ${value} What it means
Standard JSON has no representation for NaN, +Infinity, or -Infinity. Unless the writer is in lenient mode, JsonTreeWriter.value(float) rejects Float.NaN, Float.POSITIVE_INFINITY, and Float.NEGATIVE_INFINITY with IllegalArgumentException. This prevents emitting JSON that other parsers would reject as malformed.
Solutions
- Enable lenient mode: new GsonBuilder().setLenient().create(), or writer.setLenient(true)
- Sanitize NaN/Infinity to null or a sentinel value before serialization
- Register a custom TypeAdapter<Float> that converts NaN/Infinity to null
Example fix
// before - throws when f is NaN or Infinity gson.toJson(myObj); // myObj has float f = Float.NaN // after - enable lenient mode Gson gson = new GsonBuilder().setLenient().create(); gson.toJson(myObj);
Defensive patterns
Strategy: validation
Validate before calling
// Guard float before serializing unless lenient
float f = value;
if (!writer.isLenient() && (Float.isNaN(f) || Float.isInfinite(f))) {
// replace with null, a sentinel, or enable lenient mode
writer.nullValue();
} else {
writer.value(f);
} Try / catch
try {
writer.value(f);
} catch (IllegalArgumentException e) {
// 'JSON forbids NaN and infinities': set writer.setLenient(true) or sanitize f first
} Prevention
- Validate floats with Float.isFinite() before serializing in strict mode
- Enable setLenient(true) if non-finite values are legitimate in your domain
- Unit-test serialization of boundary values (0.0/0.0, 1.0/0.0)
When it happens
Trigger: Serializing a float field whose runtime value is NaN or Infinity via a non-lenient Gson/JsonTreeWriter; computing a float from division by zero or undefined arithmetic and serializing it.
Common situations: Scientific/financial computations producing Infinity (division by zero) or NaN (0.0/0.0); sensor data with missing readings encoded as NaN; migrating from lenient to strict mode.
Related errors
- Did not expect a name
- Expected one JSON element but was
- Expecting number, got
- Incomplete document
- Please begin an object before writing a name.
AI-assisted analysis of google/gson@310ac341f2 (2026-08-10).
Data as JSON: /api/errors/4ecb65b2a4551a1c.
Report an issue: GitHub.
Appendix: source
Thrown at gson/src/main/java/com/google/gson/internal/bind/JsonTreeWriter.java:191
put(new JsonPrimitive(value));
return this;
}
@CanIgnoreReturnValue
@Override
public JsonWriter value(Boolean value) throws IOException {
if (value == null) {
return nullValue();
}
put(new JsonPrimitive(value));
return this;
}
@CanIgnoreReturnValue
@Override
public JsonWriter value(float value) throws IOException {
if (!isLenient() && (Float.isNaN(value) || Float.isInfinite(value))) {
throw new IllegalArgumentException("JSON forbids NaN and infinities: " + value);
}
put(new JsonPrimitive(value));
return this;
}
@CanIgnoreReturnValue
@Override
public JsonWriter value(double value) throws IOException {
if (!isLenient() && (Double.isNaN(value) || Double.isInfinite(value))) {
throw new IllegalArgumentException("JSON forbids NaN and infinities: " + value);
}
put(new JsonPrimitive(value));
return this;
}
@CanIgnoreReturnValue
@Override
public JsonWriter value(long value) throws IOException {View on GitHub (pinned to 310ac341f2)