1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-06-02 07:55:25 +00:00
tgbotapi/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/utils/ExtractDataAndJsonFromDecoder.kt

16 lines
525 B
Kotlin

package dev.inmo.tgbotapi.utils
import kotlinx.serialization.DeserializationStrategy
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.json.JsonDecoder
import kotlinx.serialization.json.JsonElement
fun <T> Decoder.decodeDataAndJson(serializer: DeserializationStrategy<T>): Pair<T, JsonElement?> {
return if (this is JsonDecoder) {
val raw = decodeJsonElement()
json.decodeFromJsonElement(serializer, raw) to raw
} else {
serializer.deserialize(this) to null
}
}