GithubHelp home page GithubHelp logo

binaryconversion's Introduction

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 Types and Assemblys
  • 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:

  1. You have no control over how an object is created before it is deserialized.
  2. It can only be used with classes and structs; interfaces cannot implement it.
  3. It cannot be used in external classes that you do not have control over.
  4. 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);
	}
}

binaryconversion's People

Contributors

teamdoodz avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.