mirror of
https://github.com/geode-sdk/geode.git
synced 2025-02-17 00:30:26 -05:00
add utils for loading and saving structs as json files
This commit is contained in:
parent
5374ea5915
commit
2dbad94e52
1 changed files with 22 additions and 0 deletions
|
@ -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")]]
|
||||
|
|
Loading…
Reference in a new issue