mirror of
https://github.com/WarmUpTill/SceneSwitcher.git
synced 2026-03-21 17:34:57 -05:00
Add more JSON helpers
This commit is contained in:
parent
5568f92ad0
commit
4c5dbd4b7c
|
|
@ -360,6 +360,21 @@ else()
|
|||
endif()
|
||||
target_link_libraries(${LIB_NAME} PUBLIC nlohmann_json::nlohmann_json)
|
||||
|
||||
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/deps/jsoncons/CMakeLists.txt")
|
||||
# Don't build jsoncons unit tests as they are causing compilation issues and
|
||||
# won't be executed either way
|
||||
if(OS_MACOS)
|
||||
cmake_policy(SET CMP0077 NEW)
|
||||
endif()
|
||||
set(JSONCONS_BUILD_TESTS
|
||||
OFF
|
||||
CACHE BOOL "" FORCE)
|
||||
|
||||
add_subdirectory(deps/jsoncons)
|
||||
target_link_libraries(${LIB_NAME} PRIVATE jsoncons)
|
||||
target_compile_definitions(${LIB_NAME} PRIVATE JSONPATH_SUPPORT=1)
|
||||
endif()
|
||||
|
||||
find_package(CURL QUIET)
|
||||
find_package(Libcurl QUIET)
|
||||
if(CURL_FOUND)
|
||||
|
|
|
|||
|
|
@ -1,5 +1,9 @@
|
|||
#include "json-helpers.hpp"
|
||||
|
||||
#ifdef JSONPATH_SUPPORT
|
||||
#include <jsoncons/json.hpp>
|
||||
#include <jsoncons_ext/jsonpath/jsonpath.hpp>
|
||||
#endif
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <QJsonDocument>
|
||||
|
||||
|
|
@ -57,4 +61,42 @@ std::optional<std::string> GetJsonField(const std::string &jsonStr,
|
|||
return {};
|
||||
}
|
||||
|
||||
std::optional<std::string> QueryJson(const std::string &jsonStr,
|
||||
const std::string &query)
|
||||
{
|
||||
#ifdef JSONPATH_SUPPORT
|
||||
try {
|
||||
auto json = jsoncons::json::parse(jsonStr);
|
||||
auto result = jsoncons::jsonpath::json_query(json, query);
|
||||
return result.as_string();
|
||||
} catch (const jsoncons::json_exception &) {
|
||||
return {};
|
||||
} catch (const jsoncons::json_errc &) {
|
||||
return {};
|
||||
}
|
||||
#else
|
||||
return {};
|
||||
#endif
|
||||
}
|
||||
|
||||
std::optional<std::string> AccessJsonArrayIndex(const std::string &jsonStr,
|
||||
const int index)
|
||||
{
|
||||
try {
|
||||
nlohmann::json json = nlohmann::json::parse(jsonStr);
|
||||
if (!json.is_array() || index >= (int)json.size() ||
|
||||
index < 0) {
|
||||
return {};
|
||||
}
|
||||
auto result = json.at(index);
|
||||
if (result.is_string()) {
|
||||
return result.get<std::string>();
|
||||
}
|
||||
return result.dump();
|
||||
} catch (const nlohmann::json::exception &) {
|
||||
return {};
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
} // namespace advss
|
||||
|
|
|
|||
|
|
@ -13,5 +13,9 @@ 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);
|
||||
EXPORT std::optional<std::string> QueryJson(const std::string &json,
|
||||
const std::string &query);
|
||||
EXPORT std::optional<std::string> AccessJsonArrayIndex(const std::string &json,
|
||||
const int index);
|
||||
|
||||
} // namespace advss
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user