commonTest.kotlinx.serialization.EncodingCollectionsTest.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kotlinx-serialization-json
Show all versions of kotlinx-serialization-json
Kotlin multiplatform serialization runtime library
package kotlinx.serialization
import kotlinx.serialization.builtins.ListSerializer
import kotlinx.serialization.builtins.serializer
import kotlinx.serialization.descriptors.*
import kotlinx.serialization.encoding.*
import kotlinx.serialization.json.*
import kotlin.test.*
class EncodingCollectionsTest {
object ListSerializer : KSerializer> {
override val descriptor: SerialDescriptor = ListSerializer(String.serializer()).descriptor
override fun serialize(encoder: Encoder, value: List) {
encoder.encodeCollection(descriptor, value) { index, item ->
encodeStringElement(descriptor, index, item)
}
}
override fun deserialize(decoder: Decoder): List = throw NotImplementedError()
}
@Test
fun testEncoding() {
assertEquals("""["Hello","World!"]""", Json.encodeToString(ListSerializer, listOf("Hello", "World!")))
}
}