From 5568f92ad09cb5745ef34ef7d1ddcbe71b971081 Mon Sep 17 00:00:00 2001 From: WarmUpTill <19472752+WarmUpTill@users.noreply.github.com> Date: Thu, 17 Apr 2025 11:51:00 +0200 Subject: [PATCH] Move JSON helpers to lib --- CMakeLists.txt | 2 ++ lib/macro/macro-action-variable.cpp | 1 + {plugins/base => lib}/utils/json-helpers.cpp | 20 ++++++++++++++++++++ lib/utils/json-helpers.hpp | 17 +++++++++++++++++ lib/utils/utility.cpp | 20 -------------------- lib/utils/utility.hpp | 2 -- plugins/base/CMakeLists.txt | 2 -- plugins/base/utils/json-helpers.hpp | 13 ------------- plugins/base/utils/source-setting.cpp | 2 +- plugins/base/utils/transform-setting.cpp | 2 +- tests/CMakeLists.txt | 11 +++++++---- tests/test-json.cpp | 15 +++++++++++++++ tests/test-utility.cpp | 15 --------------- 13 files changed, 64 insertions(+), 58 deletions(-) rename {plugins/base => lib}/utils/json-helpers.cpp (62%) create mode 100644 lib/utils/json-helpers.hpp delete mode 100644 plugins/base/utils/json-helpers.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index a92b4df0..bda194d0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -195,6 +195,8 @@ target_sources( lib/utils/help-icon.cpp lib/utils/item-selection-helpers.cpp 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.hpp lib/utils/list-controls.cpp diff --git a/lib/macro/macro-action-variable.cpp b/lib/macro/macro-action-variable.cpp index e014fb1c..562fa5fe 100644 --- a/lib/macro/macro-action-variable.cpp +++ b/lib/macro/macro-action-variable.cpp @@ -1,5 +1,6 @@ #include "macro-action-variable.hpp" #include "advanced-scene-switcher.hpp" +#include "json-helpers.hpp" #include "layout-helpers.hpp" #include "math-helpers.hpp" #include "macro-condition-edit.hpp" diff --git a/plugins/base/utils/json-helpers.cpp b/lib/utils/json-helpers.cpp similarity index 62% rename from plugins/base/utils/json-helpers.cpp rename to lib/utils/json-helpers.cpp index 6628e782..9c21e3cc 100644 --- a/plugins/base/utils/json-helpers.cpp +++ b/lib/utils/json-helpers.cpp @@ -1,5 +1,6 @@ #include "json-helpers.hpp" +#include #include namespace advss { @@ -37,4 +38,23 @@ bool MatchJson(const std::string &json1, const std::string &json2, return j1 == j2; } +std::optional 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(); + } + return it->dump(); + } catch (const nlohmann::json::exception &) { + return {}; + } + return {}; +} + } // namespace advss diff --git a/lib/utils/json-helpers.hpp b/lib/utils/json-helpers.hpp new file mode 100644 index 00000000..f7a82771 --- /dev/null +++ b/lib/utils/json-helpers.hpp @@ -0,0 +1,17 @@ +#pragma once +#include "export-symbol-helper.hpp" +#include "regex-config.hpp" + +#include +#include + +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 GetJsonField(const std::string &json, + const std::string &id); + +} // namespace advss diff --git a/lib/utils/utility.cpp b/lib/utils/utility.cpp index c4ada7c0..014b8249 100644 --- a/lib/utils/utility.cpp +++ b/lib/utils/utility.cpp @@ -1,6 +1,5 @@ #include "utility.hpp" -#include #include #include @@ -28,25 +27,6 @@ bool ReplaceAll(std::string &str, const std::string &from, return somethingWasReplaced; } -std::optional 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(); - } - return it->dump(); - } catch (const nlohmann::json::exception &) { - return {}; - } - return {}; -} - bool CompareIgnoringLineEnding(QString &s1, QString &s2) { // Let QT deal with different types of lineendings diff --git a/lib/utils/utility.hpp b/lib/utils/utility.hpp index 2ccfae9b..472b6c99 100644 --- a/lib/utils/utility.hpp +++ b/lib/utils/utility.hpp @@ -15,8 +15,6 @@ EXPORT std::pair GetCursorPos(); bool ReplaceAll(std::string &str, const std::string &from, const std::string &to); -EXPORT std::optional GetJsonField(const std::string &json, - const std::string &id); EXPORT bool CompareIgnoringLineEnding(QString &s1, QString &s2); std::string ToString(double value); diff --git a/plugins/base/CMakeLists.txt b/plugins/base/CMakeLists.txt index 536160bb..47216a46 100644 --- a/plugins/base/CMakeLists.txt +++ b/plugins/base/CMakeLists.txt @@ -152,8 +152,6 @@ target_sources( utils/filter-selection.hpp utils/hotkey-helpers.cpp utils/hotkey-helpers.hpp - utils/json-helpers.cpp - utils/json-helpers.hpp utils/monitor-helpers.cpp utils/monitor-helpers.hpp utils/osc-helpers.cpp diff --git a/plugins/base/utils/json-helpers.hpp b/plugins/base/utils/json-helpers.hpp deleted file mode 100644 index 25f0b0b8..00000000 --- a/plugins/base/utils/json-helpers.hpp +++ /dev/null @@ -1,13 +0,0 @@ -#pragma once -#include -#include -#include - -namespace advss { - -QString FormatJsonString(std::string); -QString FormatJsonString(QString); -bool MatchJson(const std::string &json1, const std::string &json2, - const RegexConfig ®ex); - -} // namespace advss diff --git a/plugins/base/utils/source-setting.cpp b/plugins/base/utils/source-setting.cpp index 3fefe676..e0eb470a 100644 --- a/plugins/base/utils/source-setting.cpp +++ b/plugins/base/utils/source-setting.cpp @@ -1,7 +1,7 @@ #include "source-setting.hpp" +#include "json-helpers.hpp" #include "obs-module-helper.hpp" #include "math-helpers.hpp" -#include "utility.hpp" #include #include diff --git a/plugins/base/utils/transform-setting.cpp b/plugins/base/utils/transform-setting.cpp index 18a84200..01908376 100644 --- a/plugins/base/utils/transform-setting.cpp +++ b/plugins/base/utils/transform-setting.cpp @@ -1,8 +1,8 @@ #include "transform-setting.hpp" +#include "json-helpers.hpp" #include "obs-module-helper.hpp" #include "math-helpers.hpp" #include "scene-item-transform-helpers.hpp" -#include "utility.hpp" #include #include diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index f574d3a8..0523077c 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -64,9 +64,14 @@ target_sources( # --- 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( - ${PROJECT_NAME} - PRIVATE test-json.cpp ${ADVSS_SOURCE_DIR}/plugins/base/utils/json-helpers.cpp) + ${PROJECT_NAME} PRIVATE test-json.cpp + ${ADVSS_SOURCE_DIR}/lib/utils/json-helpers.cpp) # --- math --- # @@ -95,8 +100,6 @@ target_sources( # --- utility --- # -target_link_libraries(${PROJECT_NAME} PUBLIC nlohmann_json::nlohmann_json) - target_sources( ${PROJECT_NAME} PRIVATE test-utility.cpp ${ADVSS_SOURCE_DIR}/lib/utils/utility.cpp) diff --git a/tests/test-json.cpp b/tests/test-json.cpp index d1b1d81b..183f7e26 100644 --- a/tests/test-json.cpp +++ b/tests/test-json.cpp @@ -51,3 +51,18 @@ TEST_CASE("MatchJson", "[json-helpers]") result = advss::MatchJson("{\n \"test\": true\n}\n", "(", regex); 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"); +} \ No newline at end of file diff --git a/tests/test-utility.cpp b/tests/test-utility.cpp index f5a6bb2c..b45eb6a7 100644 --- a/tests/test-utility.cpp +++ b/tests/test-utility.cpp @@ -20,21 +20,6 @@ TEST_CASE("ReplaceAll", "[utility]") 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]") { QString s1;