Skip to content

Commit 62543e4

Browse files
Make sure that flag enum with value zero are correctly serialized; fixes #640
1 parent dd4c191 commit 62543e4

2 files changed

Lines changed: 41 additions & 2 deletions

File tree

include/rfl/enums.hpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
#ifndef RFL_ENUMS_HPP_
22
#define RFL_ENUMS_HPP_
33

4-
#include <string>
54
#include <sstream>
5+
#include <string>
66

77
#include "Result.hpp"
88
#include "internal/enums/get_enum_names.hpp"
99
#include "internal/strings/strings.hpp"
10-
#include "thirdparty/enchantum/enchantum.hpp"
1110
#include "thirdparty/enchantum/bitflags.hpp"
11+
#include "thirdparty/enchantum/enchantum.hpp"
1212

1313
namespace rfl {
1414

@@ -77,6 +77,9 @@ std::string enum_to_string(const EnumType _enum) {
7777
++i;
7878
val >>= 1;
7979
}
80+
if (flags.empty()) {
81+
return "0";
82+
}
8083
return internal::strings::join("|", flags);
8184
} else {
8285
return to_string_or_number(_enum);

tests/json/test_enum8.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include <gtest/gtest.h>
2+
3+
#include <cassert>
4+
#include <rfl.hpp>
5+
#include <rfl/json.hpp>
6+
7+
#include "write_and_read.hpp"
8+
9+
namespace test_enum8 {
10+
11+
enum class TestEnum : uint64_t {
12+
None = 0,
13+
Hello = 1 << 0,
14+
World = 1 << 1,
15+
HelloWorld = Hello | World
16+
};
17+
18+
TestEnum operator|(TestEnum a, TestEnum b) noexcept {
19+
return static_cast<TestEnum>(static_cast<uint64_t>(a) |
20+
static_cast<uint64_t>(b));
21+
}
22+
23+
struct SomeClass {
24+
TestEnum e;
25+
TestEnum f;
26+
TestEnum g;
27+
};
28+
29+
TEST(json, test_enum8) {
30+
SomeClass t{
31+
.e = TestEnum::None, .f = TestEnum::Hello, .g = TestEnum::HelloWorld};
32+
33+
write_and_read(t, R"({"e":"0","f":"Hello","g":"Hello|World"})");
34+
}
35+
36+
} // namespace test_enum8

0 commit comments

Comments
 (0)