UBinarySerializer 0.1.1.7-alpha
dotnet add package UBinarySerializer --version 0.1.1.7-alpha
NuGet\Install-Package UBinarySerializer -Version 0.1.1.7-alpha
<PackageReference Include="UBinarySerializer" Version="0.1.1.7-alpha" />
paket add UBinarySerializer --version 0.1.1.7-alpha
#r "nuget: UBinarySerializer, 0.1.1.7-alpha"
// Install UBinarySerializer as a Cake Addin #addin nuget:?package=UBinarySerializer&version=0.1.1.7-alpha&prerelease // Install UBinarySerializer as a Cake Tool #tool nuget:?package=UBinarySerializer&version=0.1.1.7-alpha&prerelease
UBinarySerializer
Framework for data binary serialization.
Getting started.
To create serializer for specific object type,
you need to create BinarySerializer<>
instance:
BinarySerializer<MyObject> serializer = new BinarySerializer<MyObject>();
Or to create unsafe serializer:
BinaryUnsafeSerializer<MyObject> serializer = new BinaryUnsafeSerializer<MyObject>();
Then to serialize object to binary data use Serialize
or SerializeObject
methods.
Difference between unsafe and safe serializers, is that safe serializer serializes the data safely,
allows to serialize null-reference objects and saves object data version (generation) for backward-compatibility.
Unsafe serializer optimized for fast serialization, and disallows null-references.
To control fields and properties during serialization use BinIndexAttribute
:
using System;
using NullSoftware.Serialization;
public class Player
{
[BinIndex(0)]
public int Health { get; set; }
[BinIndex(1, Generation = 2)]
public int Hunger { get; set; }
[BinIndex(2)]
public Vector3 Postion { get; set; }
[BinIndex(3)]
public GameMode GameMode { get; set; }
[BinIndex(4)]
public Texture Skin { get; set; }
[BinIndex(5)]
public List<Item> Items { get; set; }
= new List<Item>();
public int DeathsCount { get; set; } // will not be serialized.
}
Generation
allows to add new fields/properties for already serialized object without breaking serialization (in case if safe serializer was used).
If there is no BinIndexAttribute
specified in object will be serialized all not-readonly fields/properties.
Also can be used RequiredAttribute
from System.ComponentModel.DataAnnotations
to specify that current field/property can not have null-reference even in safe serialization.
Also there is possible to craete custom binary converter using IBinaryConverter
interface, and BinaryConverterAttribute
for target object.
Converter:
public class FourCharacterCodeConverter : IBinaryConverter
{
public void ToBytes(MemberInfo member, BinaryWriter stream, object value, object parameter)
{
stream.Write(((FourCharacterCode)value).Value);
}
public object ToValue(MemberInfo member, BinaryReader stream, object parameter)
{
return new FourCharacterCode(stream.ReadBytes(4));
}
}
Converter target:
[BinaryConverter(typeof(FourCharacterCodeConverter)/*, SerializerType = typeof(BinaryUnsafeSerializer) */)]
public struct FourCharacterCode : IEquatable<FourCharacterCode>
{
public byte[] Value { get; } // must be 4-bytes length
public FourCharacterCode(params byte[] value)
{
if (value == null) throw new ArgumentNullException();
if (value.Length != 4) throw new ArgumentOutOfRangeException();
Value = value;
}
public FourCharacterCode(string value)
{
if (value == null) throw new ArgumentNullException();
if (value.Length != 4) throw new ArgumentOutOfRangeException();
Value = Encoding.ASCII.GetBytes(value);
}
public override string ToString()
{
return Encoding.ASCII.GetString(Value);
}
public bool Equals(FourCharacterCode other)
{
return Value.SequenceEqual(other.Value);
}
public override bool Equals(object obj)
{
if (obj is FourCharacterCode fourCC)
return Equals(fourCC);
else
return false;
}
public override int GetHashCode()
{
return BitConverter.ToInt32(Value);
}
public static bool operator ==(FourCharacterCode left, FourCharacterCode right)
{
return left.Equals(right);
}
public static bool operator !=(FourCharacterCode left, FourCharacterCode right)
{
return !left.Equals(right);
}
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 is compatible. net5.0-windows was computed. net6.0 is compatible. 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 | netcoreapp3.0 is compatible. netcoreapp3.1 is compatible. |
.NET Framework | net452 is compatible. net46 is compatible. net461 is compatible. net462 is compatible. net463 was computed. net47 is compatible. net471 is compatible. net472 is compatible. net48 is compatible. net481 was computed. |
-
.NETCoreApp 3.0
- No dependencies.
-
.NETCoreApp 3.1
- No dependencies.
-
.NETFramework 4.5.2
- No dependencies.
-
.NETFramework 4.6
- No dependencies.
-
.NETFramework 4.6.1
- No dependencies.
-
.NETFramework 4.6.2
- No dependencies.
-
.NETFramework 4.7
- No dependencies.
-
.NETFramework 4.7.1
- No dependencies.
-
.NETFramework 4.7.2
- No dependencies.
-
.NETFramework 4.8
- No dependencies.
-
net5.0
- No dependencies.
-
net6.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 | |
---|---|---|---|
0.1.1.7-alpha | 122 | 1/20/2023 | |
0.0.3.2-alpha | 134 | 5/12/2022 | |
0.0.3.1-alpha | 145 | 5/1/2022 | |
0.0.3-alpha | 158 | 5/1/2022 | |
0.0.2.1-alpha | 150 | 5/1/2022 | |
0.0.2-alpha | 149 | 5/1/2022 | |
0.0.1-alpha | 159 | 4/23/2022 |
Reworked serializer. Now it separated to two different serializers.
Added abiality to specify custom converter using attribute.