diff --git a/Source/Core/Common/JsonUtil.cpp b/Source/Core/Common/JsonUtil.cpp index 3e842c5c18..fa9b8a73c9 100644 --- a/Source/Core/Common/JsonUtil.cpp +++ b/Source/Core/Common/JsonUtil.cpp @@ -3,6 +3,10 @@ #include "Common/JsonUtil.h" +#include + +#include "Common/FileUtil.h" + picojson::object ToJsonObject(const Common::Vec3& vec) { picojson::object obj; @@ -38,3 +42,27 @@ std::optional ReadBoolFromJson(const picojson::object& obj, const std::str return std::nullopt; return it->second.get(); } + +bool JsonToFile(const std::string& filename, const picojson::value& root, bool prettify) +{ + std::ofstream json_stream; + File::OpenFStream(json_stream, filename, std::ios_base::out); + if (!json_stream.is_open()) + { + return false; + } + json_stream << root.serialize(prettify); + return true; +} + +bool JsonFromFile(const std::string& filename, picojson::value* root, std::string* error) +{ + std::string json_data; + if (!File::ReadFileToString(filename, json_data)) + { + return false; + } + + *error = picojson::parse(*root, json_data); + return error->empty(); +} diff --git a/Source/Core/Common/JsonUtil.h b/Source/Core/Common/JsonUtil.h index eec836d507..309f32f4b4 100644 --- a/Source/Core/Common/JsonUtil.h +++ b/Source/Core/Common/JsonUtil.h @@ -56,3 +56,6 @@ std::optional ReadBoolFromJson(const picojson::object& obj, const std::str picojson::object ToJsonObject(const Common::Vec3& vec); void FromJson(const picojson::object& obj, Common::Vec3& vec); + +bool JsonToFile(const std::string& filename, const picojson::value& root, bool prettify = false); +bool JsonFromFile(const std::string& filename, picojson::value* root, std::string* error);