kotlinx.serialization icon indicating copy to clipboard operation
kotlinx.serialization copied to clipboard

decodeFromBufferedSource throws JsonDecodingException on big JSON

Open Artman12 opened this issue 3 years ago • 0 comments

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

Artman12 avatar Aug 06 '22 10:08 Artman12