Skip to main content

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 reflection
  • AddOrderPlacedEventSerializer() DI extension — TryAddSingleton registration
  • SerializerDispatcher — single dispatcher covering all annotated types in the assembly

Packages​

PackageContentsTarget frameworks
ZeroAlloc.SerialisationISerializer<T>, ISerializerDispatcher, ZeroAllocSerializableAttribute, and the bundled source generatornetstandard2.1, net8.0, net9.0, net10.0
ZeroAlloc.Serialisation.MemoryPackMemoryPack backend, the fastest binary formatnet8.0, net9.0, net10.0
ZeroAlloc.Serialisation.MessagePackMessagePack backendnet8.0, net9.0, net10.0
ZeroAlloc.Serialisation.SystemTextJsonSystem.Text.Json backend, AOT-safe through JsonSerializerContextnet8.0, net9.0, net10.0
ZeroAlloc.Serialisation.GeneratorLegacy standalone generator, already bundled inside ZeroAlloc.Serialisation. Referencing both loads it twice; see Migrating 1.x → 2.xnetstandard2.0

Documentation​

SectionDescription
Getting StartedInstall, annotate, register, and use
Source GeneratorWhat the generator emits — per-type and dispatcher output
BackendsBackend-specific setup for MemoryPack, MessagePack, and STJ
Dependency InjectionRegistration patterns and dispatcher usage
AOT & TrimmingNative AOT and IL trimming compatibility
REST IntegrationUse with ZeroAlloc.Rest
Migrating 1.x → 2.xDrop the standalone generator reference — and the two errors if you don't