Header-only library for automatic (de)serialization of C++ types to/from JSON.

Overview

fuser

1-file header-only library for automatic (de)serialization of C++ types to/from JSON.

how it works

The library has a predefined set of (de)serializers for common types:

  • std::nullptr_t
  • bool
  • all standard integer (std::int*_t, std::uint*_t) and floating-point (float, double, long double) numeric types (checks if the value fits into the destination type)
  • char is not explicitly implemented, but it will very likely fall into one of fixed-width integer types
  • char8_t, char16_t and char32_t are not implemented
  • void* and void const* (converts to std::uintptr_t)
  • std::string
  • std::array<T, N> (checks if array size matches)
  • std::vector<T>
  • std::deque<T>
  • std::unique_ptr<T> (outputs null when there is no value)
  • std::optional<T> (outputs null when there is no value)
  • std::map<K, V, C>
  • std::unordered_map<K, V, H, E>

Additionally, it supports (de)serializers for structures which are valid boost fusion sequences.

Note that the above list is exactly how template specializations are implemented. The allocator template parameter is intentionally ommited because the current implementation has no guuarantees for stateful allocators. You can always define your (partial) specializations that reuse the existing serializers - just check how they are implemented.

If a type has no (de)serializer, the library will gracefully fail on a static_assert.

requirements

  • Boost >= 1.56 (only header-only libraries)
  • nlohmann/json >= 3.0
  • C++11
  • C++17 (optional, to enable std::optional support)
  • CMake >= 3.13

example

#include <fuser/fuser.hpp>

#include <vector>
#include <optional>
#include <iostream>

struct my_struct
{
	int i;
	double d;
	std::optional<bool> b;
};
// note: fusion macros must be used in global scope
BOOST_FUSION_ADAPT_STRUCT(my_struct, i, d, b)

int main()
{
	std::vector<my_struct> v = {
		{0, 3.14, {true}}, {1, 0.125, {false}}, {0, 0.0, {std::nullopt}}
	};

	std::cout << fuser::serialize(v).dump(4, ' ');
}

output:

[
    {
        "b": true,
        "d": 3.14,
        "i": 0
    },
    {
        "b": false,
        "d": 0.125,
        "i": 1
    },
    {
        "b": null,
        "d": 0.0,
        "i": 0
    }
]

library API

basic functions

namespace fuser {

template <typename T>
nlohmann::json serialize(T const& val);

template <typename T>
T deserialize(nlohmann::json const& json);

}

defining your own (de)serializer

template <>
struct fuser::serializer<my_type>
{
	static nlohmann::json serialize(my_type const& val)
	{
		/* ... */
	}
};

template <>
struct fuser::deserializer<my_type>
{
	static my_type deserialize(nlohmann::json const& json)
	{
		/* ... */
	}
};

Both class templates have a second unused template parameter to allow easy SFINAE for specializations.

trivia

The library is named "fuser" because it is a simple merge of boost fusion and serialization.

Special thanks to cycfi/json for the original idea.

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

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

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

Comments
  •  Add install target and export cmake config

    Add install target and export cmake config

    Add an alias fuser::fuster to match the installed target name. Then the usage in the downstream project is the same for subdirectory and installed target.

    Create and install a fuserConfig.cmake file for find_package(fuser). The config target searches for dependencies if the required targets are not yet present.

    Install the header file.

    The second commit is the optional Hunter dependency manager support also provided as separate PR https://github.com/Xeverous/fuser/pull/1

    opened by NeroBurner 11
  • Add optional Hunter support to provide dependencies

    Add optional Hunter support to provide dependencies

    Use https://github.com/cpp-pm/hunter as optional pure CMake dependency manager. To wake up Hunter and tell it to provide the dependencies for your project just add -DHUNTER_ENABLED=ON to your cmake configuration line

    opened by NeroBurner 4
Owner
/u/Xeverous on reddit | personal webiste with tons of C++ quality content (guides, tutorials, reference, etc) soon...
null
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
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
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
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
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
Microsoft 2.5k Dec 31, 2022