{"record":{"id":"00cfe9dcee2fa5ee","repo":"google/gson","slug":"value-is-not-a-valid-double-value-as-per-json-s","errorCode":null,"errorMessage":"${value} is not a valid double value as per JSON specification. To override this behavior, use GsonBuilder.serializeSpecialFloatingPointValues() method.","messagePattern":"(.+?) is not a valid double value as per JSON specification\\. To override this behavior, use GsonBuilder\\.serializeSpecialFloatingPointValues\\(\\) method\\.","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"gson/src/main/java/com/google/gson/internal/bind/TypeAdapters.java","lineNumber":521,"sourceCode":"    }\n\n    @Override\n    public void write(JsonWriter out, Number value) throws IOException {\n      if (value == null) {\n        out.nullValue();\n        return;\n      }\n      double doubleValue = value.doubleValue();\n      if (strict) {\n        checkValidFloatingPoint(doubleValue);\n      }\n      out.value(doubleValue);\n    }\n  }\n\n  private static void checkValidFloatingPoint(double value) {\n    if (Double.isNaN(value) || Double.isInfinite(value)) {\n      throw new IllegalArgumentException(\n          value\n              + \" is not a valid double value as per JSON specification. To override this\"\n              + \" behavior, use GsonBuilder.serializeSpecialFloatingPointValues() method.\");\n    }\n  }\n\n  public static final TypeAdapter<Number> FLOAT = new FloatAdapter(false);\n  public static final TypeAdapter<Number> FLOAT_STRICT = new FloatAdapter(true);\n\n  public static final TypeAdapter<Number> DOUBLE = new DoubleAdapter(false);\n  public static final TypeAdapter<Number> DOUBLE_STRICT = new DoubleAdapter(true);\n\n  public static final TypeAdapter<Character> CHARACTER =\n      new TypeAdapter<Character>() {\n        @Override\n        public Character read(JsonReader in) throws IOException {\n          if (in.peek() == JsonToken.NULL) {\n            in.nextNull();","sourceCodeStart":503,"sourceCodeEnd":539,"githubUrl":"https://github.com/google/gson/blob/310ac341f2f92a454b229bf21f70d2d18b2b6db7/gson/src/main/java/com/google/gson/internal/bind/TypeAdapters.java#L503-L539","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\nGson gson = new Gson();\ngson.toJson(new Result(Double.NaN)); // throws IllegalArgumentException\n\n// after\nGson gson = new GsonBuilder()\n    .serializeSpecialFloatingPointValues()\n    .create();\ngson.toJson(new Result(Double.NaN)); // outputs \"NaN\"","handlingStrategy":"validation","validationCode":"// Check for NaN or Infinity before serializing\npublic static void replaceSpecialDoubles(Object obj) {\n    for (Field f : obj.getClass().getDeclaredFields()) {\n        if (f.getType() == double.class || f.getType() == Double.class) {\n            f.setAccessible(true);\n            Double v = (Double) f.get(obj);\n            if (v != null && (Double.isNaN(v) || Double.isInfinite(v))) {\n                f.set(obj, null); // or a sentinel value\n            }\n        }\n    }\n}","typeGuard":null,"tryCatchPattern":"try {\n    String json = gson.toJson(obj);\n} catch (IllegalArgumentException e) {\n    if (e.getMessage().contains(\"not a valid double value\")) {\n        // rebuild Gson with serializeSpecialFloatingPointValues() or sanitize data\n        Gson lenient = new GsonBuilder().serializeSpecialFloatingPointValues().create();\n        json = lenient.toJson(obj);\n    }\n}","preventionTips":["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"],"tags":["serialization","double","float","nan","infinity"],"backgroundTag":null,"analyzedSha":"310ac341f2f92a454b229bf21f70d2d18b2b6db7","analyzedAt":"2026-08-10T02:58:47.455Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}