forked from janhq/cortex.cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson_parser_utils.h
More file actions
33 lines (28 loc) · 746 Bytes
/
json_parser_utils.h
File metadata and controls
33 lines (28 loc) · 746 Bytes
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
#pragma once
#include <json/value.h>
#include <string>
#include "utils/logging_utils.h"
namespace json_parser_utils {
template <typename T>
T jsonToValue(const Json::Value& value);
template <>
inline std::string jsonToValue(const Json::Value& value) {
return value.asString();
}
template <typename T>
std::vector<T> ParseJsonArray(const Json::Value& array) {
try {
std::vector<T> result;
if (array.isArray()) {
result.reserve(array.size());
for (const Json::Value& element : array) {
result.push_back(jsonToValue<T>(element));
}
}
return result;
} catch (const std::exception& e) {
CTL_ERR("Error parsing json array: " << e.what());
return {};
}
}
}; // namespace json_parser_utils