add utils for loading and saving structs as json files

This commit is contained in:
HJfod 2023-04-22 21:37:57 +03:00
parent 5374ea5915
commit 2dbad94e52

View file

@ -24,9 +24,31 @@ namespace geode::utils::file {
GEODE_DLL Result<json::Value> readJson(ghc::filesystem::path const& path);
GEODE_DLL Result<ByteVector> readBinary(ghc::filesystem::path const& path);
template <class T>
Result<T> readFromJson(ghc::filesystem::path const& file) {
GEODE_UNWRAP_INTO(auto json, readJson(file));
try {
return json.template as<T>();
}
catch(std::exception& e) {
return Err("Error parsing JSON: {}", e.what());
}
}
GEODE_DLL Result<> writeString(ghc::filesystem::path const& path, std::string const& data);
GEODE_DLL Result<> writeBinary(ghc::filesystem::path const& path, ByteVector const& data);
template <class T>
Result<> writeToJson(ghc::filesystem::path const& path, T const& data) {
try {
GEODE_UNWRAP(writeString(json::Value(data).dump()));
return Ok();
}
catch(std::exception& e) {
return Err("Error serializing JSON: {}", e.what());
}
}
GEODE_DLL Result<> createDirectory(ghc::filesystem::path const& path);
GEODE_DLL Result<> createDirectoryAll(ghc::filesystem::path const& path);
[[deprecated("Use file::readDirectory")]]