Skip to content

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

@larshagencognite

Description

@larshagencognite
Contributor

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

Activity

self-assigned this
on Jun 23, 2025
added a commit that references this issue on Jun 26, 2025
bd6689d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

    Development

    Participants

    @sandwwraith@larshagencognite

    Issue actions

      Unexpected token for data object in a deep json structure gives ArrayIndexOutOfBoundsException · Issue #2994 · Kotlin/kotlinx.serialization