-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathjson.cc
More file actions
105 lines (94 loc) · 2.56 KB
/
json.cc
File metadata and controls
105 lines (94 loc) · 2.56 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <iostream>
#include <string>
#include <string_view>
#include <type_traits>
#include <utility>
#include <vector>
#include "bbai/base/reflection/member.h"
using namespace bbai;
template <class T>
requires std::is_integral_v<T>
void print_json(std::ostream& out, T x) noexcept { out << x; }
void print_json(std::ostream& out, std::string_view s) noexcept {
// Note: for a real implementation, we'd need to escape characters like "
// in s. But we're not going to worry about that for this example.
out << "\"" << s << "\"";
}
template <class T>
void print_json(std::ostream& out, const std::vector<T>& v) noexcept {
out << "[";
for (auto& val : v) {
print_json(out, val);
if (&val != &v.back()) {
out << ",";
}
}
out << "]";
}
namespace detail {
template <size_t I, size_t N, class T>
void print_json_member(std::ostream& out, const T& x) noexcept {
constexpr auto name = basrf::member_name_v<T, I>;
print_json(out, name);
out << ":";
print_json(out, basrf::member_get<I>(x));
if (I < N - 1) {
out << ",";
}
}
template <class T, size_t... Indexes>
void print_json_impl(std::ostream& out, const T& x,
std::index_sequence<Indexes...>) noexcept {
constexpr auto N = sizeof...(Indexes);
out << "{";
(print_json_member<Indexes, N>(out, x), ...);
out << "}";
}
} // namespace detail
template <class T>
requires(std::is_aggregate_v<T>&& basrf::num_members_v<T> >
0) void print_json(std::ostream& out, const T& x) noexcept {
detail::print_json_impl(out, x,
std::make_index_sequence<basrf::num_members_v<T>>{});
}
struct employee {
BBAI_REFLECT_MEMBER(name, std::string);
BBAI_REFLECT_MEMBER(salary, int);
};
struct team {
BBAI_REFLECT_MEMBER(department, std::string);
BBAI_REFLECT_MEMBER(employees, std::vector<employee>);
};
struct company {
BBAI_REFLECT_MEMBER(name, std::string);
BBAI_REFLECT_MEMBER(teams, std::vector<team>);
};
int main() {
company company{
.name{"Acme"},
.teams{
{
.department{"sales"},
.employees{
{
.name{"Daffy"},
.salary{70'000},
},
{
.name{"Dot"},
.salary{125'000},
},
},
},
{
.department{"engineering"},
.employees{
{.name{"Wakko"}, .salary{100'000}},
},
},
},
};
print_json(std::cout, company);
std::cout << "\n";
return 0;
}