File tree Expand file tree Collapse file tree 1 file changed +12
-5
lines changed Expand file tree Collapse file tree 1 file changed +12
-5
lines changed Original file line number Diff line number Diff line change @@ -62,16 +62,23 @@ Registered user:
6262}
6363```
6464
65- Let's say we want to decode users into the following data structure:
65+ Let's say we want to decode guests and registered users into the following data structure:
6666
6767``` kotlin
6868sealed interface User {
69- data class Guest (val displayName : String ) : User
70- data class Registered (val id : Int , val alias : String , val email : String , val phone : String? ) : User
69+ data class Guest (
70+ val displayName : String
71+ ) : User
72+ data class Registered (
73+ val id : Int ,
74+ val alias : String ,
75+ val email : String ,
76+ val phone : String?
77+ ) : User
7178}
7279```
7380
74- One way to define a decoder for ` User ` objects would be like this:
81+ First we need to define a ` Decoder ` for ` User ` objects like this:
7582
7683``` kotlin
7784val userDecoder: Decoder <User > =
@@ -95,7 +102,7 @@ val userDecoder: Decoder<User> =
95102 }
96103```
97104
98- This decoder can now be used to parse and decode the users using the ` decodeJson ` function:
105+ We can now use ` userDecoder ` to parse and decode the JSON data using the ` decodeJson ` function:
99106
100107``` kotlin
101108val guest = decodeJson(guestJson, userDecoder)
You can’t perform that action at this time.
0 commit comments