generate docs for a lot of API (test try)

This commit is contained in:
2026-02-24 18:18:10 +06:00
parent 3df90b1993
commit 4f270d9047
81 changed files with 2519 additions and 6 deletions

View File

@@ -5,6 +5,14 @@ import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
/**
* A serializer that encodes values to Base64 strings and decodes them back.
* Uses custom conversion functions to transform between type [T] and string representation.
*
* @param T The type to serialize/deserialize
* @param converterFrom Converts from type [T] to a string representation
* @param converterTo Converts from a string representation back to type [T]
*/
open class Base64Serializer<T>(
private val converterFrom: (T) -> String,
private val converterTo: (String) -> T,
@@ -14,5 +22,12 @@ open class Base64Serializer<T>(
override fun serialize(encoder: Encoder, value: T) = Base64BytesToFromStringSerializer.serialize(encoder, converterFrom(value).encodeToByteArray())
}
/**
* Serializer for [String] values encoded as Base64.
*/
object Base64StringSerializer : Base64Serializer<String>({ it }, { it })
/**
* Serializer for [ByteArray] values encoded as Base64.
*/
object Base64ByteArraySerializer : Base64Serializer<ByteArray>({ it.decodeToString() }, { it.encodeToByteArray() })

View File

@@ -6,6 +6,17 @@ import kotlinx.serialization.descriptors.*
import kotlinx.serialization.encoding.*
import kotlin.reflect.KClass
/**
* A serializer that includes type information in the serialized output.
* This allows polymorphic serialization where the exact type is preserved in the output
* as a "type" field alongside the "value" field.
*
* The serialized format is: `{"type": "TypeName", "value": {...}}`
*
* @param T The base type that this serializer handles
* @param kClass The Kotlin class of the base type
* @param presetSerializers A map of type names to their serializers for known subtypes
*/
open class TypedSerializer<T : Any>(
kClass: KClass<T>,
presetSerializers: Map<String, KSerializer<out T>> = emptyMap(),
@@ -60,29 +71,67 @@ open class TypedSerializer<T : Any>(
}
/**
* Adds a new type to this serializer with its associated serializer.
*
* @param O The specific subtype to include
* @param type The type name to use in serialized output
* @param serializer The serializer for this type
*/
open fun <O: T> include(type: String, serializer: KSerializer<O>) {
serializers[type] = serializer
}
/**
* Removes a type from this serializer by its name.
*
* @param type The type name to remove
*/
open fun exclude(type: String) {
serializers.remove(type)
}
}
/**
* Adds a type to this [TypedSerializer] using its class name as the type identifier.
*
* @param T The type to add
* @param kClass The Kotlin class to add
*/
@InternalSerializationApi
operator fun <T : Any> TypedSerializer<T>.plusAssign(kClass: KClass<T>) {
include(kClass.simpleName!!, kClass.serializer())
}
/**
* Removes a type from this [TypedSerializer] using its class name.
*
* @param T The type to remove
* @param kClass The Kotlin class to remove
*/
@InternalSerializationApi
operator fun <T : Any> TypedSerializer<T>.minusAssign(kClass: KClass<T>) {
exclude(kClass.simpleName!!)
}
/**
* Creates a [TypedSerializer] for the reified type [T].
*
* @param T The base type to serialize
* @param presetSerializers A map of type names to serializers for known subtypes
* @return A new [TypedSerializer] instance
*/
inline fun <reified T : Any> TypedSerializer(
presetSerializers: Map<String, KSerializer<out T>> = emptyMap()
) = TypedSerializer(T::class, presetSerializers)
/**
* Creates a [TypedSerializer] for the reified type [T] with vararg preset serializers.
*
* @param T The base type to serialize
* @param presetSerializers Pairs of type names to serializers for known subtypes
* @return A new [TypedSerializer] instance
*/
inline fun <reified T : Any> TypedSerializer(
vararg presetSerializers: Pair<String, KSerializer<out T>>
) = TypedSerializer(presetSerializers.toMap())