Class AvroSerializer
- Namespace
- Excalibur.Dispatch.Serialization.Avro
- Assembly
- Excalibur.Dispatch.Serialization.Avro.dll
Apache Avro implementation of ISerializer.
[RequiresUnreferencedCode("Apache.Avro uses runtime schema compilation. AvroSerializer uses Activator.CreateInstance for ISpecificRecord deserialization.")]
[RequiresDynamicCode("Apache.Avro uses runtime schema compilation. AvroSerializer uses Activator.CreateInstance which requires dynamic code generation.")]
public sealed class AvroSerializer : ISerializer
- Inheritance
-
AvroSerializer
- Implements
- Inherited Members
- Extension Methods
Remarks
Schema-based binary serializer optimized for streaming and Kafka scenarios. Types must implement Avro.Specific.ISpecificRecord for Avro schema support.
Serializer ID: Avro (5)
Constraint: Types must implement Avro.Specific.ISpecificRecord. Runtime checks enforce this since ISerializer uses unconstrained generics.
Constructors
AvroSerializer()
Initializes a new instance with default options.
public AvroSerializer()
Properties
ContentType
Gets the IANA content type for this serializer (e.g., "application/json", "application/x-msgpack").
public string ContentType { get; }
Property Value
Remarks
Used for ASP.NET Core content negotiation via Accept and Content-Type headers.
Name
Gets the unique name of this serializer (e.g., "MemoryPack", "System.Text.Json", "MessagePack").
public string Name { get; }
Property Value
Remarks
This name is used for serializer lookup by name via ISerializerRegistry. The name should remain constant across versions for compatibility.
Version
Gets the version of the serializer library (e.g., "2.1.0").
public string Version { get; }
Property Value
Remarks
Used for diagnostics and migration scenarios. This is the version of the underlying serialization library, not a format version.
Methods
DeserializeObject(ReadOnlySpan<byte>, Type)
Deserializes a runtime-typed value from bytes.
public object DeserializeObject(ReadOnlySpan<byte> data, Type type)
Parameters
dataReadOnlySpan<byte>The byte span containing serialized data.
typeTypeThe runtime type to deserialize to.
Returns
- object
The deserialized object.
Exceptions
- ArgumentNullException
Thrown when
typeis null.- SerializationException
Thrown when deserialization fails.
Deserialize<T>(ReadOnlySpan<byte>)
Deserializes a value from a contiguous byte span.
public T Deserialize<T>(ReadOnlySpan<byte> data)
Parameters
dataReadOnlySpan<byte>The byte span containing serialized data.
Returns
- T
The deserialized value.
Type Parameters
TThe type to deserialize to.
Exceptions
- SerializationException
Thrown when deserialization fails.
SerializeObject(object, Type)
Serializes a runtime-typed value to bytes.
public byte[] SerializeObject(object value, Type type)
Parameters
Returns
- byte[]
The serialized byte array.
Remarks
This overload is necessary for binary serializers (MemoryPack, MessagePack) that require concrete types when the compile-time type is an interface.
Exceptions
- ArgumentNullException
Thrown when
valueortypeis null.- SerializationException
Thrown when serialization fails.
Serialize<T>(T, IBufferWriter<byte>)
Serializes a value to a buffer writer. Allocation depends on the implementation, writer and value.
public void Serialize<T>(T value, IBufferWriter<byte> bufferWriter)
Parameters
valueTThe value to serialize.
bufferWriterIBufferWriter<byte>The buffer writer to write serialized data to.
Type Parameters
TThe type to serialize.
Remarks
Null-argument contract (canonical across all implementations): a null
value is rejected with ArgumentNullException, mirroring
SerializeObject(object, Type). This guarantees that swapping the configured
serializer (MemoryPack, MessagePack, System.Text.Json, Avro, Protobuf, …) does not change the
observable null-handling behavior. For value types T the guard is a no-op.
Exceptions
- ArgumentNullException
Thrown when
valueorbufferWriteris null.- SerializationException
Thrown when serialization fails.