ZeroAlloc.Serialisation
Source-generated, zero-allocation serialization for .NET 8+. Annotate a type — the Roslyn generator emits a sealed ISerializer<T> implementation and a DI extension. No reflection, no boxing, fully Native AOT safe.
dotnet add package ZeroAlloc.Serialisation
dotnet add package ZeroAlloc.Serialisation.SystemTextJson # or MemoryPack / MessagePack
Quick Start
// 1. Annotate your type
[ZeroAllocSerializable(SerializationFormat.SystemTextJson)]
[JsonSerializable(typeof(OrderPlacedEvent))]
public record OrderPlacedEvent(string OrderId, decimal Total);
// 2. Register
services.AddOrderPlacedEventSerializer();
// or register all types in the assembly at once:
services.AddSerializerDispatcher();
// 3. Inject and use
public class OrderStore(ISerializer<OrderPlacedEvent> serializer)
{
public void Write(IBufferWriter<byte> writer, OrderPlacedEvent evt)
=> serializer.Serialize(writer, evt);
public OrderPlacedEvent? Read(ReadOnlySpan<byte> bytes)
=> serializer.Deserialize(bytes);
}
What Gets Generated
Given [ZeroAllocSerializable(SerializationFormat.MemoryPack)] on a type, the generator emits:
OrderPlacedEventSerializer : ISerializer<OrderPlacedEvent>— sealed, no reflectionAddOrderPlacedEventSerializer()DI extension —TryAddSingletonregistrationSerializerDispatcher— single dispatcher covering all annotated types in the assembly
Packages
| Package | Contents | Target frameworks |
|---|---|---|
ZeroAlloc.Serialisation | ISerializer<T>, ISerializerDispatcher, ZeroAllocSerializableAttribute, and the bundled source generator | netstandard2.1, net8.0, net9.0, net10.0 |
ZeroAlloc.Serialisation.MemoryPack | MemoryPack backend, the fastest binary format | net8.0, net9.0, net10.0 |
ZeroAlloc.Serialisation.MessagePack | MessagePack backend | net8.0, net9.0, net10.0 |
ZeroAlloc.Serialisation.SystemTextJson | System.Text.Json backend, AOT-safe through JsonSerializerContext | net8.0, net9.0, net10.0 |
ZeroAlloc.Serialisation.Generator | Legacy standalone generator, already bundled inside ZeroAlloc.Serialisation. Referencing both loads it twice; see Migrating 1.x → 2.x | netstandard2.0 |
Documentation
| Section | Description |
|---|---|
| Getting Started | Install, annotate, register, and use |
| Source Generator | What the generator emits — per-type and dispatcher output |
| Backends | Backend-specific setup for MemoryPack, MessagePack, and STJ |
| Dependency Injection | Registration patterns and dispatcher usage |
| AOT & Trimming | Native AOT and IL trimming compatibility |
| REST Integration | Use with ZeroAlloc.Rest |
| Migrating 1.x → 2.x | Drop the standalone generator reference — and the two errors if you don't |