commonMain.io.github.jan.supabase.SupabaseSerializer.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of supabase-kt Show documentation
Show all versions of supabase-kt Show documentation
A Kotlin Multiplatform Supabase SDK
package io.github.jan.supabase
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonElement
import kotlin.reflect.KType
import kotlin.reflect.typeOf
/**
* An interface for serializing and deserializing objects.
* Note: Internally, KotlinX Serialization will always be used.
*/
interface SupabaseSerializer {
/**
* Encodes the given [value] to a string
*/
fun encode(type: KType, value: T): String
/**
* Decodes the given [value] to an object of type [T]
*/
fun decode(type: KType, value: String): T
}
/**
* Encodes the given [value] to a string
*/
inline fun SupabaseSerializer.encode(value: T): String = encode(typeOf(), value)
/**
* Encodes the given [value] to a [JsonElement]
*/
inline fun SupabaseSerializer.encodeToJsonElement(value: T): JsonElement = Json.decodeFromString(encode(value))
/**
* Decodes the given [value] to an object of type [T]
*/
inline fun SupabaseSerializer.decode(value: String): T = decode(typeOf(), value)