All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.io.github.jan.supabase.SupabaseSerializer.kt Maven / Gradle / Ivy

There is a newer version: 3.0.2
Show newest version
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)




© 2015 - 2024 Weber Informatics LLC | Privacy Policy