diff --git a/src/utils/Json.cpp b/src/utils/Json.cpp index bf65b3e4..14f1f71e 100644 --- a/src/utils/Json.cpp +++ b/src/utils/Json.cpp @@ -1,4 +1,5 @@ #include "Json.h" +#include "Platform.h" #include "../plugin-macros.generated.h" bool Utils::Json::JsonArrayIsValidObsArray(json j) @@ -152,3 +153,24 @@ json Utils::Json::ObsDataToJson(obs_data_t *d, bool includeDefault) return j; } + +bool Utils::Json::GetJsonFileContent(std::string fileName, json &content) +{ + std::string textContent; + if (!Utils::Platform::GetTextFileContent(fileName, textContent)) + return false; + + try { + content = json::parse(textContent); + } 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, json content, bool createNew) +{ + std::string textContent = content.dump(2); + return Utils::Platform::SetTextFileContent(fileName, textContent, createNew); +} diff --git a/src/utils/Json.h b/src/utils/Json.h index b9ccbd3b..4ee19fe3 100644 --- a/src/utils/Json.h +++ b/src/utils/Json.h @@ -1,5 +1,6 @@ #pragma once +#include #include #include @@ -10,5 +11,7 @@ namespace Utils { bool JsonArrayIsValidObsArray(json j); 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, json content, bool createNew = true); } } \ No newline at end of file diff --git a/src/utils/Platform.cpp b/src/utils/Platform.cpp index e4f0b300..4f2e7608 100644 --- a/src/utils/Platform.cpp +++ b/src/utils/Platform.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include #include "Platform.h" @@ -98,3 +99,27 @@ void Utils::Platform::SendTrayNotification(QSystemTrayIcon::MessageIcon icon, QS delete notification; }, (void*)notification, false); } + +bool Utils::Platform::GetTextFileContent(std::string fileName, std::string &content) +{ + QFile f(QString::fromStdString(fileName)); + if (!f.open(QIODevice::ReadOnly | QIODevice::Text)) + return false; + + content = f.readAll().toStdString(); + return true; +} + +bool Utils::Platform::SetTextFileContent(std::string fileName, std::string content, bool createNew) +{ + if (!createNew && !QFile::exists(QString::fromStdString(fileName))) + return false; + + QFile f(QString::fromStdString(fileName)); + if (!f.open(QIODevice::WriteOnly | QIODevice::Text)) + return false; + + QTextStream out(&f); + out << content.c_str(); + return true; +} diff --git a/src/utils/Platform.h b/src/utils/Platform.h index c1a58f82..404395d0 100644 --- a/src/utils/Platform.h +++ b/src/utils/Platform.h @@ -10,5 +10,7 @@ namespace Utils { QString GetCommandLineArgument(QString arg); bool GetCommandLineFlagSet(QString arg); void SendTrayNotification(QSystemTrayIcon::MessageIcon icon, QString title, QString body); + bool GetTextFileContent(std::string fileName, std::string &content); + bool SetTextFileContent(std::string filePath, std::string content, bool createNew = true); } } \ No newline at end of file