All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonTest.kotlinx.serialization.EncodingCollectionsTest.kt Maven / Gradle / Ivy

There is a newer version: 1.7.3
Show newest version
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!")))
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy