Skip to content
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

Fix lookup of primitive array serializers by Java type token #1708

Merged
merged 1 commit into from Sep 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions core/jvmMain/src/kotlinx/serialization/SerializersJvm.kt
Expand Up @@ -124,13 +124,13 @@ private fun SerializersModule.serializerByJavaTypeImpl(type: Type, failOnMissing

@OptIn(ExperimentalSerializationApi::class)
private fun SerializersModule.typeSerializer(type: Class<*>, failOnMissingTypeArgSerializer: Boolean): KSerializer<Any>? {
return if (!type.isArray) {
reflectiveOrContextual(type.kotlin as KClass<Any>, emptyList())
} else {
return if (type.isArray && !type.componentType.isPrimitive) {
val eType: Class<*> = type.componentType
val s = if (failOnMissingTypeArgSerializer) serializer(eType) else (serializerOrNull(eType) ?: return null)
val arraySerializer = ArraySerializer(eType.kotlin as KClass<Any>, s)
arraySerializer as KSerializer<Any>
} else {
reflectiveOrContextual(type.kotlin as KClass<Any>, emptyList())
}
}

Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2017-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
* Copyright 2017-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/

package kotlinx.serialization.features
Expand Down Expand Up @@ -93,6 +93,15 @@ class SerializerByTypeTest {
assertSerializedWithType(token, """["a","b","c"]""", myArr)
}

@Test
fun testPrimitiveArrayResolving() {
val myArr = intArrayOf(1, 2, 3)
val token = IntArray::class.java
val name = serializer(token).descriptor.serialName
assertTrue(name.contains("IntArray"))
assertSerializedWithType(token, """[1,2,3]""", myArr)
}

@Test
fun testReifiedListResolving() {
val myList = listOf("a", "b", "c")
Expand Down