Skip to content

Commit

Permalink
improve readme
Browse files Browse the repository at this point in the history
  • Loading branch information
konnik committed Dec 18, 2023
1 parent 561685f commit c6b2d86
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,23 @@ Registered user:
}
```

Let's say we want to decode users into the following data structure:
Let's say we want to decode guests and registered users into the following data structure:

```kotlin
sealed interface User {
data class Guest(val displayName: String) : User
data class Registered(val id: Int, val alias: String, val email: String, val phone: String?) : User
data class Guest(
val displayName: String
) : User
data class Registered(
val id: Int,
val alias: String,
val email: String,
val phone: String?
) : User
}
```

One way to define a decoder for `User` objects would be like this:
First we need to define a `Decoder` for `User` objects like this:

```kotlin
val userDecoder: Decoder<User> =
Expand All @@ -95,7 +102,7 @@ val userDecoder: Decoder<User> =
}
```

This decoder can now be used to parse and decode the users using the `decodeJson` function:
We can now use `userDecoder` to parse and decode the JSON data using the `decodeJson` function:

```kotlin
val guest = decodeJson(guestJson, userDecoder)
Expand Down

0 comments on commit c6b2d86

Please sign in to comment.