Giter Site home page Giter Site logo

abledbody / quickbin Goto Github PK

View Code? Open in Web Editor NEW
8.0 1.0 0.0 78 KB

A C#/Unity tool for quickly and easily serializing to and deserializing from binary.

License: Other

C# 100.00%
binary binary-serialization chaining csharp deserialization dotnet easy-to-use method-chaining minimal serialization serializer tools unity unity3d

quickbin's Introduction

QuickBin

QuickBin is intended to make binary serialization and deserialization nearly thoughtless.

Serialization and deserialization are made to be mirror images of each other, so it's easy to see what you're doing, and make sure you serialize and deserialize your data the exact same way. The heavy focus on method chaining makes it easy to read, and quick to implement.

Unity Installation

To install QuickBin, go to the package manager, press the plus button in the top left, select Add package by git URL... and enter https://github.com/abledbody/QuickBin.git

The Serializer class

The serializer is a wrapper for a List<byte>. Each time you call Serializer.Write it picks the appropriate overload method for the provided type, and adds the bytes to the list. Once you're ready to use the produced bytes, like to write them to a file, you can simply put the reference to the serializer into an argument or field of type byte[], and it will implicitly convert itself.

var helloWorld = "Hello, QuickBin!";

var buffer = new Serializer()
  .Write(10)
  .Write(18.5f)
  .Write(helloWorld.Length)
  .Write(helloWorld);

byte[] bytes = buffer;

The Deserializer class

The deserializer holds three pieces of information: A reference to a byte[] buffer, an int ReadIndex, and an int ForbiddenIndex. The deserializer will never mutate the byte array, and can effectively only read between ReadIndex and ForbiddenIndex. Every time you call Deserializer.Read, ReadIndex will increment by the number of bytes read for the specified type. If you attempt to read data beyond the end of the buffer, or beyond ForbiddenIndex, QuickBin will throw an exception.

Each overload for Deserializer.Read provides an out argument. Using initialization syntax or providing an existing typed field is how the deserializer selects the correct overload method for converting bytes into a type.

new Deserializer(bytes)
  .Read(out int firstNumber)
  .Read(out float secondNumber)
  .Read(out int helloWorldLength)
  .Read(out helloWorld, helloWorldLength);

Debug.Log($"firstNumber: {firstNumber}, secondNumber: {secondNumber}, helloWorld: {helloWorld}");

Custom extensions

Extending the serializer and deserializer is fairly easy. Just make a static class (I recommend it also be partial) and write a Write method for Serializer, and a Read method for Deserializer.

public static partial class QuickBinExtensions {
  public static Serializer Write(this Serializer buffer, ExampleClass value) => buffer
    .Write(value.foo)
    .Write(value.bar);
  
  public static Deserializer Read(this Deserializer buffer, out ExampleClass produced) => buffer
    .Read(out int foo)
    .Read(out double bar)
    .Return(new(foo, bar), out produced);
}

quickbin's People

Contributors

abledbody avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  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.