summaryrefslogtreecommitdiff
path: root/formats/json/commonTest/src/kotlinx/serialization/EncodingExtensionsTest.kt
blob: e8a0c48720d400b1202e58e43d430353ca4592f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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<Box> {
        override val descriptor: SerialDescriptor = buildClassSerialDescriptor("Box") {
            element<Int>("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<ArithmeticException> { Json.encodeToString(Box(1)) }
    }

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