Skip to content

SerializationManager

Manages serializers for KLib, allowing customization and extension.

Adding Serializers

KLib respects your serializer annotations, because of this you can use the @Serializable() annotation:

data class MyData(@Serializable(MyClassSerializer::class) val field: MyClass)

Otherwise, you can mark it as a @Contextual serializer and specify it in the SerializationManager:

data class MyData(@Contextual val field: MyClass)

val myModule = SerializersModule {
 contextual(MyClass::class, MyClassSerializer)
}

SerializationManager {
 module {
  include(myModule)
 }
}

Overwriting Existing Serializers

To overwrite existing serializers:

val myModule = SerializersModule {
 contextual(ResourceLocation::class, CustomResourceLocationSerializer)
}

SerializationManager overwriteWith myModule

Types

SerializationManagerBuilder

Properties

cbor

var cbor: Cbor

The shared Cbor instance, reconfigured with sharedModule whenever overwriteWith or invoke runs.

json

var json: Json

The shared Json instance (unknown keys ignored, nulls omitted), kept in sync with sharedModule.

module

val module: SerializersModule

The SerializersModule shared by cbor/json/nbt, for code that needs a serializer without going through one of those formats - e.g. module.serializer<T>() instead of the bare top-level serializer<T>(), which silently ignores every contextual serializer registered here (ResourceLocation, BlockPos, etc. - see MinecraftSerializersModule) and falls back to raw reflection instead.

nbt

var nbt: Nbt

The shared Java-edition, uncompressed Nbt instance, kept in sync with sharedModule.

Functions

get

Retrieves the ops registry for a specific DynamicOps instance.

Return

A registry that can encode/decode the DynamicOp, or null if not found.

Parameters

  • op: The DynamicOps instance.

Retrieves the serializer registry for a specific Encoder.

Return

A registry that contains the Encoder, or null if not found.

Parameters

  • encoder: The Encoder interface.

Retrieves the serializer registry for a specific Decoder.

Return

A registry that contains the Decoder, or null if not found.

Parameters

  • decoder: The Decoder interface.

invoke

Configures the SerializationManager

overwriteWith

infix fun overwriteWith(module: SerializersModule)

Overwrites existing serializers with the provided module.

Parameters

  • module: The new SerializersModule to use.