generated from cpp-best-practices/gui_starter_template
-
-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathjson2cpp.cpp
More file actions
234 lines (172 loc) · 7.9 KB
/
json2cpp.cpp
File metadata and controls
234 lines (172 loc) · 7.9 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*
MIT License
Copyright (c) 2022 Jason Turner
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "json2cpp.hpp"
#include <algorithm>
#include <cctype>
#include <fmt/std.h>
#include <fstream>
std::string compile(const nlohmann::json &value, std::size_t &obj_count, std::vector<std::string> &lines)
{
const auto current_object_number = obj_count++;
const auto json_string = [](const auto &str) { return fmt::format("R\"string({})string\"", str); };
if (value.is_object()) {
std::vector<std::string> pairs;
for (auto itr = value.begin(); itr != value.end(); ++itr) {
pairs.push_back(
fmt::format("value_pair_t{{{}, {{{}}}}},", json_string(itr.key()), compile(*itr, obj_count, lines)));
}
lines.push_back(fmt::format(
"inline constexpr std::array<value_pair_t, {}> object_data_{} = {{", pairs.size(), current_object_number));
std::transform(pairs.begin(), pairs.end(), std::back_inserter(lines), [](const auto &pair) {
return fmt::format(" {}", pair);
});
lines.emplace_back("};");
return fmt::format("object_t{{object_data_{}}}", current_object_number);
} else if (value.is_array()) {
std::vector<std::string> entries;
std::transform(value.begin(), value.end(), std::back_inserter(entries), [&](const auto &child) {
return fmt::format("{{{}}},", compile(child, obj_count, lines));
});
lines.push_back(fmt::format(
"inline constexpr std::array<json, {}> object_data_{} = {{{{", entries.size(), current_object_number));
std::transform(entries.begin(), entries.end(), std::back_inserter(lines), [](const auto &entry) {
return fmt::format(" {}", entry);
});
lines.emplace_back("}};");
return fmt::format("array_t{{object_data_{}}}", current_object_number);
} else if (value.is_number_float()) {
return fmt::format("double{{{}}}", value.get<double>());
} else if (value.is_number_unsigned()) {
return fmt::format("std::uint64_t{{{}}}", value.get<std::uint64_t>());
} else if (value.is_number() && !value.is_number_unsigned()) {
return fmt::format("std::int64_t{{{}}}", value.get<std::uint64_t>());
} else if (value.is_boolean()) {
return fmt::format("bool{{{}}}", value.get<bool>());
} else if (value.is_string()) {
return fmt::format("string_view{{{}}}", json_string(value.get<std::string>()));
} else if (value.is_null()) {
return "std::nullptr_t{}";
}
return "unhandled";
}
/**
* @brief Checks if the given string is a valid C++ identifier. A valid C++ identifier is a string that starts with an
* alphabetic character and is followed by zero or more alphanumeric characters.
*
* @param name The string to check
* @return true if the string is a valid C++ identifier, false otherwise
*/
bool is_valid_identifier(const std::string_view name)
{
// not empty
return !name.empty()
// starts with an alphabetic character
&& std::isalpha(name.front()) != 0
// and is followed by zero or more alphanumeric characters
&& std::all_of(name.begin(), name.end(), [](const auto &chr) { return std::isalnum(chr) != 0; });
}
void assert_valid_identifier(const std::string_view document_name)
{
if (!is_valid_identifier(document_name)) {
throw std::invalid_argument(
fmt::format("document_name '{}' must be a non-empty valid C++ identifier", document_name));
}
}
compile_results compile(const std::string_view document_name, const nlohmann::json &json)
{
assert_valid_identifier(document_name);
std::size_t obj_count{ 0 };
compile_results results;
results.hpp.push_back(fmt::format("#ifndef {}_COMPILED_JSON", document_name));
results.hpp.push_back(fmt::format("#define {}_COMPILED_JSON", document_name));
results.hpp.emplace_back("#include <json2cpp/json2cpp.hpp>");
results.hpp.push_back(fmt::format("namespace compiled_json::{} {{", document_name));
results.hpp.push_back(fmt::format(" const json2cpp::json &get();", document_name));
results.hpp.emplace_back("}");
results.hpp.emplace_back("#endif");
results.impl.push_back(fmt::format(
"// Just in case the user wants to use the entire document in a constexpr context, it can be included safely"));
results.impl.push_back(fmt::format("#ifndef {}_COMPILED_JSON_IMPL", document_name));
results.impl.push_back(fmt::format("#define {}_COMPILED_JSON_IMPL", document_name));
results.impl.emplace_back("#include <json2cpp/json2cpp.hpp>");
results.impl.push_back(fmt::format(R"(
namespace compiled_json::{}::impl {{
using json = json2cpp::basic_json<char>;
using data_t=json2cpp::data_variant<char>;
using string_view=std::basic_string_view<char>;
using array_t=json2cpp::basic_array_t<char>;
using object_t=json2cpp::basic_object_t<char>;
using value_pair_t=json2cpp::basic_value_pair_t<char>;
)",
document_name));
const auto last_obj_name = compile(json, obj_count, results.impl);
results.impl.push_back(fmt::format(R"(
inline constexpr auto document = json{{{{{}}}}};
}}
#endif
)",
last_obj_name));
spdlog::info("{} JSON objects processed.", obj_count);
return results;
}
compile_results compile(const std::string_view document_name, const std::filesystem::path &filename)
{
spdlog::info("Loading file: '{}'", filename.string());
std::ifstream input(filename);
if (!input.is_open()) { throw std::runtime_error(fmt::format("Unable to open the input file name: {}", filename)); }
nlohmann::json document;
input >> document;
spdlog::info("File loaded");
return compile(document_name, document);
}
void write_compilation([[maybe_unused]] std::string_view document_name,
const compile_results &results,
const std::filesystem::path &base_output)
{
assert_valid_identifier(document_name);
const auto append_extension = [](std::filesystem::path name, std::string_view ext) { return name += ext; };
// Create the directory of the base_output's parent
std::filesystem::create_directories(base_output.parent_path());
const auto hpp_name = append_extension(base_output, ".hpp");
const auto cpp_name = append_extension(base_output, ".cpp");
const auto impl_name = append_extension(base_output, "_impl.hpp");
std::ofstream hpp(hpp_name);
for (const auto &line : results.hpp) { hpp << line << '\n'; }
std::ofstream impl(impl_name);
for (const auto &line : results.impl) { impl << line << '\n'; }
std::ofstream cpp(cpp_name);
cpp << fmt::format("#include \"{}\"\n", impl_name.filename().string());
cpp << fmt::format(
"namespace compiled_json::{} {{\nconst json2cpp::json &get() {{ return compiled_json::{}::impl::document; }}\n}}\n",
document_name,
document_name);
}
void compile_to(const std::string_view document_name,
const nlohmann::json &json,
const std::filesystem::path &base_output)
{
write_compilation(document_name, compile(document_name, json), base_output);
}
void compile_to(const std::string_view document_name,
const std::filesystem::path &filename,
const std::filesystem::path &base_output)
{
write_compilation(document_name, compile(document_name, filename), base_output);
}