-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_handling.cpp
309 lines (267 loc) · 8.53 KB
/
json_handling.cpp
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#include "json_handling.h"
#include "definitions.h"
#include <fmt/core.h>
bool initialize = true;
std::string get_next_word(std::ifstream& file, bool reset = false) {
// Static variables to maintain state between calls
static std::string current_line = "";
static size_t current_pos = 0;
// Reset static variables if needed:
if (reset == true) {
current_line = "";
current_pos = 0;
return "";
}
std::string current_word = "";
bool inside_quotes = false;
while (true) {
// If we've finished with the current line (or it's our first call), read a
// new one
if (current_pos >= current_line.size()) {
if (!std::getline(file, current_line)) {
return "";
}
current_pos = 0; // Reset position for the new line
}
// Iterate over the line char by char from the current position
for (; current_pos < current_line.size(); ++current_pos) {
char current_symbol = current_line[current_pos];
// Handle double quotes
if (current_symbol == '"') {
inside_quotes = !inside_quotes; // Toggle the inside_quotes flag
}
// If not inside quotes, handle word extraction normally
if (!inside_quotes) {
if (current_symbol == ' ' && current_word.empty()) {
continue; // Skip leading spaces
}
// If the current symbol is a special character or space
if (current_symbol == ' ' || current_symbol == '[' ||
current_symbol == ']' || current_symbol == '{' ||
current_symbol == '}' || current_symbol == ',' ||
current_symbol == ':') {
if (current_word.empty()) {
current_pos++; // Move past the special character or space for the
// next word
return std::string(
1, current_symbol); // Return the special character as a word
} else {
return current_word; // Return the accumulated word
}
}
}
current_word += current_symbol;
// If we're ending a quoted word, break out and return it
if (current_symbol == '"' && !inside_quotes) {
current_pos++; // Move past the closing quote for the next word
return current_word;
}
}
}
}
void move_using_json_path(std::ifstream& file,
std::vector<std::string>& json_path_tokens,
std::stack<char>& array_stack) {
int cnt = 0;
int max = json_path_tokens.size();
if (json_path_tokens[cnt] != "$") {
throw std::runtime_error(
"Runtime error occurred.\nJsonPath is not supported!");
}
cnt++;
std::string current_token;
// Get new token if available
if (cnt < max) {
current_token = json_path_tokens[cnt];
} else {
// Add opening bracket to stack
array_stack.push('[');
return;
}
// Init to random value; just dont be empty
std::string res = ";";
// Iterate over all words
while (true) {
res = get_next_word(file);
// If empty file is empty
if (res == "") {
throw std::runtime_error("Runtime error occurred.\nJsonPath not found!");
break;
}
// If it matches the whole string
if (res == current_token) {
#ifdef DEBUG
fmt::print("Found 1\n");
#endif
cnt++;
if (cnt < max) {
current_token = json_path_tokens[cnt];
} else {
return;
}
}
// If it matches the [
if (res[0] == '[' && current_token[0] == '[') {
#ifdef DEBUG
fmt::print("Found 1\n");
#endif
array_stack.push('[');
cnt++;
if (cnt < max) {
current_token = json_path_tokens[cnt];
} else {
return;
}
}
}
}
std::string get_next_element(std::ifstream& file,
std::vector<std::string>& json_path_tokens,
bool& initialize) {
static std::stack<char> array_stack;
static std::stack<char> element_stack;
// Move to correct position
if (initialize) {
move_using_json_path(file, json_path_tokens, array_stack);
}
// Generate next element
// Init to random value; just dont be empty
std::string res = ";";
std::string current_element = "";
while (true) {
res = get_next_word(file);
// If read file is empty
if (res == "") {
break;
}
// If \r is found -> ignore it
if (res == "\r") {
continue;
}
// change stack dependent on value
if (res[0] == '[') {
array_stack.push('[');
}
if (res[0] == ']') {
array_stack.pop();
}
// If stack is empty -> not more data can be found
if (array_stack.empty()) {
#ifdef DEBUG
fmt::print("stack empty\n");
#endif
return "";
}
// change stack dependent on value
if (res[0] == '{') {
element_stack.push('{');
}
if (res[0] == '}') {
element_stack.pop();
}
if (element_stack.empty()) {
current_element += res;
if (current_element != ",") {
return current_element;
} else {
current_element = "";
continue;
}
}
current_element = current_element + " " + res;
}
return "";
}
/**
* @brief Recursive helper function to extract headers and values from a JSON
* object.
*
* This function traverses the JSON structure and creates header strings
* by concatenating keys with dots. The final header and corresponding values
* are then stored in the provided vectors.
*
* @param j The current JSON object or value being processed.
* @param headers The vector to store extracted header strings.
* @param values The vector to store corresponding values for the headers.
* @param currentKey The current key path being constructed (used for
* recursion).
*/
void json_to_csv_helper(JsonVariant variant, std::vector<std::string>& headers,
std::vector<std::string>& values,
const std::string& currentKey = "") {
if (variant.is<JsonObject>()) {
for (JsonPair keyValue : variant.as<JsonObject>()) {
std::string newKey = currentKey.empty()
? keyValue.key().c_str()
: currentKey + "." + keyValue.key().c_str();
if (keyValue.value().is<JsonObject>()) {
json_to_csv_helper(keyValue.value(), headers, values, newKey);
} else {
headers.push_back(newKey);
if (keyValue.value().is<const char*>()) {
values.push_back(
"\"" + std::string(keyValue.value().as<const char*>()) + "\"");
} else if (keyValue.value().is<int>()) {
values.push_back(std::to_string(keyValue.value().as<int>()));
} else if (keyValue.value().is<float>()) {
values.push_back(std::to_string(keyValue.value().as<float>()));
} else if (keyValue.value().is<bool>()) {
values.push_back(keyValue.value().as<bool>() ? "true" : "false");
} else if (keyValue.value().isNull()) {
values.push_back("");
} else {
throw std::runtime_error(
"Runtime error occurred.\nUnsupported json data type.");
}
}
}
}
}
/**
* @brief Converts a JSON string to a CSV string.
*
* This function processes a given JSON string and produces a CSV representation
* of it. The resulting CSV has the headers as the first row and the values as
* the second row.
*
* @param jsonString The JSON string to be converted.
* @return std::string The resulting CSV string.
*/
std::string json_to_csv(const std::string& jsonString) {
StaticJsonDocument<2048> doc; // Adjust the buffer size based on your needs
deserializeJson(doc, jsonString.c_str());
std::vector<std::string> headers;
std::vector<std::string> values;
json_to_csv_helper(doc.as<JsonVariant>(), headers, values);
std::string csvString = "";
for (const auto& header : headers) {
csvString += header + ",";
}
csvString.pop_back(); // remove trailing comma
csvString += "\n";
for (const auto& value : values) {
csvString += value + ",";
}
csvString.pop_back(); // remove trailing comma
csvString += "\n";
return csvString;
}
void reinit_json(std::ifstream& file) {
#ifdef DEBUG
fmt::print("Reset JSON parsing...\n");
#endif
initialize = true;
std::string res = get_next_word(file, true);
}
std::string get_next_element_csv(std::ifstream& file,
std::vector<std::string>& json_path_tokens) {
std::string val = get_next_element(file, json_path_tokens, initialize);
initialize = false;
if (val == "") {
return "";
}
#ifdef DEBUG
fmt::print("Final generated json element: {}\n", val);
#endif
return json_to_csv(val);
}