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

Unexpected token for data object in a deep json structure gives ArrayIndexOutOfBoundsException

Open larshagencognite opened this issue 8 months ago • 0 comments

Describe the bug

When the json parser encounters an unexpected token while parsing a data object deep in a json structure (>= 8 levels), it throws ArrayIndexOutOfBoundsException.

This seems to be related the size of the initial size of the indices array in JsonPath. At the start, all values are initialized to -1. When the array is later increased in size for a deep structure, the resize operation uses copyOf(newsize), which fills in the array with zeroes.

To Reproduce

package com.foo.bar

import kotlinx.serialization.Serializable
import kotlinx.serialization.SerializationException
import kotlinx.serialization.json.Json
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows

class TestDeepJsonFailure {
    @Test
    fun `test deep failure`() {
        val exception = assertThrows<SerializationException> {
            Json.decodeFromString<Foo>("""{"bar": {"bar": {"bar": {"bar": {"bar": {"bar": {"baz": false}}}}}}}""")
        }

        assertEquals(
            """
                Unexpected JSON token at offset 56: Expected start of the object '{', but had 'f' instead at path: ${'$'}.bar.bar.bar.bar.bar.bar.baz
                JSON input: {"bar": {"bar": {"bar": {"bar": {"bar": {"bar": {"baz": false}}}}}}}
            """.trimIndent(),
            exception.message
        )
    }

    @Test
    fun `test deeper failure`() {
        val exception = assertThrows<ArrayIndexOutOfBoundsException> {
            Json.decodeFromString<Foo>("""{"bar": {"bar": {"bar": {"bar": {"bar": {"bar": {"bar": {"baz": false}}}}}}}}""")
        }

        assertEquals(
            "Index 0 out of bounds for length 0",
            exception.message
        )
    }
}

@Serializable
data class Foo(val bar: Foo?, val baz: Baz?)

@Serializable
data object Baz

Expected behavior

The parser should fail with a similar error message as for less deep json structure.

Environment

  • Kotlin version: 2.1.10
  • Library version: 1.8.0
  • Kotlin platforms: JVM
  • Gradle version: N/A
  • IDE version N/A

larshagencognite avatar Apr 22 '25 13:04 larshagencognite