glaze icon indicating copy to clipboard operation
glaze copied to clipboard

Enum default serialization

Open razaqq opened this issue 6 months ago • 3 comments

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

razaqq avatar Sep 13 '25 11:09 razaqq

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.

stephenberry avatar Sep 14 '25 13:09 stephenberry

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>;
    
};

ZXShady avatar Sep 18 '25 10:09 ZXShady

Yes, enchantum works well with Glaze and simple_enum has built in support for Glaze.

stephenberry avatar Sep 18 '25 13:09 stephenberry