BinaryConversion 1.1.0
dotnet add package BinaryConversion --version 1.1.0
NuGet\Install-Package BinaryConversion -Version 1.1.0
<PackageReference Include="BinaryConversion" Version="1.1.0" />
paket add BinaryConversion --version 1.1.0
#r "nuget: BinaryConversion, 1.1.0"
// Install BinaryConversion as a Cake Addin
#addin nuget:?package=BinaryConversion&version=1.1.0
// Install BinaryConversion as a Cake Tool
#tool nuget:?package=BinaryConversion&version=1.1.0
BinaryConversion
A binary serialization library for .NET
The Problem
Imagine you're trying to save some data to a file:
public class CharacterData {
public string Name = "John Doe";
public int Level = 0;
}
You've looked at all the different serialization methods and decide to go with binary. Here is some possible code that could serialize/deserialize the CharacterData
class:
public void Serialize(BinaryWriter writer) {
writer.Write(this.Name);
writer.Write(this.Level);
}
public void Deserialize(BinaryReader reader) {
this.Name = reader.ReadString();
this.Level = reader.ReadInt32();
}
This code works fine; however, after working on your code more you've added a new field to the class:
public class CharacterData {
public string Name = "John Doe";
public int Level = 0;
public List<ItemInfo> Inventory = new();
}
Although you could add support for this field in your Serialize
/Deserialize
methods, you would have to write serializers for the ItemInfo
type, as well as any other types it may have as fields, any types those types may have, and so on.
As you add more and more fields to your CharacterData
class, writing serializers for each and every one of them can get very time consuming.
The Solution
BinaryConversion allows you to easily automate all of your binary serialization. For example, if you wanted to save/load an instance of CharacterData
to/from disk, you would only have to do the following:
CharacterData steve = new CharacterData();
steve.Name = "Steve";
steve.Level = 2;
// Save
BinaryConvert.SaveToFile(steve, "Steve.character");
steve.Level = 3;
// Load
steve = BinaryConvert.LoadFromFile<CharacterData>("Steve.character");
Console.WriteLine(steve.Level); // 2
Supported Types
By default, BinaryConversion has support for the following types:
- All core C# types (
int
,bool
, etc) - Nullable structs (
int?
,bool?
, etc) - Arrays
- Any type that implements the
ICollection<T>
interface - References to
Type
s andAssembly
s KeyValuePair<TKey,TValue>
DateTime
If you try to convert a type not listed here, the serializer will try to automatically convert it based on its public fields.
The IBinarySerializable Interface
IBinarySerializable
allows classes to define their own custom methods for serialization. For example, here is how the CharacterData
class could implement this interface:
public class CharacterData : IBinarySerializable {
public string Name = "John Doe";
public int Level = 0;
public List<ItemInfo> Inventory = new();
public void Serialize(BinaryWriter writer, BinarySerializer serializer) {
writer.Write(this.Name);
writer.Write(this.Level);
serializer.ToBinary(this.Inventory, writer); // Allow the library to automatically convert the Inventory collection.
}
public void Deserialize(BinaryReader reader, BinarySerializer serializer) {
this.Name = reader.ReadString();
this.Level = reader.ReadInt32();
this.Inventory = serializer.FromBinary<List<ItemInfo>>(reader);
}
}
Custom Converters
Although IBinarySerializable
is easy to use, it has some downsides:
- You have no control over how an object is created before it is deserialized.
- It can only be used with classes and structs; interfaces cannot implement it.
- It cannot be used in external classes that you do not have control over.
- Classes that implement the interface gain a dependency to the library, and won't compile without it.
If any of these downsides are a detriment, you can use a custom converter instead. Here is the previous example using IBinarySerializable
, adapted into a converter:
public class CharacterDataBinaryConverter : BinaryConverter {
public override bool CanRead(Type type, BinarySerializerSettings settings) => type == typeof(CharacterDataBinaryConverter);
public override bool CanWrite(Type type, BinarySerializerSettings settings) => type == typeof(CharacterDataBinaryConverter);
public override void Write(BinaryWriter writer, Type returnType, object? value, BinarySerializer serializer) {
CharacterData character = (value as CharacterData) ?? throw new Exception("Value is not a CharacterData.");
writer.Write(character.Name);
writer.Write(character.Level);
serializer.ToBinary(character.Inventory, writer); // Allow the library to automatically convert the Inventory collection.
}
public override object Reader(BinaryReader reader, Type returnType, BinarySerializer serializer) {
CharacterData character = new CharacterData();
character.Name = reader.ReadString();
character.Level = reader.ReadInt32();
character.Inventory = serializer.FromBinary<List<ItemInfo>>(reader);
}
}
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. |
.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.