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

@ -802,6 +802,7 @@ AdvSceneSwitcher.action.variable.type.askForValue="Get user input"
AdvSceneSwitcher.action.variable.type.environmentVariable="Set to environment variable value"
AdvSceneSwitcher.action.variable.type.sceneItemCount="Set to scene item count of scene"
AdvSceneSwitcher.action.variable.type.stringLength="Set to length of string"
AdvSceneSwitcher.action.variable.type.extractJson="Extract json field with name"
AdvSceneSwitcher.action.variable.askForValuePromptDefault="Assign value to variable \"%1\":"
AdvSceneSwitcher.action.variable.askForValuePrompt="Assign value to variable:"
AdvSceneSwitcher.action.variable.mathExpression.example="( 1 + 2 * 3 ) / 4"

View File

@ -47,6 +47,8 @@ const static std::map<MacroActionVariable::Type, std::string> actionTypes = {
"AdvSceneSwitcher.action.variable.type.sceneItemCount"},
{MacroActionVariable::Type::STRING_LENGTH,
"AdvSceneSwitcher.action.variable.type.stringLength"},
{MacroActionVariable::Type::EXTRACT_JSON,
"AdvSceneSwitcher.action.variable.type.extractJson"},
};
static void apppend(Variable &var, const std::string &value)
@ -244,6 +246,14 @@ bool MacroActionVariable::PerformAction()
var->SetValue(std::string(_strValue).length());
return true;
}
case Type::EXTRACT_JSON: {
auto value = GetJsonField(var->Value(), _strValue);
if (!value.has_value()) {
return true;
}
var->SetValue(*value);
return true;
}
}
return true;
@ -402,7 +412,7 @@ MacroActionVariableEdit::MacroActionVariableEdit(
: QWidget(parent),
_variables(new VariableSelection(this)),
_variables2(new VariableSelection(this)),
_actions(new QComboBox()),
_actions(new FilterComboBox(this)),
_strValue(new VariableTextEdit(this, 5, 1, 1)),
_numValue(new QDoubleSpinBox()),
_segmentIdx(new MacroSegmentSelection(
@ -631,7 +641,7 @@ void MacroActionVariableEdit::Variable2Changed(const QString &text)
void MacroActionVariableEdit::ActionChanged(int idx)
{
if (_loading || !_entryData) {
if (_loading || !_entryData || idx == -1) {
return;
}
@ -952,7 +962,8 @@ void MacroActionVariableEdit::SetWidgetVisibility()
_entryData->_type ==
MacroActionVariable::Type::SET_FIXED_VALUE ||
_entryData->_type == MacroActionVariable::Type::APPEND ||
_entryData->_type == MacroActionVariable::Type::STRING_LENGTH);
_entryData->_type == MacroActionVariable::Type::STRING_LENGTH ||
_entryData->_type == MacroActionVariable::Type::EXTRACT_JSON);
_numValue->setVisible(
_entryData->_type == MacroActionVariable::Type::INCREMENT ||
_entryData->_type == MacroActionVariable::Type::DECREMENT);

View File

@ -42,6 +42,7 @@ public:
ENV_VARIABLE,
SCENE_ITEM_COUNT,
STRING_LENGTH,
EXTRACT_JSON,
};
Type _type = Type::SET_FIXED_VALUE;
@ -135,7 +136,7 @@ private:
VariableSelection *_variables;
VariableSelection *_variables2;
QComboBox *_actions;
FilterComboBox *_actions;
VariableTextEdit *_strValue;
QDoubleSpinBox *_numValue;
MacroSegmentSelection *_segmentIdx;

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 */