Simple C++ 20 Serialization Library that works out of the box with aggregate types!

Overview

MSVC Unit Tests

BinaryLove3

Simple C++ 20 Serialization Library that works out of the box with aggregate types!

Requirements

BinaryLove3 is a c++20 only library. Library can serialize only types matching these requirements:

  • trivial types (e.g. uint32_t, struct { uint32_t a; float_t b; }):

    Types matching requirements for std::is_trivial.

  • agregate types (e.g. std::pair, struct { uint32_t a; std::vector b; }):

    Types matching requirements for std::is_aggregate.

  • iterable types (e.g. std::vector):

    Type is required to be compatible with std::begin, std::end and std::inserter. Additionally member type value_type is also required.

    If type's iterator fulfils std::random_access_iterator requirement and value_type matches requirement is_trivial then optimisations will be made.

  • types providing specializations for BinaryLove3::serialize and BinaryLove3::deserialize. Refer to Providing custom serialization methods

Usage

// demo.cpp

#include <iostream>
#include <vector>
#include <string>
#include <cstdint>
#include <string>
#include <list>

#include "BinaryLove3.hpp"

struct foo
{
	uint32_t v0 = 3;
	uint32_t v1 = 2;
	float_t v2 = 2.5f;
	char v3 = 'c';
	struct
	{
		std::vector<int> vec_of_trivial = { 1, 2, 3 };
		std::vector vec_of_nontrivial = { "I am a Fox!", "In a big Box!" };
		std::string str = "Foxes can fly!";
		std::list<int> non_random_access_container = { 3, 4, 5 };
	} non_trivial;
	struct
	{
		uint32_t v0 = 1;
		uint32_t v1 = 2;
	} trivial;
};

auto main() -> int32_t
{
	foo out = { 4, 5, 6.7f, 'd', {{5, 4, 3, 2}, {"cc", "dd"}, "Fly me to the moon..." , {7, 8, 9}}, {3, 4} };
	auto data = BinaryLove3::serialize(bobux);
	
	foo in;
	BinaryLove3::deserialize(data, in);
	return int32_t(0);
}

Providing custom serialization methods.

One can provide custom serialization methods by creating specializations for the following functions:

  • std::vector serialize(const T& var_)
  • bool deserialize(const std::byte*& cur_, const std::byte* end_, T& obj_)
#include <BinaryLove3>

class foo
{
  friend std::vector serialize(const foo& var_);
  friend bool deserialize(const std::byte*& cur_, const std::byte* end_, foo& obj_);
private:
  uint32_t m_a;
  std::vector<uint32_t> m_b;
public:
  void foo_method();
}

std::vector serialize(const foo& var_)
{
  auto ret = BinaryLove3::serialize(var_.m_a);
  auto data = BinaryLove3::serialize(var_.m_b);
  ret.insert(std::end(insert), std::begin(data), std::end(data));
  return ret;
}

bool deserialize(const std::byte*& cur_, const std::byte* end_, foo& obj_)
{
  uint32_t a;
  std::vector<uint32_t> b;
  if(!BinaryLove3::deserialize(cur_, end_, a))
    return false;
  
  if(!BinaryLove3::deserialize(cur_, end_, b))
    return false;
    
  obj_.m_a = std::move(a);
  obj_.m_b = std::move(b);
  
  return true;
}

Notes

This library works only with up to 10 member variables of an aggregate type right now. This will be expanded in the near future. Cheers!

You might also like...
Fast Binary Encoding is ultra fast and universal serialization solution for C++, C#, Go, Java, JavaScript, Kotlin, Python, Ruby, Swift

Fast Binary Encoding (FBE) Fast Binary Encoding allows to describe any domain models, business objects, complex data structures, client/server request

Yet Another Serialization
Yet Another Serialization

YAS Yet Another Serialization - YAS is created as a replacement of boost.serialization because of its insufficient speed of serialization (benchmark 1

Binary Serialization

Binn Binn is a binary data serialization format designed to be compact, fast and easy to use. Performance The elements are stored with their sizes to

An implementation of the MessagePack serialization format in C / msgpack.org[C]

CMP CMP is a C implementation of the MessagePack serialization format. It currently implements version 5 of the MessagePack Spec. CMP's goal is to be

MPack - A C encoder/decoder for the MessagePack serialization format / msgpack.org[C]

Introduction MPack is a C implementation of an encoder and decoder for the MessagePack serialization format. It is: Simple and easy to use Secure agai

Yet Another Serialization
Yet Another Serialization

YAS Yet Another Serialization - YAS is created as a replacement of boost.serialization because of its insufficient speed of serialization (benchmark 1

universal serialization engine

A Universal Serialization Engine Based on compile-time Reflection iguana is a modern, universal and easy-to-use serialization engine developed in c++1

A lightweight C++20 serialization framework

zpp::bits A modern C++20 binary serialization library, with just one header file. This library is a successor to zpp::serializer. The library tries to

Owner
RedSkittleFox
Highschool Student, C++ and CS enthusiast.
RedSkittleFox
Serialization framework for Unreal Engine Property System that just works!

DataConfig Serialization framework for Unreal Engine Property System that just works! Unreal Engine features a powerful Property System which implemen

null 81 Dec 19, 2022
Cista is a simple, high-performance, zero-copy C++ serialization & reflection library.

Simple C++ Serialization & Reflection. Cista++ is a simple, open source (MIT license) C++17 compatible way of (de-)serializing C++ data structures. Si

Felix G√ľndling 1.1k Jan 2, 2023
Your binary serialization library

Bitsery Header only C++ binary serialization library. It is designed around the networking requirements for real-time data delivery, especially for ga

Mindaugas Vinkelis 771 Jan 2, 2023
Cap'n Proto serialization/RPC system - core tools and C++ library

Cap'n Proto is an insanely fast data interchange format and capability-based RPC system. Think JSON, except binary. Or think Protocol Buffers, except

Cap'n Proto 9.5k Jan 1, 2023
A C++11 library for serialization

cereal - A C++11 library for serialization cereal is a header-only C++11 serialization library. cereal takes arbitrary data types and reversibly turns

iLab @ USC 3.4k Jan 3, 2023
FlatBuffers: Memory Efficient Serialization Library

FlatBuffers FlatBuffers is a cross platform serialization library architected for maximum memory efficiency. It allows you to directly access serializ

Google 19.7k Jan 9, 2023
Zmeya is a header-only C++11 binary serialization library designed for games and performance-critical applications

Zmeya Zmeya is a header-only C++11 binary serialization library designed for games and performance-critical applications. Zmeya is not even a serializ

Sergey Makeev 99 Dec 24, 2022
CppSerdes is a serialization/deserialization library designed with embedded systems in mind

A C++ serialization/deserialization library designed with embedded systems in mind

Darren V Levine 79 Nov 5, 2022
C++17 library for all your binary de-/serialization needs

blobify blobify is a header-only C++17 library to handle binary de-/serialization in your project. Given a user-defined C++ struct, blobify can encode

Tony Wasserka 247 Dec 8, 2022
Yet another JSON/YAML/BSON serialization library for C++.

ThorsSerializer Support for Json Yaml Bson NEW Benchmark Results Conformance mac linux Performance max linux For details see: JsonBenchmark Yet anothe

Loki Astari 281 Dec 10, 2022