System.Text.Json is fast but MemoryPack uses binary format for extreme performance.
Install:
dotnet add package MemoryPack
Define Serializable Type:
[MemoryPackable]
public partial class User
{
public int Id { get; set; }
public string Name { get; set; } = string.Empty;
public string Email { get; set; } = string.Empty;
public DateTime CreatedAt { get; set; }
}
Serialize/Deserialize:
var user = new User { Id = 1, Name = "John", Email = "john@example.com" };
// Serialize to bytes
byte[] bytes = MemoryPackSerializer.Serialize(user);
// Deserialize
var restored = MemoryPackSerializer.Deserialize(bytes);
Performance Comparison:
Serialize 100K objects: System.Text.Json: 850ms, 12MB output MemoryPack: 75ms, 7MB output = 11x faster, 40% smaller!
Perfect For: Cache storage, inter-service communication, game state, real-time apps
