This repository has been archived on 2025-03-15. You can view files and clone it, but cannot push or open issues or pull requests.
sparkle/libs/utils/functions.hpp

38 lines
1.3 KiB
C++
Raw Normal View History

2025-01-25 11:24:56 +05:00
#ifndef UTILS_FUNCTIONS_HPP_
#define UTILS_FUNCTIONS_HPP_
2025-01-27 22:02:21 +05:00
#include <optional>
2025-01-25 11:24:56 +05:00
struct functions {
static inline std::string isNull(const nlohmann::json& str) {
return str.is_null() ? str.dump() : str.get<std::string>();
}
2025-01-27 22:02:21 +05:00
template<typename T>
static std::optional<T> testValue(const nlohmann::json& data, const std::vector<std::string>& keys) {
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;
}
2025-01-25 11:24:56 +05:00
};
#endif