See More

A very common reason is a wrong site baseUrl configuration.\n

Current configured baseUrl = / (default value)\n

We suggest trying baseUrl = \n\n',document.body.prepend(n);var e=document.getElementById("__docusaurus-base-url-issue-banner-suggestion-container"),s=window.location.pathname,o="/"===s.substr(-1)?s:s+"/";e.textContent=o}document.addEventListener("DOMContentLoaded",function(){void 0===window.docusaurus&&insertBanner()})

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.Generator
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

Backends

PackageFormat
ZeroAlloc.Serialisation.MemoryPackMemoryPack (fastest binary)
ZeroAlloc.Serialisation.MessagePackMessagePack
ZeroAlloc.Serialisation.SystemTextJsonSystem.Text.Json (AOT-safe via JsonSerializerContext)

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