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

commonMain.app.softwork.serialization.flf.FixedLengthSealedEncoder.kt Maven / Gradle / Ivy

There is a newer version: 0.0.18
Show newest version
package app.softwork.serialization.flf

import kotlinx.serialization.*
import kotlinx.serialization.descriptors.*
import kotlinx.serialization.encoding.*
import kotlinx.serialization.modules.*

@ExperimentalSerializationApi
internal class FixedLengthSealedEncoder(
    private val typeLength: Int?,
    private val originalEncoder: FixedLengthEncoder
) : Encoder by originalEncoder, CompositeEncoder by originalEncoder {
    override val serializersModule: SerializersModule = originalEncoder.serializersModule

    override fun beginStructure(descriptor: SerialDescriptor): FixedLengthSealedEncoder {
        originalEncoder.beginStructure(descriptor)
        return this
    }

    override fun encodeStringElement(descriptor: SerialDescriptor, index: Int, value: String) {
        if (index == 0) {
            if (typeLength != null) {
                originalEncoder.encode(value, typeLength)
            }
        } else {
            originalEncoder.encodeStringElement(descriptor, index, value)
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy