{"id":"c4295fa87e513099","repo":"google/gson","slug":"json-forbids-nan-and-infinities-value","errorCode":null,"errorMessage":"JSON forbids NaN and infinities: {value}","messagePattern":"JSON forbids NaN and infinities: (.+?)","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"gson/src/main/java/com/google/gson/internal/bind/JsonTreeWriter.java","lineNumber":191,"sourceCode":"    put(new JsonPrimitive(value));\n    return this;\n  }\n\n  @CanIgnoreReturnValue\n  @Override\n  public JsonWriter value(Boolean value) throws IOException {\n    if (value == null) {\n      return nullValue();\n    }\n    put(new JsonPrimitive(value));\n    return this;\n  }\n\n  @CanIgnoreReturnValue\n  @Override\n  public JsonWriter value(float value) throws IOException {\n    if (!isLenient() && (Float.isNaN(value) || Float.isInfinite(value))) {\n      throw new IllegalArgumentException(\"JSON forbids NaN and infinities: \" + value);\n    }\n    put(new JsonPrimitive(value));\n    return this;\n  }\n\n  @CanIgnoreReturnValue\n  @Override\n  public JsonWriter value(double value) throws IOException {\n    if (!isLenient() && (Double.isNaN(value) || Double.isInfinite(value))) {\n      throw new IllegalArgumentException(\"JSON forbids NaN and infinities: \" + value);\n    }\n    put(new JsonPrimitive(value));\n    return this;\n  }\n\n  @CanIgnoreReturnValue\n  @Override\n  public JsonWriter value(long value) throws IOException {","sourceCodeStart":173,"sourceCodeEnd":209,"githubUrl":"https://github.com/google/gson/blob/8b8628c65699bc4421696183c62ae0c1b9b281dc/gson/src/main/java/com/google/gson/internal/bind/JsonTreeWriter.java#L173-L209","documentation":"Thrown by JsonTreeWriter.value(float) when the writer is not in lenient mode and the supplied float is NaN or infinite. JSON has no representation for these values (the grammar only permits finite numbers), so Gson refuses to emit them by default. The check uses Float.isNaN / Float.isInfinite and the original value appears in the message.","triggerScenarios":"Calling value(float) with Float.NaN, Float.POSITIVE_INFINITY, or Float.NEGATIVE_INFINITY while isLenient()==false (the Gson default). Common when a float field of a serialized object holds NaN from a division (0.0f/0.0f) or an unbounded computation.","commonSituations":"Serializing sensor/scientific data where NaN marks missing readings; graphics code that lets infinity leak into fields; math results not sanitized before serialization; tests that feed edge-case floats through Gson without lenient mode.","solutions":["Sanitize the float before serialization: replace NaN/Infinity with null (and serialize nulls off) or with a sentinel finite value.","Enable lenient mode on the writer (setLenient(true)) if the downstream consumer accepts NaN/Infinity tokens (non-standard JSON).","Register a custom TypeAdapter<Float> that converts NaN/Infinity to null or 0.","Fix the upstream computation so it never produces NaN/Infinity for fields that will be serialized."],"exampleFix":"// before\nfloat v = 0.0f / 0.0f; // NaN\nnew Gson().toJson(v); // throws IllegalArgumentException\n\n// after: sanitize, or use lenient\nfloat safe = Float.isNaN(v) || Float.isInfinite(v) ? 0f : v;\nnew Gson().toJson(safe);\n// or:\nJsonWriter w = ...; w.setLenient(true); w.value(v);","handlingStrategy":"validation","validationCode":"// Sanitize float before serialization\nfloat safe = (Float.isNaN(v) || Float.isInfinite(v)) ? 0f : v;\nout.value(safe);","typeGuard":null,"tryCatchPattern":"try {\n  out.value(v);\n} catch (IllegalArgumentException e) {\n  if (e.getMessage() != null && e.getMessage().startsWith(\"JSON forbids NaN\")) {\n    out.value(0f); // or out.nullValue()\n  } else throw e;\n}","preventionTips":["Guard float computations for NaN/Infinity before serialization.","If non-finite values are meaningful, use setLenient(true) and ensure the consumer accepts them.","Unit-test serialization with NaN and Infinity inputs."],"tags":["json","numeric","nan","infinity","gson","serialization"],"analyzedSha":"8b8628c65699bc4421696183c62ae0c1b9b281dc","analyzedAt":"2026-08-04T19:12:22.202Z","schemaVersion":2}