Skip to content

Commit

Permalink
Add info about Gson serialization to README
Browse files Browse the repository at this point in the history
  • Loading branch information
azabost committed Jun 29, 2023
1 parent be2091f commit d67d05a
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ when (val orange = "Orange".codifiedEnum<Fruit>()) {

### CodifiedEnum serialization ###

#### kotlinx.serialization ####

Add the following dependency in order to access `CodifiedEnum` serializer using
[Kotlin serialization](https://github.com/Kotlin/kotlinx.serialization).

Expand Down Expand Up @@ -91,8 +93,6 @@ val wrapperFromJsonWithOrange = json.parse(FruitWrapper.serializer(), jsonWithOr
Assertions.assertEquals("Orange", wrapperFromJsonWithOrange.fruit.code())
```

#### Serialization of collections with CodifiedEnum ####

When `CodifiedEnum` is a parameter of a collection such as `List`,
`@Serializable` should be applied to `CodifiedEnum` - inside the
collection type:
Expand All @@ -103,4 +103,20 @@ data class FoodBasket(
val fruits: List<@Serializable(with = Fruit.CodifiedSerializer::class) CodifiedEnum<Fruit, String>>,
val vegetables: List<@Serializable(with = Vegetable.CodifiedSerializer::class) CodifiedEnum<Vegetable, String>>
)
```

#### Gson ####

Add this dependency:

```kotlin
implementation("dev.bright.codified:enums-gson:1.8.22.1")
```

and register the `TypeAdapterFactory` for `CodifiedEnum`:

```kotlin
val gson = GsonBuilder()
.registerTypeAdapterFactory(CodifiedEnumTypeAdapter.Factory())
.create()
```

0 comments on commit d67d05a

Please sign in to comment.