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

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