Move JSON helpers to lib

This commit is contained in:
WarmUpTill 2025-04-17 11:51:00 +02:00 committed by WarmUpTill
parent 7c4c0056ce
commit 5568f92ad0
13 changed files with 64 additions and 58 deletions

View File

@ -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

View File

@ -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"

View File

@ -1,5 +1,6 @@
#include "json-helpers.hpp"
#include <nlohmann/json.hpp>
#include <QJsonDocument>
namespace advss {
@ -37,4 +38,23 @@ bool MatchJson(const std::string &json1, const std::string &json2,
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

View 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 &regex);
EXPORT std::optional<std::string> GetJsonField(const std::string &json,
const std::string &id);
} // namespace advss

View File

@ -1,6 +1,5 @@
#include "utility.hpp"
#include <nlohmann/json.hpp>
#include <QTextStream>
#include <sstream>
@ -28,25 +27,6 @@ bool ReplaceAll(std::string &str, const std::string &from,
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)
{
// Let QT deal with different types of lineendings

View File

@ -15,8 +15,6 @@ EXPORT std::pair<int, int> GetCursorPos();
bool ReplaceAll(std::string &str, const std::string &from,
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);
std::string ToString(double value);

View File

@ -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

View File

@ -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 &regex);
} // namespace advss

View File

@ -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 <nlohmann/json.hpp>
#include <QLayout>

View File

@ -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 <nlohmann/json.hpp>
#include <QLayout>

View File

@ -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)

View File

@ -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");
}

View File

@ -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;