• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package kotlinx.serialization
2 
3 import kotlinx.serialization.descriptors.*
4 import kotlinx.serialization.encoding.*
5 import kotlinx.serialization.json.*
6 import kotlin.test.*
7 
8 class EncodingExtensionsTest {
9 
10     @Serializable(with = BoxSerializer::class)
11     class Box(val i: Int)
12 
13     object BoxSerializer : KSerializer<Box> {
<lambda>null14         override val descriptor: SerialDescriptor = buildClassSerialDescriptor("Box") {
15             element<Int>("i")
16         }
17 
serializenull18         override fun serialize(encoder: Encoder, value: Box) {
19             encoder.encodeStructure(descriptor) {
20                 throw ArithmeticException()
21             }
22         }
23 
deserializenull24         override fun deserialize(decoder: Decoder): Box {
25             decoder.decodeStructure(descriptor) {
26                 throw ArithmeticException()
27             }
28         }
29     }
30 
31     @Test
testEncodingExceptionNotSwallowednull32     fun testEncodingExceptionNotSwallowed() {
33         assertFailsWith<ArithmeticException> { Json.encodeToString(Box(1)) }
34     }
35 
36     @Test
testDecodingExceptionNotSwallowednull37     fun testDecodingExceptionNotSwallowed() {
38         assertFailsWith<ArithmeticException> { Json.decodeFromString<Box>("""{"i":1}""") }
39     }
40 }
41