1-file header-only library for automatic (de)serialization of C++ types to/from JSON.
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*
andvoid const*
(converts tostd::uintptr_t
)std::string
std::array<T, N>
(checks if array size matches)std::vector<T>
std::deque<T>
std::unique_ptr<T>
(outputsnull
when there is no value)std::optional<T>
(outputsnull
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
.
- Boost >= 1.56 (only header-only libraries)
- nlohmann/json >= 3.0
- C++11
- C++17 (optional, to enable
std::optional
support) - CMake >= 3.13
#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
}
]
namespace fuser {
template <typename T>
nlohmann::json serialize(T const& val);
template <typename T>
T deserialize(nlohmann::json const& json);
}
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.
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.