Home

Awesome

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:

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

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.