https://github.com/akopetsch/byteserialization
A declarative serialization library designed for bit-level control over binary representation. You can define C# classes like C structs by assigning attributes to classes and properties. Inspired by BinarySerializer by Jeff Haynes.
https://github.com/akopetsch/byteserialization
binary buffer csharp dotnet library serialization structure
Last synced: 9 months ago
JSON representation
A declarative serialization library designed for bit-level control over binary representation. You can define C# classes like C structs by assigning attributes to classes and properties. Inspired by BinarySerializer by Jeff Haynes.
- Host: GitHub
- URL: https://github.com/akopetsch/byteserialization
- Owner: akopetsch
- License: mit
- Created: 2024-05-27T15:52:44.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-01-30T17:46:35.000Z (11 months ago)
- Last Synced: 2025-02-04T18:41:02.743Z (11 months ago)
- Topics: binary, buffer, csharp, dotnet, library, serialization, structure
- Language: C#
- Homepage:
- Size: 171 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ByteSerialization
[](https://nuget.org/packages/ByteSerialization)
A declarative serialization library designed for bit-level control over binary representation.
You can define C# classes like C structs by assigning attributes to classes and properties.
Inspired by [BinarySerializer](https://github.com/jefffhaynes/BinarySerializer) by Jeff Haynes.
This library was created from the ground up to support features like references/pointers.
Notably, this libary is used by [swe1r-assets](https://github.com/akopetsch/swe1r-assets).
## Example
Consider the following classes:
```csharp
public class Manufacturer
{
[Order(0)]
public byte NameLength { get; set; }
[Order(1), Length(nameof(NameLength))]
public char[] Name { get; set; }
}
public class Car
{
[Order(0)]
public byte NameLength { get; set; }
[Order(1), Length(nameof(NameLength))]
public char[] Name { get; set; }
[Order(2), Reference]
public Manufacturer Manufacturer { get; set; }
}
```
Create an object graph like this:
```csharp
var mf = new Manufacturer() { NameLength = 2, Name = "MF".ToArray() };
var car = new Car() { NameLength = 4, Name = "Car1".ToArray(), Manufacturer = mf };
using (var ms = new MemoryStream())
{
new ByteSerializer().Serialize(ms, car, Endianness.BigEndian);
File.WriteAllBytes("car.bin", ms.ToArray());
}
```
That gives you the following binary output:
```console
$ xxd car.bin
00000000: 0443 6172 3100 0000 0902 4d46 .Car1.....MF
```
| Bytes | Property | Value |
|-------------|------------------|-------------------|
| 04 | car.NameLength | 4 |
| 43 61 72 31 | car.Name | "Car1" in ASCII |
| 00 00 00 09 | car.Manufacturer | (pointer to mf) |
| 02 | mf.NameLength | 2 |
| 4d 46 | mf.Name | "MF" in ASCII |