Skip to content

Commit 21311a6

Browse files
authored
Clarify example for SerializationException (#2806)
1 parent 2f1dbdb commit 21311a6

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

core/commonMain/src/kotlinx/serialization/SerializationExceptions.kt

+4-2
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@ import kotlinx.serialization.descriptors.*
2222
* It is also an established pattern to validate input in user's classes in the following manner:
2323
* ```
2424
* @Serializable
25-
* class Foo(...) {
25+
* class User(val age: Int, val name: String) {
2626
* init {
27-
* required(age > 0) { ... }
27+
* require(age > 0) { ... }
2828
* require(name.isNotBlank()) { ... }
2929
* }
3030
* }
31+
*
32+
* Json.decodeFromString<User>("""{"age": -100, "name": ""}""") // throws IllegalArgumentException from require()
3133
* ```
3234
* While clearly being serialization error (when compromised data was deserialized),
3335
* Kotlin way is to throw `IllegalArgumentException` here instead of using library-specific `SerializationException`.

0 commit comments

Comments
 (0)