Skip to content

CodecSerializer

open class CodecSerializer<T>(codec: Codec<T>) : KSerializer<T> 

Used for Codec.kSerializer, you could extend this class to make any modifications you like.

Note: It is HIGHLY recommended to just use the extension function Codec.kSerializer instead of manually using this class.

Constructors

CodecSerializer

constructor(codec: Codec<T>)

Properties

codec

val <T : Any> KSerializer<T>.codec: Codec<T>

Converts a KSerializer into a Codec.

Note: By default JsonOps and NbtOps are supported. Check SerializationManager.SerializationManagerBuilder.registerDynamicOp to register another DynamicOps. Trying to use unregistered DynamicOps implementations will result in an UnsupportedOperationException.

Example
@Serializable
data class TestData(
    val str: String,
    val int: Int,
    val float: Float,
    val double: Double,
    val boolean: Boolean
)

// Using the toCodec extension:
val TestCodec = TestData.serializer().toCodec()

// The above is equivalent to manually creating a codec:
val ManualTestCodec: Codec<TestData> = RecordCodecBuilder.create {
    it.group(
        Codec.STRING.fieldOf("str").forGetter(TestData::str),
        Codec.INT.fieldOf("int").forGetter(TestData::int),
        Codec.FLOAT.fieldOf("float").forGetter(TestData::float),
        Codec.DOUBLE.fieldOf("double").forGetter(TestData::double),
        Codec.BOOL.fieldOf("boolean").forGetter(TestData::boolean)
    ).apply(it, ::TestData)
}

Return

A Codec of type T defined by the KSerializer.

Throws

UnsupportedOperationException

If the provided DynamicOps type is not supported.

descriptor

open override val descriptor: SerialDescriptor

streamCodec

val <T : Any> KSerializer<T>.streamCodec: StreamCodec<RegistryFriendlyByteBuf, T>

Converts any KSerializer into a StreamCodec using Cbor

Functions

deserialize

open override fun deserialize(decoder: Decoder): T

serialize

open override fun serialize(encoder: Encoder, value: T)