Skip to content

Codecs

ICodec turns a CloudEvent's data into a typed payload and back. It is selected by two things: the event's datacontenttype and the payload type the caller asks for.

public interface ICodec
{
    T Decode<T>(CloudEvent cloudEvent);
    CloudEvent Encode<T>(T payload, string contentType);
}

Decode

  1. read cloudEvent.DataContentType; absent means application/json (the CloudEvents default);
  2. look up (content type, typeof(T));
  3. call the decoder;
  4. return the value as T, or fail with a domain exception.

Encode

The content type is mandatory: a payload may have more than one wire form — JSON for state, FlatBuffers for the event — so the caller names the format and the codec never guesses.

  1. look up (contentType, typeof(T));
  2. call the encoder;
  3. assert the encoder produced exactly that datacontenttype.

What a codec does not do

  • It does not set type, topic, subject or extensions — the envelope does.
  • It does not decide which events are samples — a feed does.
  • It does not serialize state — the state layer renders the JSON form.