Enum default serialization
Lets say i have the following
enum class enum_t
{
foo = 0,
bar = 1,
};
template<>
struct glz::meta<enum_t>
{
using enum enum_t;
static constexpr auto value = enumerate(
"foo", foo,
"bar", bar
);
};
enum_t value = static_cast<enum_t>(100);
std::string buffer{};
glz::write_json(value , buffer);
expect(buffer == "100");
By default serialization falls back to int for enums, this can cause a lot of issues with parsers expecing strings.
My question is how can i serialize unknown enum values as for example "unknown"?
Similarly what happens if there is such an inconsistency in a json object, is there a way to skip that value?
Originally posted by @razaqq in https://github.com/stephenberry/glaze/discussions/1923
Thanks for this feedback. I'm actively developing better enum support for Glaze and will take these thoughts into consideration. I'll hopefully get back to you soon with more thoughts.
Relevant PR #1784
as a quick workaround, you could install enchantum and do
template<>
struct glz::meta<UrEnum> {
constexpr static auto value = enchantum::values<UrEnum>;
constexpr static auto keys = enchantum::names<UrEnum>;
};
Yes, enchantum works well with Glaze and simple_enum has built in support for Glaze.