commonTest.kotlinx.serialization.EncodingExtensionsTest.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.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}""") }
}
}