Skip to content

Inconsistency JsonReader vs JsonTreeReader about double precision loss #2817

@d-william

Description

@d-william

Relates #1737

Gson version

2.12.1

Description

JsonReader and JsonTreeReader do not have the same behavior while reading a long (or an int) when the next token is a double.
JsonReader will fail.
JsonTreeReader will cast, it should fail also.

String json1 = "{\"value\": 42.123}";
JsonReader reader1 = new JsonReader(new StringReader(json1));
reader1.beginObject();
reader1.nextName();
int result1 = reader1.nextInt(); // fails
System.out.println("Result 1 : " + result1);

vs

JsonObject json2 = new JsonObject();
json2.addProperty("value", 42.123);
JsonTreeReader reader2 = new JsonTreeReader(json2);
reader2.beginObject();
reader2.nextName();
int result2 = reader2.nextInt(); // cast
System.out.println("Result 2 : " + result2); // -> 42

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions