{"record":{"id":"4ecb65b2a4551a1c","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/310ac341f2f92a454b229bf21f70d2d18b2b6db7/gson/src/main/java/com/google/gson/internal/bind/JsonTreeWriter.java#L173-L209","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before - throws when f is NaN or Infinity\ngson.toJson(myObj); // myObj has float f = Float.NaN\n\n// after - enable lenient mode\nGson gson = new GsonBuilder().setLenient().create();\ngson.toJson(myObj);","handlingStrategy":"validation","validationCode":"// Guard float before serializing unless lenient\nfloat f = value;\nif (!writer.isLenient() && (Float.isNaN(f) || Float.isInfinite(f))) {\n    // replace with null, a sentinel, or enable lenient mode\n    writer.nullValue();\n} else {\n    writer.value(f);\n}","typeGuard":null,"tryCatchPattern":"try {\n  writer.value(f);\n} catch (IllegalArgumentException e) {\n  // 'JSON forbids NaN and infinities': set writer.setLenient(true) or sanitize f first\n}","preventionTips":["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)"],"tags":["json","serialization","numeric","nan-infinity"],"backgroundTag":null,"analyzedSha":"310ac341f2f92a454b229bf21f70d2d18b2b6db7","analyzedAt":"2026-08-10T02:58:47.455Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}