mirror of
https://github.com/Palakis/obs-websocket.git
synced 2024-08-30 18:12:16 +00:00
utils: Reimplement Get/SetJsonFileContent helpers
Uses <fstream> instead of the text helpers
This commit is contained in:
@ -17,8 +17,9 @@ You should have received a copy of the GNU General Public License along
|
||||
with this program. If not, see <https://www.gnu.org/licenses/>
|
||||
*/
|
||||
|
||||
#include <fstream>
|
||||
|
||||
#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;
|
||||
}
|
||||
|
@ -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(); }
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user