40 lines
1.4 KiB
C++
40 lines
1.4 KiB
C++
#ifndef UTILS_FUNCTIONS_HPP_
|
|
#define UTILS_FUNCTIONS_HPP_
|
|
#include <optional>
|
|
#include <string>
|
|
#include <nlohmann/json.hpp>
|
|
struct functions {
|
|
static inline auto isNull(const nlohmann::json& str) -> std::string {
|
|
return str.is_null() ? str.dump() : str.get<std::string>();
|
|
}
|
|
template<typename T>
|
|
static auto testValue(const nlohmann::json& data, const std::vector<std::string>& keys) -> std::optional<T> {
|
|
const nlohmann::json* current = &data;
|
|
for (const auto& key : keys) {
|
|
if (current->contains(key)) {
|
|
current = &(*current)[key];
|
|
} else {
|
|
return std::nullopt;
|
|
}
|
|
}
|
|
if constexpr (std::is_same_v<T, bool>) {
|
|
if (current->is_boolean()) {
|
|
return current->get<bool>();
|
|
}
|
|
} else if constexpr (std::is_same_v<T, int>) {
|
|
if (current->is_number_integer()) {
|
|
return current->get<int>();
|
|
}
|
|
} else if constexpr (std::is_same_v<T, double>) {
|
|
if (current->is_number_float()) {
|
|
return current->get<double>();
|
|
}
|
|
} else if constexpr (std::is_same_v<T, std::string>) {
|
|
if (current->is_string()) {
|
|
return current->get<std::string>();
|
|
}
|
|
}
|
|
return std::nullopt;
|
|
}
|
|
};
|
|
#endif |