-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_from_json.cpp
More file actions
72 lines (61 loc) · 1.78 KB
/
Copy pathexample_from_json.cpp
File metadata and controls
72 lines (61 loc) · 1.78 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
#include <iostream>
#include <meta>
#include <ranges>
#include <vimg/from_json.hpp>
using namespace vi::from_json;
constexpr std::string_view json_1 = R"(
{
"foo": "lorem",
"bar": "ipsum",
"baz": "sit"
}
)";
// Define the type (cannot generate identifiers with reflection)
struct JsonType1;
consteval {
define_aggregate_from_json(^^JsonType1, json_1);
}
// The type is now complete with a definition.
constexpr std::string_view json_2 = R"(
{
"string_type": "string_value",
"int_type": 123,
"bool_type": true,
"null_type": null
}
)";
struct JsonType2;
consteval {
define_aggregate_from_json(^^JsonType2, json_2);
}
constexpr std::string_view json_3 = R"(
{
"foo": [1, 2, 3, 4],
"bar": {
"nested_key": "nested_val"
}
}
)";
template<typename T>
void inspect_json_type() {
constexpr auto type_name = std::meta::identifier_of(^^T);
std::cout << "=== " << type_name << " Type Information ===\n";
std::cout << "Sizeof(" << type_name << "): " << sizeof(^^T) << " bytes\n";
std::cout<< std::endl;
}
int main() {
inspect_json_type<JsonType1>();
constexpr JsonType1 obj1 = parse_json<JsonType1>(json_1);
std::cout << "obj1.foo = " << obj1.foo << '\n';
std::cout << "obj1.bar = " << obj1.bar << '\n';
std::cout << "obj1.baz = " << obj1.baz << '\n';
std::cout << std::endl;
inspect_json_type<JsonType2>();
JsonType2 obj2{.string_type = "string_value", .int_type = 123, .bool_type = true, .null_type = nullptr};
std::cout << "obj2.string_type = " << obj2.string_type << '\n';
std::cout << "obj2.int_type = " << obj2.int_type << '\n';
std::cout << "obj2.bool_type = " << obj2.bool_type << '\n';
std::cout << "obj2.null_type = " << obj2.null_type << '\n';
std::cout << std::endl;
return 0;
}