google / gson

A Java serialization/deserialization library to convert Java Objects into JSON and back
Apache License 2.0
23.23k stars 4.27k forks source link

LazilyParsedNumber parsing of integers #996

Open avzuquete opened 7 years ago

avzuquete commented 7 years ago

I want to process a vector of integers. For that, I get each JsonElement from the JsonArray and use getAsInt() to obtain its integer value.

However, if the value is, say, 6.7, it does not generate an exception, it returns 6 (this was a real bug in a configuration file, where 6,7 was by mistake written as 6.7).

This happens because the getInt() method of LazilyParsedNumber uses BigDecimal to parse the text and getInt() to get some integer value ou of it. Shouldn't it use BigInteger instead of BigDecimal?

Marcono1234 commented 1 year ago

This might be the same as or closely related to #630, and might be fixed by #2031.

However, if you already know that you are deserializing a vector of integers, then why don't you deserialize it directly as int[]? This will most likely be more efficient and will also not be affected by this issue as far as I know.