KotlinxSerializationJsonSerializer

@ExperimentalSerializationApi
class KotlinxSerializationJsonSerializer(jsonFormat: Json) : JsonSerializer

A JsonSerializer for integrating with kotlinx.serialization.

CAVEAT: This class uses experimental Kotlin Serialization APIs, and only works with non-nullable types. We plan to lift these limitations after kotlinx.serialization issue 1348 is resolved.

In the meantime, if you prefer not to rely on experimental APIs, or if you must read or write a nullable type, we advise doing the serialization "manually" as shown in the following sample. Note that you can still make KotlinxSerializationJsonSerializer the default serializer, and override the serialization for nullable types as needed.

Samples

import com.couchbase.client.kotlin.Cluster
import com.couchbase.client.kotlin.Collection
import com.couchbase.client.kotlin.codec.Content
import com.couchbase.client.kotlin.codec.KotlinxSerializationJsonSerializer
import com.couchbase.client.kotlin.kv.GetResult
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.KSerializer
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json
import kotlinx.serialization.serializer
fun main() { 
   //sampleStart 
   // How to make kotlinx.serialization the default JSON serializer.

// CAVEAT: This serializer cannot handle nullable types.
// For nullable types, you must do the serialization yourself
// as shown in the other sample.
val cluster = Cluster.connect("127.0.0.1", "Administrator", "password") {
    @OptIn(ExperimentalSerializationApi::class)
    jsonSerializer = KotlinxSerializationJsonSerializer(Json {
        // Specify JSON serial format options here. For example:
        encodeDefaults = true
    })
} 
   //sampleEnd
}
import com.couchbase.client.kotlin.Cluster
import com.couchbase.client.kotlin.Collection
import com.couchbase.client.kotlin.codec.Content
import com.couchbase.client.kotlin.codec.KotlinxSerializationJsonSerializer
import com.couchbase.client.kotlin.kv.GetResult
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.KSerializer
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json
import kotlinx.serialization.serializer
fun main() { 
   //sampleStart 
   // How to do the serialization yourself. Recommended if you do not want
// to rely on experimental APIs, or if you must serialize nullable types.

// Here are some convenient extension functions:

// Returns a `Content` object which smuggles the pre-serialized
// JSON past the default transcoder.
fun <T> KSerializer<T>.encodeToJson(value: T): Content =
    Content.json(Json.encodeToString(this, value))

fun <T> GetResult.decodeFromJson(serializer: KSerializer<T>): T =
    Json.decodeFromString(serializer, String(content.bytes))

// The SampleData class used here is defined as:
//
//     @Serializable
//     data class SampleData<T>(val x: Int, val y: T)

// Depending on the Kotlin version, getting a serializer like this
// might be expensive. Save time by sharing a single instance for each type.
// See https://github.com/Kotlin/kotlinx.serialization/issues/1348
val serializer = serializer<SampleData<String?>>()

collection.upsert("foo", serializer.encodeToJson(SampleData(1, null)))
val data = collection.get("foo").decodeFromJson(serializer) 
   //sampleEnd
}

Constructors

Link copied to clipboard
fun KotlinxSerializationJsonSerializer(jsonFormat: Json = Json)

Functions

Link copied to clipboard
open override fun <T> deserialize(json: ByteArray, type: TypeRef<T>): T
Link copied to clipboard
open override fun <T> serialize(value: T, type: TypeRef<T>): ByteArray