Skip to content

Array iterator is serialized as a bean #604

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

Closed
thecam opened this issue Nov 8, 2022 · 1 comment
Closed

Array iterator is serialized as a bean #604

thecam opened this issue Nov 8, 2022 · 1 comment
Labels

Comments

@thecam
Copy link

thecam commented Nov 8, 2022

Describe the bug
Kotlin array iterators are serialized as beans, unlike Java iterators which as serialized as arrays, using the IteratorSerializer.

To Reproduce
main.kt

import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper

val kotlinArray = arrayOf(1, 2, 3)

fun main() {
    val mapper = jacksonObjectMapper()

    println("kotlin array: ${mapper.writeValueAsString(kotlinArray)}")
    println("kotlin array iterator: ${mapper.writeValueAsString(kotlinArray.iterator())}")
    println()
    println("java array: ${mapper.writeValueAsString(Java.array)}")
    println("java array iterator: ${mapper.writeValueAsString(Java.array.iterator())}")
}

Java.java

public class Java {
    public static int[] array = {1, 2, 3};
}

Output:

kotlin array: [1,2,3]
kotlin array iterator: {"array":[1,2,3]}

java array: [1,2,3]
java array iterator: [1,2,3]

Expected behavior
I expected the JSON for Kotlin and Java array iterators to be consistent.

Versions
Kotlin:
Jackson-module-kotlin: 2.14.0
Jackson-databind: 2.14.0

Additional context
This problem was found in a project with a mixed Java and Kotlin codebase.

Workaround:
Force serializing all iterators as such, using a mixin:

@JsonSerialize(`as` = Iterator::class)
internal interface KotlinIteratorMixin

val mapper = jsonMapper {
        addModule(kotlinModule())
        addMixIn(Iterator::class.java, KotlinIteratorMixin::class.java)
}
@k163377
Copy link
Contributor

k163377 commented Feb 8, 2025

In fact, it is more correct to use intArrayOf.

val kotlinArray = intArrayOf(1, 2, 3)

For Java's Integer[] and Kotlin's Array<Int> the serialization results match, so this issue is closed.

If you think they should be serialized as well as primitive arrays, please submit a feature request to databind.
This was a misunderstanding.
In fact, there seems to be a problem with the serialization result of kotlin.jvm.internal.ArrayIterator, so re-open #674.

@k163377 k163377 closed this as completed Feb 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants