Skip to content

Allow JsonReader to tell if it peeked a long or double #1621

@garydgregory

Description

@garydgregory

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions