https://github.com/nullsoftware/ubinaryserializer
Library that allows to serialize .NET objects to byte array (or deserialize from it).
https://github.com/nullsoftware/ubinaryserializer
Last synced: 4 months ago
JSON representation
Library that allows to serialize .NET objects to byte array (or deserialize from it).
- Host: GitHub
- URL: https://github.com/nullsoftware/ubinaryserializer
- Owner: nullsoftware
- License: mit
- Created: 2021-08-26T21:56:50.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2023-01-20T07:15:59.000Z (about 3 years ago)
- Last Synced: 2025-10-06T23:03:36.676Z (4 months ago)
- Language: C#
- Size: 153 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
[](https://stand-with-ukraine.pp.ua)
[](https://www.nuget.org/packages/UBinarySerializer/)
[](https://www.nuget.org/packages/UBinarySerializer/)
# UBinarySerializer
Framework for data binary serialization.
## Getting started.
Use one of the follwing methods to install and use this library:
- **Package Manager:**
```batch
PM> Install-Package UBinarySerializer
```
- **.NET CLI:**
```batch
> dotnet add package UBinarySerializer
```
----
To create serializer for specific object type,
you need to create `BinarySerializer<>` instance:
```C#
BinarySerializer serializer = new BinarySerializer();
```
Or to create unsafe serializer:
```C#
BinaryUnsafeSerializer serializer = new BinaryUnsafeSerializer();
```
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`:
```C#
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 Items { get; set; }
= new List();
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:
```C#
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:
```C#
[BinaryConverter(typeof(FourCharacterCodeConverter)/*, SerializerType = typeof(BinaryUnsafeSerializer) */)]
public struct FourCharacterCode : IEquatable
{
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);
}
}
```