Class CodecRegistry.Builder
-
- All Implemented Interfaces:
public final class CodecRegistry.BuilderCollects the codecs that override the JSON default, and the Json every derived codec uses.
Order does not matter: registrations are materialised once the block has run, so configureJson reaches codecs registered before it as well as after.
-
-
Method Summary
Modifier and Type Method Description final <T extends Any> Unitregister(MessageCodec<T> codec)Carry T with codec instead of JSON. final <T extends Any> UnitregisterJson(KSerializer<T> serializer)Carry T as JSON using serializer rather than the one derived from the type. final UnitconfigureJson(Function1<JsonBuilder, Unit> block)Adjust the Json that backs every JSON codec, derived or registered. -
-
Method Detail
-
register
final <T extends Any> Unit register(MessageCodec<T> codec)
Carry T with codec instead of JSON.
Matched by exact type, so a codec registered for
Foois not used forList<Foo>.
-
registerJson
final <T extends Any> Unit registerJson(KSerializer<T> serializer)
Carry T as JSON using serializer rather than the one derived from the type.
Only needed for a hand-written or otherwise non-default KSerializer: an
@Serializabletype is already served without any registration. The serializer is paired with the registry's configured Json, which is why this exists rather thanregister(JsonCodec(serializer, json))- that Json is not reachable from here.
-
configureJson
final Unit configureJson(Function1<JsonBuilder, Unit> block)
Adjust the Json that backs every JSON codec, derived or registered.
The block starts from JsonCodec.defaults - strict JSON grammar, unknown keys ignored, defaults not encoded - and changes only what it names:
codecs { configureJson { explicitNulls = false serializersModule = SerializersModule { polymorphic(Event::class) { … } } } }This is also where a kotlinx.serialization.modules.SerializersModule enters, which is what makes contextual and polymorphic serialization work for auto-derived codecs.
Call it as many times as you like, before or after any registration; the blocks are applied in order once the
codecs { }block has finished.
-
-
-
-