-
Notifications
You must be signed in to change notification settings - Fork 637
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Do not try to end structure in encode/decode stucture extensions if a…
…n exception has been thrown, so the original exception will be propagated Fixes #1187
- Loading branch information
Showing
3 changed files
with
54 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
formats/json/commonTest/src/kotlinx/serialization/EncodingExtensionsTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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}""") } | ||
} | ||
} |