From bdf812dc09035459bf0f54fece984992443fb663 Mon Sep 17 00:00:00 2001 From: tt2468 Date: Mon, 22 Apr 2024 18:22:27 -0700 Subject: [PATCH] utils: Reimplement Get/SetJsonFileContent helpers Uses instead of the text helpers --- src/utils/Json.cpp | 22 +++++++++++++++------- src/utils/Json.h | 2 +- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/utils/Json.cpp b/src/utils/Json.cpp index 81fa52af..6f686494 100644 --- a/src/utils/Json.cpp +++ b/src/utils/Json.cpp @@ -17,8 +17,9 @@ You should have received a copy of the GNU General Public License along with this program. If not, see */ +#include + #include "Json.h" -#include "Platform.h" #include "plugin-macros.generated.h" bool Utils::Json::JsonArrayIsValidObsArray(const json &j) @@ -177,21 +178,28 @@ json Utils::Json::ObsDataToJson(obs_data_t *d, bool includeDefault) bool Utils::Json::GetJsonFileContent(std::string fileName, json &content) { - std::string textContent; - if (!Utils::Platform::GetTextFileContent(fileName, textContent)) + std::ifstream f(fileName); + if (!f.is_open()) return false; try { - content = json::parse(textContent); + content = json::parse(f); } catch (json::parse_error &e) { blog(LOG_WARNING, "Failed to decode content of JSON file `%s`. Error: %s", fileName.c_str(), e.what()); return false; } + return true; } -bool Utils::Json::SetJsonFileContent(std::string fileName, const json &content, bool createNew) +bool Utils::Json::SetJsonFileContent(std::string fileName, const json &content) { - std::string textContent = content.dump(2); - return Utils::Platform::SetTextFileContent(fileName, textContent, createNew); + std::ofstream f(fileName); + if (!f.is_open()) + return false; + + // Set indent to 2 spaces, then dump content + f << std::setw(2) << content; + + return true; } diff --git a/src/utils/Json.h b/src/utils/Json.h index 6fadb15b..57c9173d 100644 --- a/src/utils/Json.h +++ b/src/utils/Json.h @@ -76,7 +76,7 @@ namespace Utils { obs_data_t *JsonToObsData(json j); json ObsDataToJson(obs_data_t *d, bool includeDefault = false); bool GetJsonFileContent(std::string fileName, json &content); - bool SetJsonFileContent(std::string fileName, const json &content, bool createNew = true); + bool SetJsonFileContent(std::string fileName, const json &content); static inline bool Contains(const json &j, std::string key) { return j.contains(key) && !j[key].is_null(); } } }