Class JsonCodec

  • All Implemented Interfaces:
    eu.vstoyanov.natsy.codec.MessageCodec

    
    public final class JsonCodec<T extends Object>
     implements MessageCodec<T>
                        

    Carries a value as UTF-8 JSON via kotlinx.serialization.

    This is what CodecRegistry derives for any @Serializable type, so it rarely needs to be constructed by hand - do so to give one type a Json of its own:

    codecs { register(JsonCodec(Report.serializer(), Json(JsonCodec.defaults) { prettyPrint = true })) }

    Starting that Json from defaults rather than from kotlinx's own is what keeps the one type behaving like every other: Json has no notion of "inherit the rest", so a bare Json { prettyPrint = true } quietly opts the type out of tolerating unknown keys, and the producer that adds a field stops this consumer instead of the two carrying on apart.

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      public class JsonCodec.Companion
    • Constructor Summary

      Constructors 
      Constructor Description
      JsonCodec(KSerializer<T> serializer, Json json)
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
    • Method Summary

      Modifier and Type Method Description
      String getContentType() Content type identifier for this codec
      ByteArray encode(T value) Encode a value for publishing.
      T decode(ByteArray data) Decode bytes received from NATS.
      • Methods inherited from class java.lang.Object

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

      • JsonCodec

        JsonCodec(KSerializer<T> serializer, Json json)