Built-in Serializers
Dekaf includes serializers for common types that are automatically used when you don't specify a custom serializer.
Supported Types
| Type | Serializer | Description |
|---|---|---|
string | Serializers.String | UTF-8 encoded |
byte[] | Serializers.ByteArray | Raw bytes |
ReadOnlyMemory<byte> | Serializers.RawBytes | Zero-copy bytes |
int | Serializers.Int32 | Big-endian 32-bit |
long | Serializers.Int64 | Big-endian 64-bit |
Guid | Serializers.Guid | 16 bytes |
Ignore | Serializers.Ignore | Writes zero bytes when serializing and ignores input bytes when deserializing |
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 stringProducer = await Kafka.CreateProducer<string, string>()
.WithBootstrapServers("localhost:9092")
.BuildAsync();
// Automatically uses Serializers.String for key, Serializers.Int64 for value
var longProducer = 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
Use Ignore as the consumer's key type to discard the key bytes. On a producer,
default(Ignore) sends a non-null key of length zero.
To send a record with a null Kafka key, pass null for a reference-type key.
using Dekaf;
await using var producer = await Kafka.CreateProducer<string, string>()
.WithBootstrapServers("localhost:9092")
.BuildAsync();
await producer.ProduceAsync("topic", null, "value");
When you read this record with ConsumeRawBatchAsync, IsKeyNull is true.
For a record produced with default(Ignore), IsKeyNull is false. Both keys have
Key.Length == 0.
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();
The exception names the missing builder call:
No built-in value serializer exists for type 'MyCustomType'. Configure .WithValueSerializer(...) on the producer builder...
Before:
var producer = await Kafka.CreateProducer<string, MyCustomType>()
.WithBootstrapServers("localhost:9092")
.BuildAsync();
After:
var producer = await Kafka.CreateProducer<string, MyCustomType>()
.WithBootstrapServers("localhost:9092")
.WithValueSerializer(new JsonSerializer<MyCustomType>())
.BuildAsync();
For custom types, see JSON Serialization or Custom Serializers.