google/gson · error · IllegalArgumentException

value + " is not a valid double value as per JSON specificat

Error message

value + " is not a valid double value as per JSON specification. To override this behavior, use GsonBuilder.serializeSpecialFloatingPointValues() method."

What it means

checkValidFloatingPoint throws IllegalArgumentException when a double or float being serialized is NaN or +/- Infinity, because the JSON specification does not permit these tokens. The default FLOAT/DOUBLE adapters are 'strict' and call this check on write; to emit non-finite values you must opt in via GsonBuilder.serializeSpecialFloatingPointValues().

Source

Thrown at gson/src/main/java/com/google/gson/internal/bind/TypeAdapters.java:519

    }

    @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 8b8628c656)

Solutions

  1. Enable non-finite serialization: new GsonBuilder().serializeSpecialFloatingPointValues().create().
  2. Sanitize the value before serialization (replace NaN/Infinity with null or a sentinel).
  3. Register a custom TypeAdapter<Double> that writes NaN/Infinity as a string token.
  4. Guard computations that can produce NaN/Infinity at the source.

Example fix

// before
Gson gson = new Gson();
gson.toJson(new Stats(Double.POSITIVE_INFINITY, Double.NaN)); // throws

// after
Gson gson = new GsonBuilder()
    .serializeSpecialFloatingPointValues()
    .create();
gson.toJson(new Stats(Double.POSITIVE_INFINITY, Double.NaN));
Defensive patterns

Strategy: validation

Validate before calling

// Reject NaN/Infinity before serializing, or decide to allow them
for (Field f : Stats.class.getDeclaredFields()) {
  if ((f.getType()==double.class||f.getType()==Double.class) && !gsonAllowsSpecial()) {
    double d = f.getDouble(obj);
    if (Double.isNaN(d)||Double.isInfinite(d)) throw new IllegalStateException("Non-finite: "+f);
  }
}

Type guard

// Guard values before they reach Gson
static boolean isSerializableDouble(double d, boolean allowSpecial) {
  return allowSpecial || (!Double.isNaN(d) && !Double.isInfinite(d));
}

Try / catch

try {
  gson.toJson(stats);
} catch (IllegalArgumentException e) {
  if (e.getMessage().contains("is not a valid double value as per JSON specification")) {
    // rebuild gson with serializeSpecialFloatingPointValues, or sanitize
    gson = new GsonBuilder().serializeSpecialFloatingPointValues().create();
  } else throw e;
}

Prevention

When it happens

Trigger: Serializing an object whose double/float field holds NaN or Infinity (e.g., result of 0.0/0.0, 1.0/0.0, overflow) using the default Gson FLOAT_STRICT/DOUBLE_STRICT adapter. Triggered at line 519 inside the adapter write path.

Common situations: Math/financial computations producing NaN/Infinity; division-by-zero results; sensor/statistical aggregates; unit tests with degenerate values; migrating from Jackson which may emit these as strings by default.

Related errors


AI-assisted analysis of google/gson@8b8628c656 (2026-08-04). Data as JSON: /data/errors/a8878177b85c9e5e.json. Report an issue: GitHub.