Skip to content

decodeFromBufferedSource throws JsonDecodingException on big JSON #2006

@Artman12

Description

@Artman12

Describe the bug
Trying to use new Okio streams, introduced recently and encountering JsonDecodingException when trying to decode big JSON via decodeFromBufferedSource().

decodeFromString() works fine.

To Reproduce

@Serializable
data class Node(
    val children: List<Node>?
)

fun test() {
        fun getNodes(count: Int, depth: Int) : List<Node> {
            val ret = mutableListOf<Node>()
            if (depth == 0) return ret
            for (i in 0 until count) {
                ret.add(Node(getNodes(1, depth - 1)))
            }
            return ret
        }

        val rootNode = Node(getNodes(1000, 10))

        val json = Serializer.json.encodeToString(rootNode)

        val buffer = okio.Buffer().apply {
            write(json.toByteArray())
        }

//        val decodedNode = Serializer.json.decodeFromString<Node>(json) // works fine
        val decodedNode = Serializer.json.decodeFromBufferedSource<Node>(buffer) // throws JsonDecodingException 
    }

Exception error:

kotlinx.serialization.json.internal.JsonDecodingException: Expected colon ':', but had 'EOF' instead at path: $.children[107].children[0].children[108]
    JSON input: .....hildren":[{"children":[{"children":[{"children":[{"children"

I've created github repository with reproducible example: https://github.com/Artman12/OkioTest

Expected behavior
decodeFromBufferedSource decodes JSON correctly.

Environment

  • Kotlin version: 1.7.10
  • Library version: 1.4.0-RC
  • Kotlin platforms: JVM, Native

Metadata

Metadata

Assignees

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