Hexarc.Borsh
2.1.0
dotnet add package Hexarc.Borsh --version 2.1.0
NuGet\Install-Package Hexarc.Borsh -Version 2.1.0
<PackageReference Include="Hexarc.Borsh" Version="2.1.0" />
<PackageVersion Include="Hexarc.Borsh" Version="2.1.0" />
<PackageReference Include="Hexarc.Borsh" />
paket add Hexarc.Borsh --version 2.1.0
#r "nuget: Hexarc.Borsh, 2.1.0"
#:package Hexarc.Borsh@2.1.0
#addin nuget:?package=Hexarc.Borsh&version=2.1.0
#tool nuget:?package=Hexarc.Borsh&version=2.1.0
Borsh in .NET
Hexarc.Borsh is .NET implementation of the Binary Object Representation Serializer for Hashing format.
Features
- 100% C# library
- Zero dependencies
- Ready for Blazor WebAssembly
Supported platforms
| .NET | Hexarc.Borsh |
|---|---|
6.x |
1.x |
7.x |
2.x |
Getting started
Install the package with the NuGet CLI:
dotnet add package Hexarc.Borsh
Reference the Hexarc.Borsh namespace in your code:
using Hexarc.Borsh;
Serialize and deserialize .NET objects via the BorshSerializer class:
[BorshObject]
public sealed class Point
{
[BorshPropertyOrder(0)]
public required Int32 X { get; init; }
[BorshPropertyOrder(1)]
public required Int32 Y { get; init; }
[BorshPropertyOrder(2)]
public required Int32 Z { get; init; }
}
var point = new Point { X = 5, Y = 10, Z = 20 };
var raw = BorshSerializer.Serialize(point);
var restored = BorshSerializer.Deserialize<Point>(raw);
Serialization capabilities
These types can be serialized by default:
Byte,SByte,Boolean,Int16,UInt16,Int32,UInt32,Int64,UInt64,Int128,UInt128Single,Double,HalfNullable<T>EnumStringDateTimeValueTuple- Arrays as
T[] List<T>HashSet<T>Dictionary<TKey, TValue>Hexarc.Borsh.Option<T>- POCO like user defined classes
Object serialization
Serializable types must be annotated with the BorshObject attribute.
The BorshIgnore attribute can be used to exclude properties from serialization.
[BorshObject]
public sealed class Point
{
[BorshPropertyOrder(0)]
public required Int32 X { get; init; }
[BorshPropertyOrder(1)]
public required Int32 Y { get; init; }
[BorshPropertyOrder(2)]
public required Int32 Z { get; init; }
// This property will be excluded from serialization.
[BorshIgnore]
public String? Memo { get; init; }
}
var raw = BorshSerializer.Serialize(new Point { X = 1, Y = 2, Z = 3 });
Records serialization:
[BorshObject]
public sealed record Rect(
[property: BorshPropertyOrder(0)] Int32 Width,
[property: BorshPropertyOrder(1)] Int32 Height
);
var rect = new Rect(10, 20);
var raw = BorshSerializer.Serialize(rect);
Nullable reference type serialization
Another important notice that Borsh is mostly designed to support the Rust
type system. So null reference values are not directly supported in .NET implementation.
Please use the special BorshOptional attribute or Hexarc.Borsh.Option<T> type for this purpose.
Property annotation example:
[BorshObject]
public sealed class PersonDetails
{
[BorshPropertyOrder(0)]
[BorshOptional]
public String? FirstName { get; init; }
[BorshPropertyOrder(1)]
[BorshOptional]
public String? LastName { get; init; }
}
In case you need to serialize a top level nullable reference type object:
String? input = Console.ReadLine();
var raw = BorshSerializer.Serialize(Option<String>.Create(input));
var restored = BorshSerializer.Deserialize<Option<String>>(raw);
Fixed array type serialization
The BorshFixedArray attribute allows to serialize fixed array types according
to the Borsh specification:
[BorshObject]
public sealed class Data
{
[BorshPropertyOrder(0)]
[BorshFixedArray(3)]
public required Int32[] Numbers { get; init; }
}
var data = new Data { Numbers = new[] { 1, 2, 3 } };
var raw = BorshSerializer.Serialize(data);
Union type serialization
The BorshUnion attribute allows to serialize union types:
[BorshObject]
[BorshUnion<Circle>(0)]
[BorshUnion<Square>(1)]
public abstract class Figure {}
[BorshObject]
public sealed class Circle : Figure
{
[BorshPropertyOrder(0)]
public required Int32 Radius { get; init; }
}
[BorshObject]
public sealed class Square : Figure
{
[BorshPropertyOrder(0)]
public required Int32 SideSize { get; init; }
}
Figure square = new Square { SideSize = 1 };
var raw = BorshSerializer.Serialize(square);
Acknowledgments
Built with JetBrains tools for Open Source projects.
License
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net7.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 2.1.0 | 12,756 | 12/4/2022 |
| 2.0.1 | 507 | 11/18/2022 |
| 2.0.0 | 511 | 11/12/2022 |
| 1.3.1 | 121,830 | 8/31/2022 |
| 1.3.0 | 11,277 | 8/29/2022 |
| 1.2.2 | 557 | 8/28/2022 |
| 1.2.1 | 542 | 8/28/2022 |
| 1.2.0 | 555 | 8/26/2022 |
| 1.1.2 | 561 | 8/6/2022 |
| 1.1.1 | 3,849 | 7/15/2022 |
| 1.1.0 | 591 | 7/15/2022 |
| 1.0.0 | 5,940 | 2/26/2022 |
| 0.6.0 | 610 | 2/25/2022 |
| 0.5.0 | 608 | 2/24/2022 |
| 0.4.0 | 633 | 2/23/2022 |
| 0.3.0 | 594 | 2/22/2022 |
| 0.2.0 | 604 | 2/21/2022 |
| 0.1.0 | 583 | 2/20/2022 |
| 0.0.15 | 591 | 2/19/2022 |
| 0.0.14 | 602 | 2/18/2022 |
| 0.0.13 | 612 | 2/18/2022 |
| 0.0.12 | 628 | 2/17/2022 |
| 0.0.11 | 597 | 2/17/2022 |
| 0.0.10 | 560 | 2/16/2022 |
| 0.0.9 | 585 | 2/16/2022 |
| 0.0.8 | 573 | 2/16/2022 |
| 0.0.7 | 559 | 2/16/2022 |
| 0.0.6 | 586 | 2/16/2022 |
| 0.0.5 | 590 | 2/14/2022 |
| 0.0.4 | 575 | 2/10/2022 |
| 0.0.3 | 579 | 2/10/2022 |
| 0.0.2 | 590 | 2/9/2022 |
| 0.0.1 | 621 | 2/6/2022 |