-
-
Notifications
You must be signed in to change notification settings - Fork 7.5k
Expand file tree
/
Copy pathtest_empty_macro.cpp
More file actions
59 lines (50 loc) · 2.02 KB
/
Copy pathtest_empty_macro.cpp
File metadata and controls
59 lines (50 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Standalone compile test for issue #4041
// Tests that zero-member NLOHMANN_DEFINE_TYPE_* macros compile and work correctly.
#include <cassert>
#include <string>
#include <nlohmann/json.hpp>
// --- Intrusive (friend functions inside the class) ---
struct EmptyIntrusive
{
bool operator==(const EmptyIntrusive&) const { return true; }
NLOHMANN_DEFINE_TYPE_INTRUSIVE(EmptyIntrusive)
};
struct EmptyIntrusiveWithDefault
{
bool operator==(const EmptyIntrusiveWithDefault&) const { return true; }
NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT(EmptyIntrusiveWithDefault)
};
struct EmptyIntrusiveOnlySerialize
{
NLOHMANN_DEFINE_TYPE_INTRUSIVE_ONLY_SERIALIZE(EmptyIntrusiveOnlySerialize)
};
// --- Non-intrusive (free functions in the same namespace) ---
struct EmptyNonIntrusive
{
bool operator==(const EmptyNonIntrusive&) const { return true; }
};
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(EmptyNonIntrusive)
struct EmptyNonIntrusiveWithDefault
{
bool operator==(const EmptyNonIntrusiveWithDefault&) const { return true; }
};
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(EmptyNonIntrusiveWithDefault)
struct EmptyNonIntrusiveOnlySerialize {};
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_ONLY_SERIALIZE(EmptyNonIntrusiveOnlySerialize)
int main()
{
// to_json produces {}
assert(nlohmann::json(EmptyIntrusive{}).dump() == "{}");
assert(nlohmann::json(EmptyIntrusiveWithDefault{}).dump() == "{}");
assert(nlohmann::json(EmptyIntrusiveOnlySerialize{}).dump() == "{}");
assert(nlohmann::json(EmptyNonIntrusive{}).dump() == "{}");
assert(nlohmann::json(EmptyNonIntrusiveWithDefault{}).dump() == "{}");
assert(nlohmann::json(EmptyNonIntrusiveOnlySerialize{}).dump() == "{}");
// from_json round-trips successfully
nlohmann::json j = nlohmann::json::object();
auto e1 = j.get<EmptyIntrusive>(); (void)e1;
auto e2 = j.get<EmptyIntrusiveWithDefault>(); (void)e2;
auto e3 = j.get<EmptyNonIntrusive>(); (void)e3;
auto e4 = j.get<EmptyNonIntrusiveWithDefault>(); (void)e4;
return 0;
}