Class CodecRegistry
-
- All Implemented Interfaces:
public final class CodecRegistryDecides which MessageCodec carries a given type on and off the wire.
Nothing has to be registered. Any
@Serializabletype - including a parameterised one likeList<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 acodecs { }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>andList<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: aTinferred from a value a Java method returned is a platform type (String!), and itsKTypeis not equal totypeOf<String>()-kotlin.jvm.internal.TypeReference.equalscompares 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 andT : Anyruling 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
Foosays nothing aboutList<Foo>, which still goes through JSON. That is deliberate - see codecForType.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description public final classCodecRegistry.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.
-