wezrule / fuser

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

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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:

  • bool
  • all standard integer and floating-point numeric types (checks if the value fits into the destination type)
  • 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)

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

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.

About

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

License:MIT License


Languages

Language:C++ 91.6%Language:CMake 8.4%