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

commonMain.com.charleskorn.kaml.YamlContentPolymorphicSerializer.kt Maven / Gradle / Ivy

The newest version!
/*

   Copyright 2018-2023 Charles Korn.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       https://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

*/

package com.charleskorn.kaml

import kotlinx.serialization.DeserializationStrategy
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.InternalSerializationApi
import kotlinx.serialization.KSerializer
import kotlinx.serialization.SerialInfo
import kotlinx.serialization.SerializationException
import kotlinx.serialization.descriptors.PolymorphicKind
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.descriptors.buildSerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import kotlinx.serialization.serializerOrNull
import kotlin.reflect.KClass

@OptIn(ExperimentalSerializationApi::class)
public abstract class YamlContentPolymorphicSerializer(private val baseClass: KClass) : KSerializer {
    @OptIn(InternalSerializationApi::class)
    override val descriptor: SerialDescriptor = buildSerialDescriptor(
        "${YamlContentPolymorphicSerializer::class.simpleName}<${baseClass.simpleName}>",
        PolymorphicKind.SEALED,
    ) {
        annotations += Marker()
    }

    @SerialInfo
    internal annotation class Marker

    @OptIn(InternalSerializationApi::class)
    override fun serialize(encoder: Encoder, value: T) {
        val actualSerializer = encoder.serializersModule.getPolymorphic(baseClass, value)
            ?: value::class.serializerOrNull()
            ?: throwSubtypeNotRegistered(value::class, baseClass)
        @Suppress("UNCHECKED_CAST")
        (actualSerializer as KSerializer).serialize(encoder, value)
    }

    override fun deserialize(decoder: Decoder): T {
        return decoder.decodeSerializableValue(selectDeserializer((decoder as YamlInput).node))
    }

    public abstract fun selectDeserializer(node: YamlNode): DeserializationStrategy

    private fun throwSubtypeNotRegistered(subClass: KClass<*>, baseClass: KClass<*>): Nothing {
        val subClassName = subClass.simpleName ?: "$subClass"
        throw SerializationException(
            """
            Class '$subClassName' is not registered for polymorphic serialization in the scope of '${baseClass.simpleName}'.
            Mark the base class as 'sealed' or register the serializer explicitly.
            """.trimIndent(),
        )
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy