-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide an available KotlinXJsonConverter, since the kotlinx.serializ…
…ation is multiplatform and requires inline reified functions, which aren't compatible with a generic interface or abstract class
- Loading branch information
Showing
3 changed files
with
60 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
strada/src/main/kotlin/dev/hotwire/strada/StradaJsonConverter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,63 @@ | ||
package dev.hotwire.strada | ||
|
||
import kotlinx.serialization.decodeFromString | ||
import kotlinx.serialization.encodeToString | ||
import kotlinx.serialization.json.Json | ||
import java.lang.Exception | ||
|
||
abstract class StradaJsonConverter { | ||
companion object { | ||
const val NO_CONVERTER = | ||
"A Strada.config.jsonConverter must be set to encode or decode json" | ||
|
||
const val INVALID_CONVERTER = | ||
"The configured json converter must be a StradaJsonTypeConverter " + | ||
"or use the provided KotlinXJsonConverter." | ||
|
||
inline fun <reified T> toObject(jsonData: String): T? { | ||
val converter = requireNotNull(Strada.config.jsonConverter) { NO_CONVERTER } | ||
|
||
return when (converter) { | ||
is KotlinXJsonConverter -> converter.toObject<T>(jsonData) | ||
is StradaJsonTypeConverter -> converter.toObject(jsonData, T::class.java) | ||
else -> throw IllegalStateException(INVALID_CONVERTER) | ||
} | ||
} | ||
|
||
inline fun <reified T> toJson(data: T): String { | ||
val converter = requireNotNull(Strada.config.jsonConverter) { NO_CONVERTER } | ||
|
||
return when (converter) { | ||
is KotlinXJsonConverter -> converter.toJson(data) | ||
is StradaJsonTypeConverter -> converter.toJson(data, T::class.java) | ||
else -> throw IllegalStateException(INVALID_CONVERTER) | ||
} | ||
} | ||
} | ||
} | ||
|
||
abstract class StradaJsonTypeConverter : StradaJsonConverter() { | ||
abstract fun <T> toObject(jsonData: String, type: Class<T>): T? | ||
abstract fun <T> toJson(data: T, type: Class<T>): String | ||
} | ||
|
||
class KotlinXJsonConverter : StradaJsonConverter() { | ||
val json = Json { ignoreUnknownKeys = true } | ||
|
||
inline fun <reified T> toObject(jsonData: String): T? { | ||
return try { | ||
json.decodeFromString(jsonData) | ||
} catch(e: Exception) { | ||
logException(e) | ||
null | ||
} | ||
} | ||
|
||
inline fun <reified T> toJson(data: T): String { | ||
return json.encodeToString(data) | ||
} | ||
|
||
fun logException(e: Exception) { | ||
logEvent("kotlinXJsonConverterFailedWithError", e.toString()) | ||
} | ||
} |