Fluent.Unions.MessagePack
0.3.0
See the version list below for details.
dotnet add package Fluent.Unions.MessagePack --version 0.3.0
NuGet\Install-Package Fluent.Unions.MessagePack -Version 0.3.0
<PackageReference Include="Fluent.Unions.MessagePack" Version="0.3.0" />
<PackageVersion Include="Fluent.Unions.MessagePack" Version="0.3.0" />
<PackageReference Include="Fluent.Unions.MessagePack" />
paket add Fluent.Unions.MessagePack --version 0.3.0
#r "nuget: Fluent.Unions.MessagePack, 0.3.0"
#:package Fluent.Unions.MessagePack@0.3.0
#addin nuget:?package=Fluent.Unions.MessagePack&version=0.3.0
#tool nuget:?package=Fluent.Unions.MessagePack&version=0.3.0
![]()
Fluent.Unions.MessagePack
A MessagePack-CSharp (MIT) formatter and resolver for Option<T>. An option is written as its value, or as nil when it holds none. Results refuse to serialize, like in the System.Text.Json support of Fluent.Unions.
Installation
dotnet add package Fluent.Unions.MessagePack
Usage
using MessagePack;
using Fluent.Unions;
using Fluent.Unions.MessagePack;
var options = MessagePackSerializerOptions.Standard.WithUnions();
byte[] bytes = MessagePackSerializer.Serialize<Option<int>>(7, options); // 7
byte[] none = MessagePackSerializer.Serialize<Option<int>>(Option.None, options); // nil
Option<int> back = MessagePackSerializer.Deserialize<Option<int>>(bytes, options); // 7
WithUnions() puts UnionsResolver in front of the resolver the options already use.
Source-generated formatters
The MessagePack source generator cannot generate code for a member of type Option<T>: it treats the union as a type to generate a formatter for, and the generated resolver does not compile (CS0426). Choose one of these on the containing type:
[MessagePackObject]
public class Customer
{
[Key(0)]
[MessagePackFormatter(typeof(OptionFormatter<string>))]
public Option<string> Nickname { get; set; }
}
[MessagePackObject(SuppressSourceGeneration = true)]
public class Scoreboard
{
[Key(0)]
public Option<int>[] Scores { get; set; } = [];
}
The first names the formatter per member and keeps the source generator. The second falls back to the reflection-based resolver, which finds UnionsResolver through the options.
Documentation
Rules
- Nil is
None. Nil reads asNone, and a formatter that returnsnullalso givesNone.nullnever becomes a value. - Results throw.
Result,Result<T>andResult<T, TError>throwNotSupportedException, wrapped by MessagePack in aMessagePackSerializationException, when writing and when reading. An error would expose exception details such as the stack trace. Convert the result first. - Reflection.
UnionsResolvercreates formatters withMakeGenericType. AOT and trimming are not tested. - Your own unions are not covered by this package.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net11.0 is compatible. |
-
net11.0
- Fluent.Unions (>= 0.3.0)
- MessagePack (>= 3.1.12)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.