BitSerialization 1.0.1
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package BitSerialization --version 1.0.1
NuGet\Install-Package BitSerialization -Version 1.0.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="BitSerialization" Version="1.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="BitSerialization" Version="1.0.1" />
<PackageReference Include="BitSerialization" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add BitSerialization --version 1.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: BitSerialization, 1.0.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package BitSerialization@1.0.1
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=BitSerialization&version=1.0.1
#tool nuget:?package=BitSerialization&version=1.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
BitSerializer
A lightweight, binary serializer for .NET that converts objects to and from a compact binary format.
Features
- Binary format — compact serialization with a magic number header and SHA256 field-name verification
- Type-safe — generic
Serialize<T>/Deserialize<T>API with compile-time type safety - Flexible — supports
Streamandbyte[]I/O, sync and async - Order control — use
[Order(n)]to control field serialization order - Self-contained — no dependencies beyond .NET 8+
Supported types
| FieldType | .NET Type |
|---|---|
Bool |
bool |
Byte |
byte |
SByte |
sbyte |
Char |
char |
Int16 |
short |
UInt16 |
ushort |
Int32 |
int |
UInt32 |
uint |
Int64 |
long |
UInt64 |
ulong |
Single |
float |
Double |
double |
Decimal |
decimal |
String |
string |
ByteArray |
byte[] |
DateTime |
DateTime |
TimeSpan |
TimeSpan |
Guid |
Guid |
IntPtr |
nint / nuint |
Enum |
Any enum |
Note:
ObjectandArraytypes are not yet supported for serialization.
Usage
Basic serialization
using BitSerialization;
public class Player
{
[Order(1)]
public string Name = "hero";
[Order(2)]
public int Health = 100;
[Order(3)]
public float Speed = 5.5f;
}
var player = new Player { Name = "Aria", Health = 100, Speed = 5.5f };
// Serialize to bytes
byte[] data = BitSerializer.Serialize(player);
// Deserialize back
Player restored = BitSerializer.Deserialize<Player>(data);
Serialize to a stream
using var stream = new MemoryStream();
BitSerializer.SerializeToStream(player, stream, leaveOpen: true);
stream.Position = 0;
var fromStream = BitSerializer.Deserialize<Player>(stream);
Serialize to a file
byte[] data = BitSerializer.Serialize(player);
File.WriteAllBytes("save.bin", data);
var loaded = BitSerializer.Deserialize<Player>(File.ReadAllBytes("save.bin"));
Async stream serialization
await BitSerializer.SerializeToStreamAsync(player, stream, leaveOpen: true);
Binary format
┌─────────────────────────────┐
│ Magic number (3 bytes) │ 0x10 0x20 0x30
├─────────────────────────────┤
│ Field count (int32) │
├─────────────────────────────┤
│ ┌─ Field ─────────────────┐ │
│ │ Order (int32) │ │
│ │ NameHash (SHA256) │ │
│ │ FieldType (byte) │ │
│ │ Value (variable) │ │
│ └─────────────────────────┘ │
│ ... │
└─────────────────────────────┘
- Magic number:
0x10 0x20 0x30— identifies a valid BitSerializer payload - Field count: 32-bit signed integer
- NameHash: SHA-256 hash of the field name (used to match fields during deserialization)
- FieldType: A single byte identifying the data type (see
FieldTypeenum) - Value: Serialized according to its
FieldType
Value encoding
| Type | Encoding |
|---|---|
bool |
1 byte |
byte |
1 byte |
int32 |
4 bytes, little-endian |
string |
Length-prefixed (7-bit encoded) UTF-8 |
byte[] |
4-byte length prefix + raw bytes |
DateTime |
8 bytes (Ticks, little-endian) |
Guid |
16 bytes |
| (others) | Standard BinaryWriter / BinaryReader encoding |
Order attribute
Fields are serialized in declaration order by default. Use [Order(n)] to specify a custom position:
[Order(2)]
public string Name = "default";
[Order(1)]
public int Id;
Fields without [Order] are assigned incrementing order values starting from the last explicit order (or 0).
Requirements
- .NET 8.0 or later
License
MIT
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. 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. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net8.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.