https://github.com/hexarc-software/hexarc-borsh
.NET implementation of Binary Object Representation Serializer for Hashing
https://github.com/hexarc-software/hexarc-borsh
binary blazor blazor-webassembly blockchain borsh cryptography csharp deserialization dotnet hashing near serialization solana webassembly
Last synced: about 2 months ago
JSON representation
.NET implementation of Binary Object Representation Serializer for Hashing
- Host: GitHub
- URL: https://github.com/hexarc-software/hexarc-borsh
- Owner: hexarc-software
- License: mit
- Created: 2022-02-06T10:10:05.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2022-12-24T10:31:18.000Z (over 2 years ago)
- Last Synced: 2025-04-10T00:52:24.960Z (about 2 months ago)
- Topics: binary, blazor, blazor-webassembly, blockchain, borsh, cryptography, csharp, deserialization, dotnet, hashing, near, serialization, solana, webassembly
- Language: C#
- Homepage:
- Size: 187 KB
- Stars: 5
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/funding.yml
- License: License.txt
Awesome Lists containing this project
README
# Borsh in .NET
[](http://badges.mit-license.org)
[](https://www.nuget.org/packages/Hexarc.Borsh)
[](https://www.nuget.org/packages/Hexarc.Borsh)
[](https://fondify.app/to/9qjvN4GZf78M2JsLUxXUNQWnDo96s12Zzzkzoo8cFpqw)Hexarc.Borsh is .NET implementation of the [Binary Object Representation Serializer for Hashing](https://borsh.io/) 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:
```sh
dotnet add package Hexarc.Borsh
```Reference the `Hexarc.Borsh` namespace in your code:
```cs
using Hexarc.Borsh;
```Serialize and deserialize .NET objects via the `BorshSerializer` class:
```cs
[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(raw);
```## Serialization capabilities
These types can be serialized by default:
* `Byte`, `SByte`, `Boolean`, `Int16`, `UInt16`, `Int32`, `UInt32`, `Int64`, `UInt64`, `Int128`, `UInt128`
* `Single`, `Double`, `Half`
* `Nullable`
* `Enum`
* `String`
* `DateTime`
* `ValueTuple`
* Arrays as `T[]`
* `List`
* `HashSet`
* `Dictionary`
* `Hexarc.Borsh.Option`
* 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.
```cs
[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:
```cs
[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` type for this purpose.Property annotation example:
```cs
[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:
```cs
String? input = Console.ReadLine();var raw = BorshSerializer.Serialize(Option.Create(input));
var restored = BorshSerializer.Deserialize>(raw);
```### Fixed array type serialization
The `BorshFixedArray` attribute allows to serialize fixed array types according
to the Borsh specification:
```cs
[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:
```cs
[BorshObject]
[BorshUnion(0)]
[BorshUnion(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](https://jb.gg/OpenSourceSupport) projects.
## License
MIT © [Hexarc Software and its contributors](https://github.com/hexarc-software)