• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2017-2024 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3  */
4 
5 package kotlinx.serialization
6 
7 import kotlinx.serialization.descriptors.*
8 import kotlinx.serialization.encoding.*
9 import kotlinx.serialization.modules.*
10 import kotlinx.serialization.test.*
11 import kotlinx.serialization.test.shouldFail
12 import org.junit.Test
13 import kotlin.reflect.*
14 import kotlin.test.*
15 
16 interface JApiError {
17     val code: Int
18 }
19 
20 class InterfaceContextualSerializerTestJvm {
21     object MyApiErrorSerializer : KSerializer<JApiError> {
22         override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("JApiError", PrimitiveKind.INT)
23 
serializenull24         override fun serialize(encoder: Encoder, value: JApiError) {
25             encoder.encodeInt(value.code)
26         }
27 
deserializenull28         override fun deserialize(decoder: Decoder): JApiError {
29             val code = decoder.decodeInt()
30             return object : JApiError {
31                 override val code: Int = code
32             }
33         }
34     }
35 
36     @Test
testDefaultnull37     fun testDefault() {
38         assertEquals(PolymorphicKind.OPEN, serializer(typeTokenOf<JApiError>()).descriptor.kind)
39     }
40 
41     @Suppress("UNCHECKED_CAST")
42     @Test
testContextualnull43     fun testContextual() {
44         val module = serializersModuleOf(JApiError::class, MyApiErrorSerializer)
45         assertSame(MyApiErrorSerializer, module.serializer(typeTokenOf<JApiError>()) as KSerializer<JApiError>)
46     }
47 
48     @Test
testInsideListnull49     fun testInsideList() {
50         val module = serializersModuleOf(JApiError::class, MyApiErrorSerializer)
51         assertSame(MyApiErrorSerializer.descriptor, module.serializer(typeTokenOf<List<JApiError>>()).descriptor.elementDescriptors.first())
52     }
53 
54     interface Parametrized<T> {
55         val param: List<T>
56     }
57 
58     class PSer<T>(val tSer: KSerializer<T>): KSerializer<Parametrized<T>> {
59         override val descriptor: SerialDescriptor
60             get() = buildClassSerialDescriptor("PSer<${tSer.descriptor.serialName}>")
61 
serializenull62         override fun serialize(encoder: Encoder, value: Parametrized<T>) {
63             TODO("Not yet implemented")
64         }
65 
deserializenull66         override fun deserialize(decoder: Decoder): Parametrized<T> {
67             TODO("Not yet implemented")
68         }
69     }
70 
71     @Test
testParametrizedInterfacenull72     fun testParametrizedInterface() {
73         assertEquals(PolymorphicKind.OPEN, serializer(typeTokenOf<Parametrized<String>>()).descriptor.kind)
74         val md = SerializersModule {
75             contextual(Parametrized::class) { PSer(it[0]) }
76         }
77         assertEquals("PSer<kotlin.String>", md.serializer(typeTokenOf<Parametrized<String>>()).descriptor.serialName)
78     }
79 }
80