Home

Awesome

Azura

source generation based serialization

PackageRelease
AzuraNuGet
Azura.GeneratorNuGet

Why?

Generating source code instead of relying on reflection is nice. For example, when using NativeAOT you should be able to strip reflection metadata and still have serialization work, as everything is hooked up at design time.

Target use case is communication between game clients and third-party programs.

Usage

  1. Add generator library (includes base package, does not need to be explicitly included)
<PackageReference Include="Azura.Generator" Version="version" />
  1. Define a data type and mark properties or fields
using Azura;
[Azura]
public record Data
{
    [Azura] public int Property1 { get; init; }
    [Azura] public string? Property2 { get; init; }
    [Azura] public string?[]? Property3 { get; init; }
}
  1. Serialize the data
var data = new Data {Property2 = "Execute order", Property1 = 66};
data.Serialize(stream);
  1. Retrieve the data
    • Helper types are generated as <ClassName>Serialization
var data = DataSerialization.Deserialize(stream);

Limitations

Pending features

Binary format

Custom serialization

Custom serialization for existing library types can be added but requires manual implementation of the below signatures in a class under the target type's namespace named <ClassName>Serialization.

Value types

public static T Deserialize(Stream stream);
public static void Deserialize(Stream stream, out T self) => self = Deserialize(stream);
public static void Serialize(T self, Stream stream) => Serialize(in self, stream);
public static void Serialize(this in T self, Stream stream);

Reference types

public static T Deserialize(Stream stream);
public static void Deserialize(Stream stream, out T self) => self = Deserialize(stream);
public static void Serialize(this T self, Stream stream) => Serialize(in self, stream);
public static void Serialize(in T self, Stream stream);