Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(retrofit): Add JsonFallback annotation and addEnumAdapter extension function for adding fallback to enums #59

Merged
merged 1 commit into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/libs.toml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ junit4 = { module = "junit:junit", version.ref = "junit4" }
junit5-api = { module = "org.junit.jupiter:junit-jupiter-engine", version.ref = "junit5" }
junit5-engine = { module = "org.junit.jupiter:junit-jupiter-engine", version.ref = "junit5" }
moshi = { module = "com.squareup.moshi:moshi", version.ref = "moshi" }
moshi-adapters = { module = "com.squareup.moshi:moshi-adapters", version.ref = "moshi" }
okhttp = { module = "com.squareup.okhttp3:okhttp", version.ref = "okhttp" }
okhttp-mockWebServer = { module = "com.squareup.okhttp3:mockwebserver", version.ref = "okhttp" }
orgJson = { module = "org.json:json", version.ref = "orgJson" }
Expand Down
22 changes: 22 additions & 0 deletions retrofit/README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,28 @@ moshiBuilder.add(JavaTimeMoshiAdapter)
Moshi adapter for converting java.time's `Instant`, `LocalTime`, `LocalDate`, `LocalDateTime` and `ZonedDateTime` to ISO String
for JSON serialization.

## Enum json fallback

Annotation `@JsonFallback` for adding fallback value to enum.
Enum class can be added via `Moshi.Builder` with extension function `addEnumAdapter`.

```kotlin
@JsonClass(generateAdapter = false)
enum class Level {
@Json(name = "low")
LOW,

@Json(name = "medium")
MEDIUM,

@Json(name = "high")
HIGH,

@JsonFallback
OTHER;
}
```

## OKHttp Cache manager

(Only on Android projects)
Expand Down
1 change: 1 addition & 0 deletions retrofit/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ kotlin {
dependencies {
api(libs.okhttp)
api(libs.moshi)
api(libs.moshi.adapters)
api(libs.retrofit)

implementation(projects.core)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright 2023 INOVA IT d.o.o.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software
* is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package si.inova.kotlinova.retrofit.moshi

import com.squareup.moshi.JsonAdapter
import com.squareup.moshi.Moshi
import com.squareup.moshi.adapters.EnumJsonAdapter

/**
* Annotation for adding fallback value to enum.
*
* Enum class can be added via [Moshi.Builder] with extension function [addEnumAdapter].
*
* Usage:
*
* ```kotlin
* @JsonClass(generateAdapter = false)
* enum class Level {
* @Json(name = "low")
* LOW,
*
* @Json(name = "medium")
* MEDIUM,
*
* @Json(name = "high")
* HIGH,
*
* @JsonFallback
* OTHER;
* }
* ```
*/
annotation class JsonFallback

inline fun <reified T : Enum<T>> Moshi.Builder.addEnumAdapter(): Moshi.Builder =
with(T::class.java) { add(this, createEnumAdapter()) }

fun <T : Enum<T>> Class<T>.createEnumAdapter(): JsonAdapter<T> {
var default: T? = null

enumConstants?.forEach { enumConstant ->
if (getField(enumConstant.name).isAnnotationPresent(JsonFallback::class.java)) {
check(default == null) { "Multiple entries annotated with @${JsonFallback::class.simpleName} on $name" }
default = enumConstant
}
}

requireNotNull(default) { "No entry annotated with @${JsonFallback::class.simpleName} on $name" }

return EnumJsonAdapter.create(this).withUnknownFallback(default)
}
Loading