HexaEngine.Sappho is a high-performance, source-generated binary serialization library for .NET with support for polymorphism, cyclic references, and efficient memory management. Designed as a submodule of the HexaEngine game engine, it provides fast binary serialization suitable for game state persistence, network communication, asset management, and complex object graph serialization.
$ dotnet add package HexaEngine.SapphoA high-performance, source-generated binary serialization library for .NET with support for polymorphism, cyclic references, and efficient memory management. Part of the HexaEngine game engine ecosystem.
HexaEngine.Sappho is a specialized serialization library designed for game development scenarios where performance is critical. Originally developed as a submodule for the HexaEngine game engine, it provides efficient binary serialization suitable for:
While designed for game engines, Sappho is a standalone library suitable for any .NET application requiring high-performance binary serialization.
dotnet add package HexaEngine.Sappho
Use the [SapphoObject] attribute to mark classes or structs for serialization:
using HexaEngine.Sappho;
[SapphoObject]
public partial class Person
{
public string Name { get; set; }
public int Age { get; set; }
public float Height { get; set; }
}
Note: Classes must be marked as
partialto allow the source generator to add serialization code.
// Create a serialization context
var context = new SapphoSerializationContext();
// Create an object
var person = new Person
{
Name = "John Doe",
Age = 30,
Height = 5.9f
};
// Serialize
using var writer = new SapphoWriter(context);
writer.WriteObject(person);
// Deserialize
var reader = new SapphoReader(writer.Buffer, writer.Count, context);
var deserializedPerson = reader.ReadObject<Person>();
Use the [SapphoPolymorphic] attribute to enable polymorphic serialization:
[SapphoObject]
public partial class Animal
{
public string Name { get; set; }
}
[SapphoObject]
public partial class Dog : Animal
{
public string Breed { get; set; }
}
[SapphoObject]
public partial class Cat : Animal
{
public int LivesRemaining { get; set; }
}
[SapphoObject]
public partial class Zoo
{
[SapphoPolymorphic]
public Animal[] Animals { get; set; }
}
When using polymorphic types, register them with the serialization context:
context.RegisterType<Animal>(Animal.SapphoTypeId);
context.RegisterType<Dog>(Dog.SapphoTypeId);
context.RegisterType<Cat>(Cat.SapphoTypeId);
var zoo = new Zoo
{
Animals = new Animal[]
{
new Dog { Name = "Buddy", Breed = "Golden Retriever" },
new Cat { Name = "Whiskers", LivesRemaining = 9 }
}
};
The library automatically handles cyclic references:
[SapphoObject]
public partial class Node
{
public string Name { get; set; }
public Node? Next { get; set; }
}
var node1 = new Node { Name = "Node 1" };
var node2 = new Node { Name = "Node 2" };
node1.Next = node2;
node2.Next = node1; // Cyclic reference
// Serialization will handle this correctly
Use [SapphoIgnore] to exclude properties from serialization:
[SapphoObject]
public partial class CachedData
{
public string Data { get; set; }
[SapphoIgnore]
public string CachedValue { get; set; } // Not serialized
}
Use [SapphoMember] for fine-grained control over serialization:
[SapphoObject]
public partial class CustomClass
{
[SapphoMember]
public string ImportantField { get; set; }
}
byte, sbyte, short, ushort, int, uint, long, ulong, float, double, bool, charISapphoSerializable<T>HexaEngine.Sappho is designed for high-performance scenarios:
// From the Example project:
// Average serialization time: ~XXX ns (depends on object complexity)
The library uses:
ISapphoSerializable<T>: Base interface for serializable typesISapphoSerializer<T>: Custom serializer interfaceISapphoInstanceId: Optional interface for custom instance ID trackingHexaEngine.Sappho/
├── HexaEngine.Sappho/ # Core serialization library
├── HexaEngine.Sappho.Analyzer/ # Source generator
├── HexaEngine.Sappho.Tests/ # Unit tests
└── Example/ # Example usage
dotnet build
dotnet test
The test suite includes comprehensive tests for:
Contributions are welcome! Please ensure:
This project is licensed under the MIT License. See the LICENSE file for more details.
Part of the HexaEngine ecosystem.
Developed as a submodule for HexaEngine - a modern game engine for .NET.