mirror of
https://github.com/WarmUpTill/SceneSwitcher.git
synced 2026-03-21 17:34:57 -05:00
Move JSON helpers to lib
This commit is contained in:
parent
7c4c0056ce
commit
5568f92ad0
|
|
@ -195,6 +195,8 @@ target_sources(
|
||||||
lib/utils/help-icon.cpp
|
lib/utils/help-icon.cpp
|
||||||
lib/utils/item-selection-helpers.cpp
|
lib/utils/item-selection-helpers.cpp
|
||||||
lib/utils/item-selection-helpers.hpp
|
lib/utils/item-selection-helpers.hpp
|
||||||
|
lib/utils/json-helpers.cpp
|
||||||
|
lib/utils/json-helpers.hpp
|
||||||
lib/utils/layout-helpers.cpp
|
lib/utils/layout-helpers.cpp
|
||||||
lib/utils/layout-helpers.hpp
|
lib/utils/layout-helpers.hpp
|
||||||
lib/utils/list-controls.cpp
|
lib/utils/list-controls.cpp
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
#include "macro-action-variable.hpp"
|
#include "macro-action-variable.hpp"
|
||||||
#include "advanced-scene-switcher.hpp"
|
#include "advanced-scene-switcher.hpp"
|
||||||
|
#include "json-helpers.hpp"
|
||||||
#include "layout-helpers.hpp"
|
#include "layout-helpers.hpp"
|
||||||
#include "math-helpers.hpp"
|
#include "math-helpers.hpp"
|
||||||
#include "macro-condition-edit.hpp"
|
#include "macro-condition-edit.hpp"
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
#include "json-helpers.hpp"
|
#include "json-helpers.hpp"
|
||||||
|
|
||||||
|
#include <nlohmann/json.hpp>
|
||||||
#include <QJsonDocument>
|
#include <QJsonDocument>
|
||||||
|
|
||||||
namespace advss {
|
namespace advss {
|
||||||
|
|
@ -37,4 +38,23 @@ bool MatchJson(const std::string &json1, const std::string &json2,
|
||||||
return j1 == j2;
|
return j1 == j2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 {};
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace advss
|
} // namespace advss
|
||||||
17
lib/utils/json-helpers.hpp
Normal file
17
lib/utils/json-helpers.hpp
Normal file
|
|
@ -0,0 +1,17 @@
|
||||||
|
#pragma once
|
||||||
|
#include "export-symbol-helper.hpp"
|
||||||
|
#include "regex-config.hpp"
|
||||||
|
|
||||||
|
#include <QString>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace advss {
|
||||||
|
|
||||||
|
EXPORT QString FormatJsonString(std::string);
|
||||||
|
EXPORT QString FormatJsonString(QString);
|
||||||
|
EXPORT bool MatchJson(const std::string &json1, const std::string &json2,
|
||||||
|
const RegexConfig ®ex);
|
||||||
|
EXPORT std::optional<std::string> GetJsonField(const std::string &json,
|
||||||
|
const std::string &id);
|
||||||
|
|
||||||
|
} // namespace advss
|
||||||
|
|
@ -1,6 +1,5 @@
|
||||||
#include "utility.hpp"
|
#include "utility.hpp"
|
||||||
|
|
||||||
#include <nlohmann/json.hpp>
|
|
||||||
#include <QTextStream>
|
#include <QTextStream>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
|
||||||
|
|
@ -28,25 +27,6 @@ bool ReplaceAll(std::string &str, const std::string &from,
|
||||||
return somethingWasReplaced;
|
return somethingWasReplaced;
|
||||||
}
|
}
|
||||||
|
|
||||||
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 {};
|
|
||||||
}
|
|
||||||
|
|
||||||
bool CompareIgnoringLineEnding(QString &s1, QString &s2)
|
bool CompareIgnoringLineEnding(QString &s1, QString &s2)
|
||||||
{
|
{
|
||||||
// Let QT deal with different types of lineendings
|
// Let QT deal with different types of lineendings
|
||||||
|
|
|
||||||
|
|
@ -15,8 +15,6 @@ EXPORT std::pair<int, int> GetCursorPos();
|
||||||
|
|
||||||
bool ReplaceAll(std::string &str, const std::string &from,
|
bool ReplaceAll(std::string &str, const std::string &from,
|
||||||
const std::string &to);
|
const std::string &to);
|
||||||
EXPORT std::optional<std::string> GetJsonField(const std::string &json,
|
|
||||||
const std::string &id);
|
|
||||||
EXPORT bool CompareIgnoringLineEnding(QString &s1, QString &s2);
|
EXPORT bool CompareIgnoringLineEnding(QString &s1, QString &s2);
|
||||||
std::string ToString(double value);
|
std::string ToString(double value);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -152,8 +152,6 @@ target_sources(
|
||||||
utils/filter-selection.hpp
|
utils/filter-selection.hpp
|
||||||
utils/hotkey-helpers.cpp
|
utils/hotkey-helpers.cpp
|
||||||
utils/hotkey-helpers.hpp
|
utils/hotkey-helpers.hpp
|
||||||
utils/json-helpers.cpp
|
|
||||||
utils/json-helpers.hpp
|
|
||||||
utils/monitor-helpers.cpp
|
utils/monitor-helpers.cpp
|
||||||
utils/monitor-helpers.hpp
|
utils/monitor-helpers.hpp
|
||||||
utils/osc-helpers.cpp
|
utils/osc-helpers.cpp
|
||||||
|
|
|
||||||
|
|
@ -1,13 +0,0 @@
|
||||||
#pragma once
|
|
||||||
#include <QString>
|
|
||||||
#include <string>
|
|
||||||
#include <regex-config.hpp>
|
|
||||||
|
|
||||||
namespace advss {
|
|
||||||
|
|
||||||
QString FormatJsonString(std::string);
|
|
||||||
QString FormatJsonString(QString);
|
|
||||||
bool MatchJson(const std::string &json1, const std::string &json2,
|
|
||||||
const RegexConfig ®ex);
|
|
||||||
|
|
||||||
} // namespace advss
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
#include "source-setting.hpp"
|
#include "source-setting.hpp"
|
||||||
|
#include "json-helpers.hpp"
|
||||||
#include "obs-module-helper.hpp"
|
#include "obs-module-helper.hpp"
|
||||||
#include "math-helpers.hpp"
|
#include "math-helpers.hpp"
|
||||||
#include "utility.hpp"
|
|
||||||
|
|
||||||
#include <nlohmann/json.hpp>
|
#include <nlohmann/json.hpp>
|
||||||
#include <QLayout>
|
#include <QLayout>
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,8 @@
|
||||||
#include "transform-setting.hpp"
|
#include "transform-setting.hpp"
|
||||||
|
#include "json-helpers.hpp"
|
||||||
#include "obs-module-helper.hpp"
|
#include "obs-module-helper.hpp"
|
||||||
#include "math-helpers.hpp"
|
#include "math-helpers.hpp"
|
||||||
#include "scene-item-transform-helpers.hpp"
|
#include "scene-item-transform-helpers.hpp"
|
||||||
#include "utility.hpp"
|
|
||||||
|
|
||||||
#include <nlohmann/json.hpp>
|
#include <nlohmann/json.hpp>
|
||||||
#include <QLayout>
|
#include <QLayout>
|
||||||
|
|
|
||||||
|
|
@ -64,9 +64,14 @@ target_sources(
|
||||||
|
|
||||||
# --- json --- #
|
# --- json --- #
|
||||||
|
|
||||||
|
if(TARGET jsoncons)
|
||||||
|
target_link_libraries(${PROJECT_NAME} PRIVATE jsoncons)
|
||||||
|
target_compile_definitions(${PROJECT_NAME} PRIVATE JSONPATH_SUPPORT=1)
|
||||||
|
endif()
|
||||||
|
target_link_libraries(${PROJECT_NAME} PUBLIC nlohmann_json::nlohmann_json)
|
||||||
target_sources(
|
target_sources(
|
||||||
${PROJECT_NAME}
|
${PROJECT_NAME} PRIVATE test-json.cpp
|
||||||
PRIVATE test-json.cpp ${ADVSS_SOURCE_DIR}/plugins/base/utils/json-helpers.cpp)
|
${ADVSS_SOURCE_DIR}/lib/utils/json-helpers.cpp)
|
||||||
|
|
||||||
# --- math --- #
|
# --- math --- #
|
||||||
|
|
||||||
|
|
@ -95,8 +100,6 @@ target_sources(
|
||||||
|
|
||||||
# --- utility --- #
|
# --- utility --- #
|
||||||
|
|
||||||
target_link_libraries(${PROJECT_NAME} PUBLIC nlohmann_json::nlohmann_json)
|
|
||||||
|
|
||||||
target_sources(
|
target_sources(
|
||||||
${PROJECT_NAME} PRIVATE test-utility.cpp
|
${PROJECT_NAME} PRIVATE test-utility.cpp
|
||||||
${ADVSS_SOURCE_DIR}/lib/utils/utility.cpp)
|
${ADVSS_SOURCE_DIR}/lib/utils/utility.cpp)
|
||||||
|
|
|
||||||
|
|
@ -51,3 +51,18 @@ TEST_CASE("MatchJson", "[json-helpers]")
|
||||||
result = advss::MatchJson("{\n \"test\": true\n}\n", "(", regex);
|
result = advss::MatchJson("{\n \"test\": true\n}\n", "(", regex);
|
||||||
REQUIRE(result == false);
|
REQUIRE(result == false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("GetJsonField", "[utility]")
|
||||||
|
{
|
||||||
|
auto result = advss::GetJsonField("{}", "");
|
||||||
|
REQUIRE_FALSE(result);
|
||||||
|
|
||||||
|
result = advss::GetJsonField("{}", "not there");
|
||||||
|
REQUIRE_FALSE(result);
|
||||||
|
|
||||||
|
result = advss::GetJsonField("invalid json", "not there");
|
||||||
|
REQUIRE_FALSE(result);
|
||||||
|
|
||||||
|
result = advss::GetJsonField("{ \"test\": 1 }", "test");
|
||||||
|
REQUIRE(*result == "1");
|
||||||
|
}
|
||||||
|
|
@ -20,21 +20,6 @@ TEST_CASE("ReplaceAll", "[utility]")
|
||||||
REQUIRE(input == "sleeping");
|
REQUIRE(input == "sleeping");
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("GetJsonField", "[utility]")
|
|
||||||
{
|
|
||||||
auto result = advss::GetJsonField("{}", "");
|
|
||||||
REQUIRE_FALSE(result);
|
|
||||||
|
|
||||||
result = advss::GetJsonField("{}", "not there");
|
|
||||||
REQUIRE_FALSE(result);
|
|
||||||
|
|
||||||
result = advss::GetJsonField("invalid json", "not there");
|
|
||||||
REQUIRE_FALSE(result);
|
|
||||||
|
|
||||||
result = advss::GetJsonField("{ \"test\": 1 }", "test");
|
|
||||||
REQUIRE(*result == "1");
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_CASE("CompareIgnoringLineEnding", "[utility]")
|
TEST_CASE("CompareIgnoringLineEnding", "[utility]")
|
||||||
{
|
{
|
||||||
QString s1;
|
QString s1;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user