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

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

There is a newer version: 1.7.3
Show newest version
package kotlinx.serialization

import kotlinx.serialization.descriptors.*
import kotlinx.serialization.encoding.*
import kotlinx.serialization.json.*
import kotlin.test.*

class EncodingExtensionsTest {

    @Serializable(with = BoxSerializer::class)
    class Box(val i: Int)

    @Serializer(forClass = Box::class)
    object BoxSerializer : KSerializer {
        override val descriptor: SerialDescriptor = buildClassSerialDescriptor("Box") {
            element("i")
        }

        override fun serialize(encoder: Encoder, value: Box) {
            encoder.encodeStructure(descriptor) {
                throw ArithmeticException()
            }
        }

        override fun deserialize(decoder: Decoder): Box {
            decoder.decodeStructure(descriptor) {
                throw ArithmeticException()
            }
        }
    }

    @Test
    fun testEncodingExceptionNotSwallowed() {
        assertFailsWith { Json.encodeToString(Box(1)) }
    }

    @Test
    fun testDecodingExceptionNotSwallowed() {
        assertFailsWith { Json.decodeFromString("""{"i":1}""") }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy