GenPack 1.0.0
dotnet add package GenPack --version 1.0.0
NuGet\Install-Package GenPack -Version 1.0.0
<PackageReference Include="GenPack" Version="1.0.0" />
<PackageVersion Include="GenPack" Version="1.0.0" />
<PackageReference Include="GenPack" />
paket add GenPack --version 1.0.0
#r "nuget: GenPack, 1.0.0"
#:package GenPack@1.0.0
#addin nuget:?package=GenPack&version=1.0.0
#tool nuget:?package=GenPack&version=1.0.0
<img src="logo.png" width="128" height="128">
GenPack
GenPack is a library that uses the .NET source generator to automatically generate packets as classes once you define a schema for the packets. It's easy to use and the results are useful.
GenPack also works well with Native AOT. You can take advantage of the benefits of Native AOT.
Simple to use[GenPackable]
public partial record PeoplePacket
{
public readonly static PacketSchema Schema = PacketSchemaBuilder.Create()
.@short("Age", "Age description")
.@string("Name", "Name description")
.Build();
}
The following code is automatically generated by the schema information.
public partial record PeoplePacket : GenPack.IGenPackable
{
/// <summary>
/// Age description
/// </summary>
public short Age { get; set; }
/// <summary>
/// Name description
/// </summary>
public string Name { get; set; } = string.Empty;
public byte[] ToPacket()
{
using var ms = new System.IO.MemoryStream();
ToPacket(ms);
return ms.ToArray();
}
public void ToPacket(System.IO.Stream stream)
{
using var writer = new GenPack.IO.EndianAwareBinaryWriter(stream, GenPack.UnitEndian.Little, GenPack.StringEncoding.UTF8);
writer.Write(Age);
writer.Write(Name);
}
public static PeoplePacket FromPacket(byte[] data)
{
using var ms = new System.IO.MemoryStream(data);
return FromPacket(ms);
}
public static PeoplePacket FromPacket(System.IO.Stream stream)
{
PeoplePacket result = new PeoplePacket();
using var reader = new GenPack.IO.EndianAwareBinaryReader(stream, GenPack.UnitEndian.Little, GenPack.StringEncoding.UTF8);
int size = 0;
byte[] buffer = null;
result.Age = reader.ReadInt16();
result.Name = reader.ReadString();
return result;
}
}
It's simple to use. You can binary serialize with ToPacket()
and deserialize with FromPacket()
.
var p = new PeoplePacket()
{
Age = 10,
Name = "John"
};
var data = p.ToPacket();
var newP = PeoplePacket.FromPacket(data);
Console.WriteLine(newP);PeoplePacket { Age = 10, Name = John }
How to create a packet schema
Decorate the attribute of class
or record
with GenPackable
. At this point, the target must be given partial
.
GenPack's packet schema is represented by creating a PacketSchema
using the PacketSchemaBuilder
.
[GenPackable]
public partial record PeoplePacket
{
public readonly static PacketSchema Schema = PacketSchemaBuilder.Create()
.@short("Age", "Age description")
.@string("Name", "Name description")
.Build();
}
The format beginning with @
means the schema property to be created. For example, @short("Age", "Age description")
gives the Age
property the type short
and the description Age description
.
This translates to the following,
/// <summary>
/// Age description
/// </summary>
public short Age { get; set; }You can then use the auto-generated properties.
var p = new PeoplePacket()
p.Age = 32;
Schema Properties
Property | Description | Bits | Arguments |
---|---|---|---|
@byte | byte | 8 | property name, description |
@sbyte | signed byte | 8 | property name, description |
@short | short int | 16 | property name, description |
@ushort | unsigned short int | 16 | property name, description |
@int | int | 32 | property name, description |
@uint | unsigned int | 32 | property name, description |
@long | long int | 64 | property name, description |
@ulong | unsigned long int | 64 | property name, description |
@float | single float | 32 | property name, description |
@double | double float | 64 | property name, description |
@string | string | N | property name, description |
@object<type> | genpackable object | N | property name, description |
@list<type> | variable list | N | property name, [sizeMode], description |
@dict<type> | variable dictionary | N | property name, [sizeMode], description |
@array<type> | fixed array | N | property name, size, description |
Size Modes for Lists and Dictionaries
GenPack supports multiple size encoding modes for @list
and @dict
to optimize for different use cases:
[GenPackable]
public partial record SizeModeExamplePacket
{
public readonly static PacketSchema Schema = PacketSchemaBuilder.Create()
// Default: Variable 7-bit encoding (most space efficient for small sizes)
.@list<int>("DefaultList", "Uses variable 7-bit encoding")
// Fixed 8-bit encoding (max 255 items)
.@list<string>("SmallList", SizeMode.Fixed8Bit, "Max 255 items")
// Fixed 16-bit encoding (max 65,535 items)
.@dict<int>("MediumDict", SizeMode.Fixed16Bit, "Max 65,535 items")
// Fixed 32-bit encoding (max 2,147,483,647 items)
.@list<byte>("LargeList", SizeMode.Fixed32Bit, "Very large lists")
.Build();
}
SizeMode Options
Size Mode | Bytes | Maximum Items | Use Case |
---|---|---|---|
Variable7Bit | 1-5 | 2,147,483,647 | Default, space efficient for small sizes |
Fixed8Bit | 1 | 255 | Small collections, embedded systems |
Fixed16Bit | 2 | 65,535 | Medium collections, protocol compatibility |
Fixed32Bit | 4 | 2,147,483,647 | Large collections, performance critical |
Benefits of Different Size Modes
- Variable7Bit: Most space efficient for collections with < 128 items
- Fixed8Bit: Consistent 1-byte overhead, ideal for embedded systems
- Fixed16Bit: Common in network protocols, predictable size
- Fixed32Bit: Better performance for large collections, no variable decoding
Tasks
- Support for Endian, string Encoding.
- Support for checksums.
- Support 8-bit, 16-bit, 32-bit, or variable 7-bit sizes for
@list
and@dict
. - Add
@ver
property to allow revision control of packets. - Automatically select and deserialize target structures based on packet command(identification code).
- Generate JSON and gRPC schema with
PacketSchema
. - Process device packets with uncomplicated packet structures.
- Process structures with complex packets, such as PLCs.
- Process packets that require speed, such as
MemoryPack
.
<img src="logo.png" width="32" height="32">
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. 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. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.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 |
---|---|---|
1.0.0 | 113 | 7/16/2025 |
0.9.0-preview1 | 103 | 4/3/2024 |