As many others have said this is a pretty fantastic library.
I'm trying to serialize some enums and this works (with the right to_json and from_json functions defined but not shown here):
struct Foo_t
{
enum class color
{
none = -1,
black = 0,
red,
blue,
green
};
int number{0};
color foo_color{color::none};
};
NLOHMANN_JSON_SERIALIZE_ENUM(Foo_t::color,
{
{Foo_t::color::none, nullptr},
{Foo_t::color::black, "black"},
{Foo_t::color::red, "red"},
{Foo_t::color::blue, "blue"},
{Foo_t::color::green, "green"}
})
however if I want to use those string definitions independent of json then I have to define another mapping, something like
static const std::unordered_map<Foo_t::color, const std::string> color_map =
{
{Foo_t::color::none, ""},
{Foo_t::color::black, "black"},
{Foo_t::color::red, "red"},
{Foo_t::color::blue, "blue"},
{Foo_t::color::green, "green"}
};
and unfortunately this doesn't work:
NLOHMANN_JSON_SERIALIZE_ENUM(Foo_t::color, color_map)
because the macro is expecting something that can be converted to an array of std::pairs.
Is there way to not have to duplicate the definitions?
As many others have said this is a pretty fantastic library.
I'm trying to serialize some enums and this works (with the right to_json and from_json functions defined but not shown here):
however if I want to use those string definitions independent of json then I have to define another mapping, something like
and unfortunately this doesn't work:
because the macro is expecting something that can be converted to an array of std::pairs.
Is there way to not have to duplicate the definitions?