Class CodecRegistry.Builder

  • All Implemented Interfaces:

    
    public final class CodecRegistry.Builder
    
                        

    Collects 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.

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
    • Field Summary

      Fields 
      Modifier and Type Field Description
    • Constructor Summary

      Constructors 
      Constructor Description
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
    • Method Summary

      Modifier and Type Method Description
      final <T extends Any> Unit register(MessageCodec<T> codec) Carry T with codec instead of JSON.
      final <T extends Any> Unit registerJson(KSerializer<T> serializer) Carry T as JSON using serializer rather than the one derived from the type.
      final Unit configureJson(Function1<JsonBuilder, Unit> block) Adjust the Json that backs every JSON codec, derived or registered.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

    • 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 Foo is not used for List<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 @Serializable type is already served without any registration. The serializer is paired with the registry's configured Json, which is why this exists rather than register(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.