Use Codec
Delegates field decoding/encoding to an existing Codec object.
Use this instead of writing a full SPI module when you need a custom field type. The referenced codec must be a Kotlin object implementing Codec<T>.
Without a length annotation — the codec reads directly from the buffer:
@ProtocolMessage
data class Message(
@UseCodec(VariableIntCodec::class) val length: Int,
)
// Generated: val length = VariableIntCodec.decode(buffer)Content copied to clipboard
With a length annotation — the codec receives a size-limited slice:
@ProtocolMessage
data class ImageFrame(
val bitmapLength: Int,
@UseCodec(PngBitmapCodec::class) @LengthFrom("bitmapLength") val bitmap: ImageBitmap,
)
// Generated: val _slice = buffer.readBytes(bitmapLength); val bitmap = PngBitmapCodec.decode(_slice)Content copied to clipboard
Composes with @LengthPrefixed, @RemainingBytes, and @LengthFrom.
Parameters
codec
A KClass referencing a Kotlin object that implements Codec<T>.