https://github.com/bright/codified
https://github.com/bright/codified
Last synced: 9 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/bright/codified
- Owner: bright
- License: mit
- Created: 2020-07-18T19:05:33.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2024-06-16T19:29:04.000Z (almost 2 years ago)
- Last Synced: 2025-04-07T02:38:15.060Z (about 1 year ago)
- Language: Kotlin
- Size: 101 KB
- Stars: 19
- Watchers: 8
- Forks: 3
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README

# codified #
Facilitates objects "codification".
At the moment, the main application of this library is making it easier to encode and decode enum classes in a forward compatible way.
## Installation and usage ##
First, make sure Maven Central is in your repositories block in Gradle build script.
```kotlin
repositories {
mavenCentral()
}
```
### CodifiedEnum ###
Add the following dependency in order to access `CodifiedEnum` class.
```kotlin
implementation("dev.bright.codified:enums:1.9.24.1")
```
`CodifiedEnum` is a sealed class which represents either "known" or "unknown" enum type depending on the `code`
it is represented by. For example:
```kotlin
enum class Fruit(override val code: String) : Codified {
APPLE("Apple")
}
val codifiedAppleFromEnum: CodifiedEnum = Fruit.APPLE.codifiedEnum()
val codifiedAppleFromString: CodifiedEnum = "Apple".codifiedEnum()
val codifiedCherry: CodifiedEnum = "Cherry".codifiedEnum()
Assertions.assertEquals(Fruit.APPLE, codifiedAppleFromEnum.knownOrNull())
Assertions.assertEquals(Fruit.APPLE, codifiedAppleFromString.knownOrNull())
Assertions.assertEquals(Fruit.APPLE.code, codifiedAppleFromEnum.code())
Assertions.assertEquals(Fruit.APPLE.code, codifiedAppleFromString.code())
Assertions.assertEquals(codifiedAppleFromEnum, codifiedAppleFromString)
Assertions.assertNotEquals(codifiedAppleFromEnum, codifiedCherry)
Assertions.assertEquals("Cherry", codifiedCherry.code())
when (val orange = "Orange".codifiedEnum()) {
is CodifiedEnum.Known -> when (orange.value) {
Fruit.APPLE -> TODO()
}
is CodifiedEnum.Unknown -> TODO()
}
```
### CodifiedEnum serialization ###
#### kotlinx.serialization ####
Add the following dependency in order to access `CodifiedEnum` serializer using
[Kotlin serialization](https://github.com/Kotlin/kotlinx.serialization).
```kotlin
implementation("dev.bright.codified:enums-serializer:1.9.24.1")
```
Add `CodifiedSerializer` object for your enum class to handle both known and unknown enum types.
```kotlin
enum class Fruit(override val code: String) : Codified {
APPLE("Apple");
object CodifiedSerializer : KSerializer> by codifiedEnumSerializer()
}
@Serializable
data class FruitWrapper(
@Serializable(with = Fruit.CodifiedSerializer::class)
val fruit: CodifiedEnum
)
val json = Json(JsonConfiguration.Stable)
val wrapperWithApple = FruitWrapper(Fruit.APPLE.codifiedEnum())
val string = json.stringify(FruitWrapper.serializer(), wrapperWithApple)
Assertions.assertEquals("{\"fruit\":\"Apple\"}", string)
val jsonWithApple = "{\"fruit\":\"Apple\"}"
val wrapperFromJsonWithApple = json.parse(FruitWrapper.serializer(), jsonWithApple)
Assertions.assertEquals(Fruit.APPLE, wrapperFromJsonWithApple.fruit.knownOrNull())
val jsonWithOrange = "{\"fruit\":\"Orange\"}"
val wrapperFromJsonWithOrange = json.parse(FruitWrapper.serializer(), jsonWithOrange)
Assertions.assertEquals("Orange", wrapperFromJsonWithOrange.fruit.code())
```
When `CodifiedEnum` is a parameter of a collection such as `List`,
`@Serializable` should be applied to `CodifiedEnum` - inside the
collection type:
```kotlin
@Serializable
data class FoodBasket(
val fruits: List<@Serializable(with = Fruit.CodifiedSerializer::class) CodifiedEnum>,
val vegetables: List<@Serializable(with = Vegetable.CodifiedSerializer::class) CodifiedEnum>
)
```
#### Gson ####
Add this dependency:
```kotlin
implementation("dev.bright.codified:enums-gson:1.9.24.1")
```
and register the `TypeAdapterFactory` for `CodifiedEnum`:
```kotlin
val gson = GsonBuilder()
.registerTypeAdapterFactory(CodifiedEnumTypeAdapter.Factory())
.create()
```