I am using Gson, I encountered the problem of serializing an object:
java.lang.IllegalArgumentException: NaN is not a valid double value as per JSON specification. To override this behavior, use GsonBuilder.serializeSpecialFloatingPointValues() method. at com.google.gson.Gson.checkValidFloatingPoint(Gson.java:296) ...
Is there a way for Gson to print the class name / field name it encounters? I can use the serializeSpecialFloatingPointValues() method as suggested, but ideally I would like to understand where my objects have NaN.
-------- Update ----------------
After carefully overcoming this, I believe that the cause is an uninitialized double. My setup looks like this:
public class Foo { private double price; } String jsonFromNet = ...; Foo foo = Gson.fromJson(jsonFromNet); Gson.toJson(foo, Foo.class); <-- throws the exception
Returning json from my api does not include the βpriceβ attribute, so I think the member variable βdβ remains uninitialized. When I proceed to serialize it, gson throws an error.
If I give the "price" an explicit value before serialization or, as it turned out, in the api json answer, everything works fine.
Also, if I change the "price" from double to float, this does not look like an uninitialized state.
Now I will look at GsonBuilder.serializeSpecialFloatingPointValues ββ(), I'm just wondering how dual serializes in this state. I would be fine with the default value of zero or something.
thanks
source share