Allow JsonReader to tell if it peeked a long or double
See original GitHub issueProblem: I need to tell apart long from doubles when generically using a JsonReader
.
Example:
{ "amount" : "93.21948702", "count": 12 }
Today I need to know in advance that one is a double and the other an int/long so I can call nextDouble()
or nextLong()
. I want the reader to provide me enough information so that I can know which one to call or just give me the right object.
Solution 1: Add JSonReader#getPeeked()
and make PEEKED_LONG
and PEEKED_NUMBER
public.
This leave me to do:
if (reader.getPeeked() == JSonReader.PEEKED_LONG) {
return Long.valueOf(reader.nextLong());
}
return Double.valueOf(reader.nextDouble());
Solution 2: Add JsonReader#getNumber()
returns java.lang.Number
.
Which leaves me to do:
reader.nextNumber()
and I can deal with a Double
or Long
down the line.
I can propose a PR but I’d like to know if there are alternatives. Right now I am using, arg, reflection to get to JSonReader.peeked
, PEEKED_LONG
and PEEKED_NUMBER
.
Issue Analytics
- State:
- Created 4 years ago
- Comments:10 (4 by maintainers)
Top GitHub Comments
If I’m not mistaken and remember how Gson readers work, you can check whether the peeked token type indicates a number, and then read it as a string. This would allow any parsing strategy since JSON is not restricted only with longs and doubles.
Fixed by #1290.