Common: add file serialization/deserialization functions for JSON
This commit is contained in:
parent
46a89936ae
commit
1d4f758b14
|
@ -3,6 +3,10 @@
|
||||||
|
|
||||||
#include "Common/JsonUtil.h"
|
#include "Common/JsonUtil.h"
|
||||||
|
|
||||||
|
#include <fstream>
|
||||||
|
|
||||||
|
#include "Common/FileUtil.h"
|
||||||
|
|
||||||
picojson::object ToJsonObject(const Common::Vec3& vec)
|
picojson::object ToJsonObject(const Common::Vec3& vec)
|
||||||
{
|
{
|
||||||
picojson::object obj;
|
picojson::object obj;
|
||||||
|
@ -38,3 +42,27 @@ std::optional<bool> ReadBoolFromJson(const picojson::object& obj, const std::str
|
||||||
return std::nullopt;
|
return std::nullopt;
|
||||||
return it->second.get<bool>();
|
return it->second.get<bool>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
|
|
@ -56,3 +56,6 @@ std::optional<bool> ReadBoolFromJson(const picojson::object& obj, const std::str
|
||||||
|
|
||||||
picojson::object ToJsonObject(const Common::Vec3& vec);
|
picojson::object ToJsonObject(const Common::Vec3& vec);
|
||||||
void FromJson(const picojson::object& obj, 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);
|
||||||
|
|
Loading…
Reference in New Issue