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
- read
cloudEvent.DataContentType; absent meansapplication/json(the CloudEvents default); - look up
(content type, typeof(T)); - call the decoder;
- 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.
- look up
(contentType, typeof(T)); - call the encoder;
- assert the encoder produced exactly that
datacontenttype.
What a codec does not do
- It does not set
type,topic,subjector 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.