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

xyz.cssxsh.bilibili.data.dynamic.WarpStringSerializer.kt Maven / Gradle / Ivy

The newest version!
package xyz.cssxsh.bilibili.data.dynamic

import kotlinx.serialization.*
import kotlinx.serialization.descriptors.*
import kotlinx.serialization.encoding.*
import kotlinx.serialization.json.*

@PublishedApi
internal class WarpStringSerializer(private val original: KSerializer) : KSerializer {

    override val descriptor: SerialDescriptor =
        PrimitiveSerialDescriptor(this::class.qualifiedName!!, PrimitiveKind.STRING)

    override fun serialize(encoder: Encoder, value: T) {
        val json = (encoder as JsonEncoder).json
        val text = json.encodeToString(serializer = original, value = value)
        encoder.encodeString(value = text)
    }

    override fun deserialize(decoder: Decoder): T {
        val json = (decoder as JsonDecoder).json
        val text = decoder.decodeString()
        return json.decodeFromString(deserializer = original, string = text)
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy