Add action to enable extraction of values from json string

This commit is contained in:
WarmUpTill
2023-11-04 15:55:21 +01:00
committed by WarmUpTill
parent d290dbe86b
commit 33635d6991
5 changed files with 39 additions and 4 deletions

View File

@@ -31,6 +31,7 @@
#include <unordered_map>
#include <regex>
#include <set>
#include <nlohmann/json.hpp>
namespace advss {
@@ -701,6 +702,24 @@ void LoadSplitterPos(QList<int> &sizes, obs_data_t *obj, const std::string name)
obs_data_array_release(array);
}
std::optional<std::string> GetJsonField(const std::string &jsonStr,
const std::string &fieldToExtract)
{
try {
nlohmann::json json = nlohmann::json::parse(jsonStr);
auto it = json.find(fieldToExtract);
if (it == json.end()) {
return {};
}
if (it->is_string()) {
return it->get<std::string>();
}
return it->dump();
} catch (const nlohmann::json::exception &) {
return {};
}
return {};
}
QStringList GetSourceNames()
{
auto sourceEnum = [](void *param, obs_source_t *source) -> bool /* -- */

View File

@@ -11,6 +11,7 @@
#include <obs-frontend-api.h>
#include <deque>
#include <unordered_map>
#include <optional>
namespace advss {
@@ -134,6 +135,8 @@ void SaveSplitterPos(const QList<int> &sizes, obs_data_t *obj,
const std::string name);
void LoadSplitterPos(QList<int> &sizes, obs_data_t *obj,
const std::string name);
std::optional<std::string> GetJsonField(const std::string &json,
const std::string &id);
/* Legacy helpers */