Refactor json utility to wxdata, add ReadJsonString function

This commit is contained in:
Dan Paulat 2025-02-01 18:21:41 -06:00
parent 895e760fee
commit 9f33189c18
12 changed files with 245 additions and 228 deletions

View file

@ -1,10 +1,10 @@
#include <scwx/qt/manager/marker_manager.hpp>
#include <scwx/qt/types/marker_types.hpp>
#include <scwx/qt/util/color.hpp>
#include <scwx/qt/util/json.hpp>
#include <scwx/qt/util/texture_atlas.hpp>
#include <scwx/qt/main/application.hpp>
#include <scwx/qt/manager/resource_manager.hpp>
#include <scwx/util/json.hpp>
#include <scwx/util/logger.hpp>
#include <filesystem>
@ -62,7 +62,7 @@ public:
bool markerFileRead_ {false};
void InitalizeIds();
void InitalizeIds();
types::MarkerId NewId();
types::MarkerId lastId_ {0};
};
@ -70,15 +70,9 @@ public:
class MarkerManager::Impl::MarkerRecord
{
public:
MarkerRecord(const types::MarkerInfo& info) :
markerInfo_ {info}
{
}
MarkerRecord(const types::MarkerInfo& info) : markerInfo_ {info} {}
const types::MarkerInfo& toMarkerInfo()
{
return markerInfo_;
}
const types::MarkerInfo& toMarkerInfo() { return markerInfo_; }
types::MarkerInfo markerInfo_;
@ -175,7 +169,7 @@ void MarkerManager::Impl::ReadMarkerSettings()
// Determine if marker settings exists
if (std::filesystem::exists(markerSettingsPath_))
{
markerJson = util::json::ReadJsonFile(markerSettingsPath_);
markerJson = scwx::util::json::ReadJsonFile(markerSettingsPath_);
}
if (markerJson != nullptr && markerJson.is_array())
@ -224,8 +218,8 @@ void MarkerManager::Impl::WriteMarkerSettings()
logger_->info("Saving location marker settings");
const std::shared_lock lock(markerRecordLock_);
auto markerJson = boost::json::value_from(markerRecords_);
util::json::WriteJsonFile(markerSettingsPath_, markerJson);
auto markerJson = boost::json::value_from(markerRecords_);
scwx::util::json::WriteJsonFile(markerSettingsPath_, markerJson);
}
std::shared_ptr<MarkerManager::Impl::MarkerRecord>
@ -357,10 +351,11 @@ types::MarkerId MarkerManager::add_marker(const types::MarkerInfo& marker)
types::MarkerId id;
{
const std::unique_lock lock(p->markerRecordLock_);
id = p->NewId();
id = p->NewId();
size_t index = p->markerRecords_.size();
p->idToIndex_.emplace(id, index);
p->markerRecords_.emplace_back(std::make_shared<Impl::MarkerRecord>(marker));
p->markerRecords_.emplace_back(
std::make_shared<Impl::MarkerRecord>(marker));
p->markerRecords_[index]->markerInfo_.id = id;
add_icon(marker.iconName);
@ -499,7 +494,6 @@ void MarkerManager::set_marker_settings_path(const std::string& path)
p->markerSettingsPath_ = path;
}
std::shared_ptr<MarkerManager> MarkerManager::Instance()
{
static std::weak_ptr<MarkerManager> markerManagerReference_ {};

View file

@ -2,10 +2,10 @@
#include <scwx/qt/manager/font_manager.hpp>
#include <scwx/qt/manager/resource_manager.hpp>
#include <scwx/qt/main/application.hpp>
#include <scwx/qt/util/json.hpp>
#include <scwx/qt/util/network.hpp>
#include <scwx/gr/placefile.hpp>
#include <scwx/network/cpr.hpp>
#include <scwx/util/json.hpp>
#include <scwx/util/logger.hpp>
#include <shared_mutex>
@ -385,7 +385,7 @@ void PlacefileManager::Impl::ReadPlacefileSettings()
// Determine if placefile settings exists
if (std::filesystem::exists(placefileSettingsPath_))
{
placefileJson = util::json::ReadJsonFile(placefileSettingsPath_);
placefileJson = scwx::util::json::ReadJsonFile(placefileSettingsPath_);
}
// If placefile settings was successfully read
@ -428,7 +428,7 @@ void PlacefileManager::Impl::WritePlacefileSettings()
std::shared_lock lock {placefileRecordLock_};
auto placefileJson = boost::json::value_from(placefileRecords_);
util::json::WriteJsonFile(placefileSettingsPath_, placefileJson);
scwx::util::json::WriteJsonFile(placefileSettingsPath_, placefileJson);
}
void PlacefileManager::SetRadarSite(

View file

@ -9,7 +9,7 @@
#include <scwx/qt/settings/text_settings.hpp>
#include <scwx/qt/settings/ui_settings.hpp>
#include <scwx/qt/settings/unit_settings.hpp>
#include <scwx/qt/util/json.hpp>
#include <scwx/util/json.hpp>
#include <scwx/util/logger.hpp>
#include <filesystem>

View file

@ -1,4 +1,5 @@
#include <scwx/qt/manager/update_manager.hpp>
#include <scwx/util/json.hpp>
#include <scwx/util/logger.hpp>
#include <mutex>
@ -29,8 +30,7 @@ public:
~Impl() {}
static std::string GetVersionString(const std::string& releaseName);
static boost::json::value ParseResponseText(const std::string& s);
static std::string GetVersionString(const std::string& releaseName);
size_t PopulateReleases();
size_t AddReleases(const boost::json::value& json);
@ -70,28 +70,6 @@ UpdateManager::Impl::GetVersionString(const std::string& releaseName)
return versionString;
}
boost::json::value UpdateManager::Impl::ParseResponseText(const std::string& s)
{
boost::json::stream_parser p;
boost::system::error_code ec;
p.write(s, ec);
if (ec)
{
logger_->warn("{}", ec.message());
return nullptr;
}
p.finish(ec);
if (ec)
{
logger_->warn("{}", ec.message());
return nullptr;
}
return p.release();
}
bool UpdateManager::CheckForUpdates(const std::string& currentVersion)
{
std::unique_lock lock(p->updateMutex_);
@ -148,7 +126,7 @@ size_t UpdateManager::Impl::PopulateReleases()
// Successful REST API query
if (r.status_code == 200)
{
boost::json::value json = Impl::ParseResponseText(r.text);
boost::json::value json = util::json::ReadJsonString(r.text);
if (json == nullptr)
{
logger_->warn("Response not JSON: {}", r.header["content-type"]);