Skip to main content

Built-in Serializers

Dekaf includes serializers for common types that are automatically used when you don't specify a custom serializer.

Supported Types

TypeSerializerDescription
stringSerializers.StringUTF-8 encoded
byte[]Serializers.ByteArrayRaw bytes
ReadOnlyMemory<byte>Serializers.RawBytesZero-copy bytes
intSerializers.Int32Big-endian 32-bit
longSerializers.Int64Big-endian 64-bit
GuidSerializers.Guid16 bytes
IgnoreSerializers.IgnoreNull/empty (for null keys)

Automatic Selection

When you create a producer or consumer without specifying serializers, Dekaf automatically uses the appropriate built-in serializer:

using Dekaf;

// Automatically uses Serializers.String for both key and value
var producer = await Kafka.CreateProducer<string, string>()
.WithBootstrapServers("localhost:9092")
.BuildAsync();

// Automatically uses Serializers.String for key, Serializers.Int64 for value
var producer = await Kafka.CreateProducer<string, long>()
.WithBootstrapServers("localhost:9092")
.BuildAsync();

String Serializer

Encodes strings as UTF-8:

using Dekaf;

var producer = await Kafka.CreateProducer<string, string>()
.WithBootstrapServers("localhost:9092")
.BuildAsync();

await producer.ProduceAsync("topic", "key", "Hello, World!");

Null strings are handled as null Kafka values.

Byte Array Serializer

Passes bytes through unchanged:

using Dekaf;

var producer = await Kafka.CreateProducer<string, byte[]>()
.WithBootstrapServers("localhost:9092")
.BuildAsync();

await producer.ProduceAsync("topic", "key", new byte[] { 1, 2, 3, 4 });

ReadOnlyMemory Serializer

Zero-copy byte handling:

using Dekaf;

var producer = await Kafka.CreateProducer<string, ReadOnlyMemory<byte>>()
.WithBootstrapServers("localhost:9092")
.BuildAsync();

ReadOnlyMemory<byte> data = GetData();
await producer.ProduceAsync("topic", "key", data);

Integer Serializers

Big-endian encoding (network byte order):

using Dekaf;

// 32-bit integer
var producer32 = await Kafka.CreateProducer<int, string>()
.WithBootstrapServers("localhost:9092")
.BuildAsync();

await producer32.ProduceAsync("topic", 12345, "value");

// 64-bit integer
var producer64 = await Kafka.CreateProducer<long, string>()
.WithBootstrapServers("localhost:9092")
.BuildAsync();

await producer64.ProduceAsync("topic", 123456789L, "value");

Guid Serializer

16-byte binary representation:

using Dekaf;

var producer = await Kafka.CreateProducer<Guid, string>()
.WithBootstrapServers("localhost:9092")
.BuildAsync();

await producer.ProduceAsync("topic", Guid.NewGuid(), "value");

Ignore Type

For topics where you don't need keys:

using Dekaf;

var producer = await Kafka.CreateProducer<Ignore, string>()
.WithBootstrapServers("localhost:9092")
.BuildAsync();

// Key is always null
await producer.ProduceAsync("topic", Ignore.Value, "value");

Using Serializers Explicitly

You can also use the built-in serializers explicitly:

using Dekaf;

var producer = await Kafka.CreateProducer<string, string>()
.WithBootstrapServers("localhost:9092")
.WithKeySerializer(Serializers.String)
.WithValueSerializer(Serializers.String)
.BuildAsync();

Unsupported Types

If you use a type without a built-in serializer:

using Dekaf;

// This throws InvalidOperationException at BuildAsync()
var producer = await Kafka.CreateProducer<string, MyCustomType>()
.WithBootstrapServers("localhost:9092")
.BuildAsync(); // Error: No default serializer for type MyCustomType

For custom types, see JSON Serialization or Custom Serializers.