Protobuf Serializer/Deserializer of .NET suite for Apache Kafka. KNet is a comprehensive .NET suite for Apache Kafka providing all features: Producer, Consumer, Admin, Streams, Connect, backends (KRaft).
$ dotnet add package MASES.KNet.Serialization.ProtobufKNet comes with a base set of serializer and deserializer. Most of them are usable with primitives types (bool, int, etc) and array of bytes.
If the user wants to use structures types there are two ways:
KNet suite offers some ready made serializer/deserializer usable with the specific APIs (KNetProducer/KNetConsumer).
The current available packages are:
Starting from version 2.7.0, KNet comes with two kind of data exchange mechanisms:
byte array transfer and this is the standard used since last versionByteBuffer objects:
ByteBuffer are exchangedByteBuffer, the memory pointers originating the information and the counterpart reads that memory without make copiesAll available packages listed at the beginning comes with both versions and the user can choose its preferred one.
As example, let consider a type defined like the following one:
public class TestType
{
public TestType(int i)
{
name = description = value = i.ToString();
}
public string name;
public string description;
public string value;
public override string ToString()
{
return $"name {name} - description {description} - value {value}";
}
}
To manage it within C#, without create TestType in Java™, an user can create:
SerDesRaw<TestType> serializer = new SerDesRaw<TestType>()
{
OnSerialize = (topic, type) => { return Array.Empty<byte>(); }
};
SerDesRaw<TestType> deserializer = new SerDesRaw<TestType>()
{
OnDeserialize = (topic, data) => { return new TestType(0); }
};
Otherwise the user can use a ready made class like in the following snippet:
ISerDesRaw<TestType> serdes = JsonSerDes.Value<TestType>.NewByteArraySerDes();
A single JsonSerDes.ValueRaw can be used in serialization and deserialization, and produce Json serialized data.
The reader noticed that in the example was used JsonSerDes.Value<T>().NewByteArraySerDes(). It is a serializer/deserializer, based on byte array, generally used for values because it stores, within the record Headers information related to the value itself.
All packages listed above have multiple types based on the scope and data exchange mechanism:
byte array and ByteBufferbyte array and ByteBufferwhere [Serialization format] depends on the serializatin package in use and the selection of the data transfer can be made from underlying code or can be requested from the user:
[Serialization Format].[Key or Value]<TData>.NewByteArraySerDes(): returns an ISerDesRaw<TData>[Serialization Format].[Key or Value]<TData>.NewByteBufferSerDes(): returns an ISerDesBuffered<TData>[!TIP] As specified above, each serializer stores info within the
Headersand this behavior is controlled from a property namedUseHeaders. If the user writes a code like:ISerDesRaw<TestType> serdes = JsonSerDes.Value<TestType>.NewByteArraySerDes(); serdes.UseHeader = false;The
ISerDesRaw<TestType>instance does not writes theHeadersand can be used both for key and value.
Some kind of serializers extension have specific needs will be listed below.
The Avro serializer is based on Apache.Avro package. The types managed are:
NOTE: simple types (the one that have an Apache Kafka™ default serializer) are not managed and will be refused
The MessagePack serializer is based on MessagePack package. The types managed are:
NOTE: simple types (the one that have an Apche Kafka™ default serializer) are not managed and will be refused
The Protobuf serializer is based on Google.Protobuf package. The types managed are:
IMessage<T>NOTE: simple types (the one that have an Apche Kafka™ default serializer) are not managed and will be refused