Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid JSON from Double.NaN as JsonObject property #1203

Open
danorton-cubic-austin opened this issue Dec 11, 2017 · 4 comments
Open

Invalid JSON from Double.NaN as JsonObject property #1203

danorton-cubic-austin opened this issue Dec 11, 2017 · 4 comments

Comments

@danorton-cubic-austin
Copy link

danorton-cubic-austin commented Dec 11, 2017

Double.NaN as JsonObject property generates invalid JSON and does not throw exception
This is with 2.8.2
Code fragment:

    final Gson gson = new GsonBuilder().serializeNulls().create();
    JsonObject jsonObject = new JsonObject();
    jsonObject.addProperty("invalidDouble", Double.NaN);
    JsonElement jsonElement = jsonObject;
    log("As property within object: " + jsonElement.toString()); // expect this to throw exception
    jsonElement = new JsonPrimitive(Double.NaN);
    log("As primitive: " + jsonElement.toString());  // expect this to throw exception
    jsonElement = gson.toJsonTree(Double.NaN);
    log("As raw value toJsonTree: " + jsonElement.toString());  // this throws exception, as expected

Output:

As property within object: {"invalidDouble":NaN}
As primitive: NaN
Exception in thread "main" 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:324)
	at com.google.gson.Gson$1.write(Gson.java:292)
	at com.google.gson.Gson$1.write(Gson.java:278)
	at com.google.gson.Gson.toJson(Gson.java:669)
	at com.google.gson.Gson.toJsonTree(Gson.java:562)
	at com.google.gson.Gson.toJsonTree(Gson.java:541)
@danorton-cubic-austin danorton-cubic-austin changed the title Double.NaN as JsonObject property generates invalid JSON and does not throw exception Invalid JSON from Double.NaN as JsonObject property Dec 11, 2017
@danorton-cubic-austin
Copy link
Author

danorton-cubic-austin commented Dec 11, 2017

Related:

@danorton-cubic-austin
Copy link
Author

danorton-cubic-austin commented Dec 11, 2017

Is this a result of this PR?

@danorton-cubic-austin
Copy link
Author

See also:

@Marcono1234
Copy link
Collaborator

The problem here is that the non-finite floating point value check, which is affected by GsonBuilder.serializeSpecialFloatingPointValues(), only occurs for Float and Double (and the corresponding primitive types). However, any other adapter which directly uses JsonWriter.value(Number) (such as the adapter for JsonElement) is not influenced by this, and is only affected by the lenientness of the JsonWriter. And as mentioned in #372 (and related issues) that writer is unfortunately lenient by default.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants