-
Notifications
You must be signed in to change notification settings - Fork 626
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Function to retrieve KSerializer by KClass and type arguments seriali…
…zers (#2291) Implemented obtainment of KSerializer by KClass in SerializersModule Resolves #2025 The limitations of this API are the inability to implement stable caching, because serialization runtime does not control the equals function of the received parameters serializers, which can cause a memory leak. Also, a technical limitation is the inability to create an array serializer. Co-authored-by: Leonid Startsev <sandwwraith@gmail.com>
- Loading branch information
1 parent
5a8795a
commit a27e86f
Showing
6 changed files
with
226 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
core/commonTest/src/kotlinx/serialization/SerializersModuleTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
/* | ||
* Copyright 2017-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package kotlinx.serialization | ||
|
||
import kotlinx.serialization.builtins.* | ||
import kotlinx.serialization.descriptors.* | ||
import kotlinx.serialization.internal.* | ||
import kotlinx.serialization.modules.* | ||
import kotlinx.serialization.test.* | ||
import kotlin.reflect.* | ||
import kotlin.test.* | ||
|
||
class SerializersModuleTest { | ||
@Serializable | ||
object Object | ||
|
||
@Serializable | ||
sealed class SealedParent { | ||
@Serializable | ||
data class Child(val i: Int) : SealedParent() | ||
} | ||
|
||
@Serializable | ||
abstract class Abstract | ||
|
||
@Serializable | ||
enum class SerializableEnum { A, B } | ||
|
||
@Serializable(CustomSerializer::class) | ||
class WithCustomSerializer(val i: Int) | ||
|
||
@Serializer(forClass = WithCustomSerializer::class) | ||
object CustomSerializer | ||
|
||
@Serializable | ||
class Parametrized<T : Any>(val a: T) | ||
|
||
class ContextualType(val i: Int) | ||
|
||
class ParametrizedContextual<T : Any>(val a: T) | ||
|
||
@Serializer(forClass = ContextualType::class) | ||
object ContextualSerializer | ||
|
||
@Serializer(forClass = ParametrizedContextual::class) | ||
object ParametrizedContextualSerializer | ||
|
||
@Serializable | ||
class ContextualHolder(@Contextual val contextual: ContextualType) | ||
|
||
@Test | ||
fun testCompiled() = noJsLegacy { | ||
assertSame<KSerializer<*>>(Object.serializer(), serializer(Object::class, emptyList(), false)) | ||
assertSame<KSerializer<*>>(SealedParent.serializer(), serializer(SealedParent::class, emptyList(), false)) | ||
assertSame<KSerializer<*>>( | ||
SealedParent.Child.serializer(), | ||
serializer(SealedParent.Child::class, emptyList(), false) | ||
) | ||
|
||
assertSame<KSerializer<*>>(Abstract.serializer(), serializer(Abstract::class, emptyList(), false)) | ||
assertSame<KSerializer<*>>(SerializableEnum.serializer(), serializer(SerializableEnum::class, emptyList(), false)) | ||
} | ||
|
||
@Test | ||
fun testBuiltIn() { | ||
assertSame<KSerializer<*>>(Int.serializer(), serializer(Int::class, emptyList(), false)) | ||
} | ||
|
||
@Test | ||
fun testCustom() { | ||
val m = SerializersModule { } | ||
assertSame<KSerializer<*>>(CustomSerializer, m.serializer(WithCustomSerializer::class, emptyList(), false)) | ||
} | ||
|
||
@Test | ||
fun testParametrized() { | ||
val serializer = serializer(Parametrized::class, listOf(Int.serializer()), false) | ||
assertEquals<KClass<*>>(Parametrized.serializer(Int.serializer())::class, serializer::class) | ||
assertEquals(PrimitiveKind.INT, serializer.descriptor.getElementDescriptor(0).kind) | ||
|
||
val mapSerializer = serializer(Map::class, listOf(String.serializer(), Int.serializer()), false) | ||
assertIs<MapLikeSerializer<*, *, *, *>>(mapSerializer) | ||
assertEquals(PrimitiveKind.STRING, mapSerializer.descriptor.getElementDescriptor(0).kind) | ||
assertEquals(PrimitiveKind.INT, mapSerializer.descriptor.getElementDescriptor(1).kind) | ||
} | ||
|
||
@Test | ||
fun testUnsupportedArray() { | ||
assertFails { | ||
serializer(Array::class, listOf(Int.serializer()), false) | ||
} | ||
} | ||
|
||
@Suppress("UNCHECKED_CAST") | ||
@Test | ||
fun testContextual() { | ||
val m = SerializersModule { | ||
contextual<ContextualType>(ContextualSerializer) | ||
contextual<ParametrizedContextual<*>>(ParametrizedContextualSerializer as KSerializer<ParametrizedContextual<*>>) | ||
contextual(ContextualGenericsTest.ThirdPartyBox::class) { args -> ContextualGenericsTest.ThirdPartyBoxSerializer(args[0]) } | ||
} | ||
|
||
val contextualSerializer = m.serializer(ContextualType::class, emptyList(), false) | ||
assertSame<KSerializer<*>>(ContextualSerializer, contextualSerializer) | ||
|
||
val boxSerializer = m.serializer(ContextualGenericsTest.ThirdPartyBox::class, listOf(Int.serializer()), false) | ||
assertIs<ContextualGenericsTest.ThirdPartyBoxSerializer<Int>>(boxSerializer) | ||
assertEquals(PrimitiveKind.INT, boxSerializer.descriptor.getElementDescriptor(0).kind) | ||
|
||
val parametrizedSerializer = m.serializer(ParametrizedContextual::class, listOf(Int.serializer()), false) | ||
assertSame<KSerializer<*>>(ParametrizedContextualSerializer, parametrizedSerializer) | ||
|
||
val holderSerializer = m.serializer(ContextualHolder::class, emptyList(), false) | ||
assertSame<KSerializer<*>>(ContextualHolder.serializer(), holderSerializer) | ||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters