Move JSON temp var heper functions

This commit is contained in:
WarmUpTill
2025-03-06 19:50:42 +01:00
committed by WarmUpTill
parent 7b0f985f18
commit d670b6d07e
3 changed files with 51 additions and 46 deletions

View File

@@ -2,6 +2,7 @@
#include "token.hpp"
#include <log-helper.hpp>
#include <nlohmann/json.hpp>
namespace advss {
@@ -330,4 +331,34 @@ RequestResult SendDeleteRequest(const TwitchToken &token,
return processResult(response, __func__);
}
void SetJsonTempVars(const std::string &jsonStr,
std::function<void(const char *, const char *)> setVarFunc)
{
try {
auto json = nlohmann::json::parse(jsonStr);
for (auto it = json.begin(); it != json.end(); ++it) {
if (it->is_string()) {
setVarFunc(it.key().c_str(),
it->get<std::string>().c_str());
continue;
}
setVarFunc(it.key().c_str(), it->dump().c_str());
}
} catch (const nlohmann::json::exception &e) {
vblog(LOG_INFO, "%s", jsonStr.c_str());
vblog(LOG_INFO, "%s", e.what());
}
}
void SetJsonTempVars(obs_data_t *data,
std::function<void(const char *, const char *)> setVarFunc)
{
auto jsonStr = obs_data_get_json(data);
if (!jsonStr) {
return;
}
SetJsonTempVars(jsonStr, setVarFunc);
}
} // namespace advss