Class CodecRegistry

  • All Implemented Interfaces:

    
    public final class CodecRegistry
    
                        

    Decides which MessageCodec carries a given type on and off the wire.

    Nothing has to be registered. Any @Serializable type - including a parameterised one like List<OrderEvent> - is serialized as JSON with the Json configured by Builder.configureJson, resolved from the type at the call site. The registry is an override table: it holds the built-in text and binary codecs, plus whatever a codecs { } block registers for types that need something other than JSON.

    Resolution, in order:

    • an exact type match among the overrides - this is what carries a registration for a parameterised type, since List<A> and List<B> are different keys;

    • for a type with no type arguments, a match on the classifier alone (see below);

    • a JSON codec derived from the type's serializer, or NatsyMissingCodecException if it has none.

    Why step 2 exists. typeOf<T>() is sensitive to flexibility: a T inferred from a value a Java method returned is a platform type (String!), and its KType is not equal to typeOf<String>() - kotlin.jvm.internal.TypeReference.equals compares the platform-type upper bound. Without step 2, kv.put("audit", ack.stream) would miss StringCodec and write a JSON-quoted string. With no type arguments and T : Any ruling out a nullable top level, the classifier determines the codec, so this step is exact rather than a fallback - it cannot serve a codec for some other type, which is the hazard an erased lookup would reintroduce.

    An override is matched by type, not by assignability: registering a codec for Foo says nothing about List<Foo>, which still goes through JSON. That is deliberate - see codecForType.

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      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.

    • 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
      • Methods inherited from class java.lang.Object

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

    • Method Detail