diff --git a/include/rfl/enums.hpp b/include/rfl/enums.hpp index 1829a75c..b913b6bd 100644 --- a/include/rfl/enums.hpp +++ b/include/rfl/enums.hpp @@ -1,14 +1,14 @@ #ifndef RFL_ENUMS_HPP_ #define RFL_ENUMS_HPP_ -#include #include +#include #include "Result.hpp" #include "internal/enums/get_enum_names.hpp" #include "internal/strings/strings.hpp" -#include "thirdparty/enchantum/enchantum.hpp" #include "thirdparty/enchantum/bitflags.hpp" +#include "thirdparty/enchantum/enchantum.hpp" namespace rfl { @@ -77,6 +77,9 @@ std::string enum_to_string(const EnumType _enum) { ++i; val >>= 1; } + if (flags.empty()) { + return "0"; + } return internal::strings::join("|", flags); } else { return to_string_or_number(_enum); diff --git a/tests/json/test_enum8.cpp b/tests/json/test_enum8.cpp new file mode 100644 index 00000000..b64a4eb6 --- /dev/null +++ b/tests/json/test_enum8.cpp @@ -0,0 +1,36 @@ +#include + +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_enum8 { + +enum class TestEnum : uint64_t { + None = 0, + Hello = 1 << 0, + World = 1 << 1, + HelloWorld = Hello | World +}; + +TestEnum operator|(TestEnum a, TestEnum b) noexcept { + return static_cast(static_cast(a) | + static_cast(b)); +} + +struct SomeClass { + TestEnum e; + TestEnum f; + TestEnum g; +}; + +TEST(json, test_enum8) { + SomeClass t{ + .e = TestEnum::None, .f = TestEnum::Hello, .g = TestEnum::HelloWorld}; + + write_and_read(t, R"({"e":"0","f":"Hello","g":"Hello|World"})"); +} + +} // namespace test_enum8