Skip to content
This repository has been archived by the owner on Dec 3, 2020. It is now read-only.

Add nullable readers #26

Open
reitzig opened this issue Mar 31, 2017 · 1 comment
Open

Add nullable readers #26

reitzig opened this issue Mar 31, 2017 · 1 comment

Comments

@reitzig
Copy link
Contributor

reitzig commented Mar 31, 2017

When writing a reader, one has to step through the JSON stream entry by entry. Value accessors like nextInt() fail if they encounter NULL; dealing with JSON elements that can, in Kotlin terms, be Int? is cumbersome.

The workaround is quite easy:

fun JsonReader.nextIntOrNull(): Int? {
    if ( this.peek() != JsonToken.NULL ) {
        return this.nextInt()
    } else {
        this.nextNull()
        return null
    }
}

One could argue for this.peek() == JsonToken.NUMBER (where does the distinction between integers and floats happen, by the way?); that's a design decision between silent and loud parsing errors.

Please include nullable variants into JsonReader.

@reitzig
Copy link
Contributor Author

reitzig commented Mar 31, 2017

The same issue arises for nullable objects or arrays. Any ideas how we could implement it neatly for those two?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant