Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When using iterator from decodeToSequence, iterator.hasNext() throws when read all items and more #2267

Closed
MihaMarkic opened this issue Apr 11, 2023 · 1 comment
Assignees

Comments

@MihaMarkic
Copy link

When using iterator created from Json.decodeToSequence method and reading past all items, iterator.hasNext() will throw.
See the repro case where there are two elements in the list, while code tries to read 3 of them.

To Reproduce

val source = "[1, 2]"
val input = source.byteInputStream()
input.use {
    val iterator = Json.decodeToSequence<Int>(it).iterator()
    val values = iterator.asSequence().take(3).toList()
    var b = iterator.hasNext()  // throws
}

Exception is something like: Exception in thread "main" kotlinx.serialization.json.internal.JsonDecodingException: Expected end of the array ']', but had 'EOF' instead at path: $

Expected result

iterator.hasNext() returns false.

Environment

  • Kotlin version: 1.8.0
  • Library version: 2.2.4
  • Kotlin platforms: JVM
  • Gradle version: 7.4.2
@sandwwraith sandwwraith self-assigned this Apr 11, 2023
sandwwraith added a commit that referenced this issue Apr 11, 2023
…rapped inputs:

hasNext() shouldn't throw exception if it was called more than once
after the stream has ended.

Fixes #2267
@MihaMarkic
Copy link
Author

FYI the library version is 1.5.0 of course, not 2.2.4 as I wrote. Nice to see such a quick fix!

xBaank pushed a commit to xBaank/kotlinx.serialization that referenced this issue Apr 20, 2023
…rapped inputs: (Kotlin#2268)

hasNext() shouldn't throw exception if it was called more than once
after the stream has ended.

Fixes Kotlin#2267
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants