google/gson · error · IllegalArgumentException
${value} is not a valid double value as per JSON specificati
Error message
${value} is not a valid double value as per JSON specification. To override this behavior, use GsonBuilder.serializeSpecialFloatingPointValues() method. What it means
By default Gson uses a strict double/float adapter (DOUBLE_STRICT / FLOAT_STRICT) that rejects NaN and Infinity because they are not valid JSON values per the specification. The check fires during serialization when writing a double or float field. The error message tells you to call GsonBuilder.serializeSpecialFloatingPointValues() to switch to the lenient adapter.
Source
Thrown at gson/src/main/java/com/google/gson/internal/bind/TypeAdapters.java:521
}
@Override
public void write(JsonWriter out, Number value) throws IOException {
if (value == null) {
out.nullValue();
return;
}
double doubleValue = value.doubleValue();
if (strict) {
checkValidFloatingPoint(doubleValue);
}
out.value(doubleValue);
}
}
private static void checkValidFloatingPoint(double value) {
if (Double.isNaN(value) || Double.isInfinite(value)) {
throw new IllegalArgumentException(
value
+ " is not a valid double value as per JSON specification. To override this"
+ " behavior, use GsonBuilder.serializeSpecialFloatingPointValues() method.");
}
}
public static final TypeAdapter<Number> FLOAT = new FloatAdapter(false);
public static final TypeAdapter<Number> FLOAT_STRICT = new FloatAdapter(true);
public static final TypeAdapter<Number> DOUBLE = new DoubleAdapter(false);
public static final TypeAdapter<Number> DOUBLE_STRICT = new DoubleAdapter(true);
public static final TypeAdapter<Character> CHARACTER =
new TypeAdapter<Character>() {
@Override
public Character read(JsonReader in) throws IOException {
if (in.peek() == JsonToken.NULL) {
in.nextNull();View on GitHub (pinned to 310ac341f2)
Solutions
- Call new GsonBuilder().serializeSpecialFloatingPointValues().create() to allow NaN and Infinity in output
- Sanitize the data before serialization by replacing NaN/Infinity with null or a sentinel value
- Register a custom JsonSerializer for Double/Float that handles special values explicitly
Example fix
// before
Gson gson = new Gson();
gson.toJson(new Result(Double.NaN)); // throws IllegalArgumentException
// after
Gson gson = new GsonBuilder()
.serializeSpecialFloatingPointValues()
.create();
gson.toJson(new Result(Double.NaN)); // outputs "NaN" Defensive patterns
Strategy: validation
Validate before calling
// Check for NaN or Infinity before serializing
public static void replaceSpecialDoubles(Object obj) {
for (Field f : obj.getClass().getDeclaredFields()) {
if (f.getType() == double.class || f.getType() == Double.class) {
f.setAccessible(true);
Double v = (Double) f.get(obj);
if (v != null && (Double.isNaN(v) || Double.isInfinite(v))) {
f.set(obj, null); // or a sentinel value
}
}
}
} Try / catch
try {
String json = gson.toJson(obj);
} catch (IllegalArgumentException e) {
if (e.getMessage().contains("not a valid double value")) {
// rebuild Gson with serializeSpecialFloatingPointValues() or sanitize data
Gson lenient = new GsonBuilder().serializeSpecialFloatingPointValues().create();
json = lenient.toJson(obj);
}
} Prevention
- Call GsonBuilder.serializeSpecialFloatingPointValues() when your data may contain NaN or Infinity
- Sanitize computation results (division, aggregation) before serializing
- Unit-test serialization of objects with edge-case double/float values
When it happens
Trigger: Serializing an object with a double or float field whose value is Double.NaN, Double.POSITIVE_INFINITY, or Double.NEGATIVE_INFINITY using a default Gson instance.
Common situations: Computational results that produce NaN or Infinity (division by zero, indeterminate forms); sensor or measurement data with sentinel infinite values; porting from Jackson which allows these by default.
Related errors
- JSON forbids NaN and infinities: ${result}
- cannot serialize ${srcType.getName()}; did you forget to reg
- cannot serialize ${srcType.getName()} because it already def
- Cannot parse ${value}; at path ${in.getPreviousPath()}
- Deserialization is unsupported
AI-assisted analysis of google/gson@310ac341f2 (2026-08-10).
Data as JSON: /api/errors/00cfe9dcee2fa5ee.
Report an issue: GitHub.