MemoryPack serialization tools/snippets
$ dotnet add package OutWit.Common.MemoryPackOutWit.Common.MemoryPack is a .NET library that provides a collection of helper methods and pre-configured, serializable types to simplify and accelerate development with the MemoryPack serialization library. It offers extension methods for common tasks, custom formatters, and a suite of ready-to-use data structures for various domain needs.
PropertyChangedEventArgs: Includes a built-in formatter for System.ComponentModel.PropertyChangedEventArgs, which is automatically registered and essential for MVVM applications.[MemoryPackable] types designed to be used directly in your data models:
MemoryPackMap<TKey, TValue> and MemoryPackSet<TValue> for serializable, read-only collections.MemoryPackMessage and MemoryPackMessageWith<TData> for creating standardized DTOs.MemoryPackRange<TValue>, MemoryPackRangeSet<TValue>, and MemoryPackValueInSet<TValue> for representing common data constraints.Install the package from NuGet using the .NET CLI:
dotnet add package OutWit.Common.MemoryPack
The MemoryPackUtils class provides convenient extension methods.
using OutWit.Common.MemoryPack;
using MemoryPack;
[MemoryPackable]
public partial class User
{
public string Name { get; set; }
public int Age { get; set; }
}
var user = new User { Name = "John Doe", Age = 30 };
// Serialize the object to bytes
byte[] bytes = user.ToMemoryPackBytes();
// Deserialize the bytes back to an object
User deserializedUser = bytes.FromMemoryPackBytes<User>();
You can easily create a deep clone of any [MemoryPackable] object.
// Clone the user object
[cite_start]User clonedUser = user.MemoryPackClone();
Save a collection to a file and load it back.
using OutWit.Common.MemoryPack.Ranges;
var ranges = new List<MemoryPackRange<int>>
{
new(1, 10),
new(20, 30)
};
// Export the list to a file
await ranges.ExportAsMemoryPackAsync("ranges.bin");
// Load the list from the file
IReadOnlyList<MemoryPackRange<int>> loadedRanges = await MemoryPackUtils.LoadAsMemoryPackAsync<MemoryPackRange<int>>("ranges.bin");
You can register your own custom MemoryPackFormatter during application startup.
// Your custom formatter
public class MyCustomTypeFormatter : MemoryPackFormatter<MyCustomType>
{
// ... implementation
}
// Register it
MemoryPackUtils.Register(options =>
{
options.Register(new MyCustomTypeFormatter());
});
Licensed under the Apache License, Version 2.0. See LICENSE.
If you use OutWit.Common.MemoryPack in a product, a mention is appreciated (but not required), for example: "Powered by OutWit.Common.MemoryPack (https://ratner.io/)"_.
"OutWit" and the OutWit logo are used to identify the official project by Dmitry Ratner.
You may:
You may not: