power7714 / google-gson

Automatically exported from code.google.com/p/google-gson
0 stars 0 forks source link

Serialization of NaN/Inf with complex map key serialization and special floating point values enabled is broken #639

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?

1. Run the following code snippet:

        Map map = new HashMap();
        map.put(Double.NaN, "x");
        System.out.println(new GsonBuilder()
                .enableComplexMapKeySerialization()
                .serializeSpecialFloatingPointValues()
                .create()
                .toJson(map));

What is the expected output? What do you see instead?

Expected:

{"NaN":"x"}

Actual:

Exception in thread "main" java.lang.IllegalArgumentException: JSON forbids NaN 
and infinities: NaN
    at com.google.gson.internal.bind.JsonTreeWriter.value(JsonTreeWriter.java:183)
    at com.google.gson.internal.bind.TypeAdapters$10.write(TypeAdapters.java:298)
    at com.google.gson.internal.bind.TypeAdapters$10.write(TypeAdapters.java:287)
    at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
    at com.google.gson.TypeAdapter.toJsonTree(TypeAdapter.java:230)
    at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.write(MapTypeAdapterFactory.java:219)
    at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.write(MapTypeAdapterFactory.java:145)
    at com.google.gson.Gson.toJson(Gson.java:595)
    at com.google.gson.Gson.toJson(Gson.java:574)
    at com.google.gson.Gson.toJson(Gson.java:529)
    at com.google.gson.Gson.toJson(Gson.java:509)

What version of the product are you using? On what operating system?
- Gson 2.3.1
- Windows 7 Enterprise, Servie Pack 1

Please provide any additional information below.

Looks like the complex map key serialization doesn't forward the lenient flag 
from the JsonWriter to the JsonTreeWriter, hence the error.

Original issue reported on code.google.com by kaspar.thommen@gmail.com on 13 May 2015 at 1:35

GoogleCodeExporter commented 9 years ago
Correction: The above stack trace is for 2.3. The stack trace for 2.3.1 is:

Exception in thread "main" java.lang.IllegalArgumentException: JSON forbids NaN 
and infinities: NaN
    at com.google.gson.internal.bind.JsonTreeWriter.value(JsonTreeWriter.java:183)
    at com.google.gson.internal.bind.TypeAdapters$10.write(TypeAdapters.java:298)
    at com.google.gson.internal.bind.TypeAdapters$10.write(TypeAdapters.java:287)
    at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
    at com.google.gson.TypeAdapter.toJsonTree(TypeAdapter.java:230)
    at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.write(MapTypeAdapterFactory.java:219)
    at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.write(MapTypeAdapterFactory.java:145)
    at com.google.gson.Gson.toJson(Gson.java:600)
    at com.google.gson.Gson.toJson(Gson.java:579)
    at com.google.gson.Gson.toJson(Gson.java:534)
    at com.google.gson.Gson.toJson(Gson.java:514)

Original comment by kaspar.thommen@gmail.com on 13 May 2015 at 1:38