question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow JsonReader to tell if it peeked a long or double

See original GitHub issue

Problem: 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:closed
  • Created 4 years ago
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
lyubomyr-shaydarivcommented, Nov 26, 2019

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.

0reactions
eamonnmcmanuscommented, Oct 9, 2021

Fixed by #1290.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Check if type of next token is double, long or int without using ...
You can read next number as a String using nextString() and JsonReader will return it without failing. Then, you can choose between long...
Read more >
com.google.gson.stream.JsonReader.nextDouble java code ...
Consumes the next token from the JSON stream and asserts that it is the end of the current object. nextName. Returns the next...
Read more >
core/java/android/util/JsonReader.java - Google Git
* #advance}. If null, peek() will assign a value. * and length in the buffer. * Creates a new instance that reads a...
Read more >
JsonReader (Gson 2.8.0 API) - Javadoc.io
If a value may be null, you should first check using peek() . ... long id = -1; String text = null; User...
Read more >
com.google.gson.stream.JsonReader - Java Source Code
<p>If a value may be null, you should first check using {@link #peek()}. ... public List<Double> readDoublesArray(JsonReader reader) throws IOException {.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found