diff options
Diffstat (limited to 'serialize')
-rw-r--r-- | serialize/json-helper.hpp | 28 |
1 files changed, 9 insertions, 19 deletions
diff --git a/serialize/json-helper.hpp b/serialize/json-helper.hpp index b8e290b7..b1502295 100644 --- a/serialize/json-helper.hpp +++ b/serialize/json-helper.hpp @@ -9,46 +9,36 @@ struct json_helper final { template<typename t> [[nodiscard]] - static std::tuple<t, bool> from_json(const std::filesystem::path& pathname); + static t from_json(const std::filesystem::path& pathname); template<typename t> - [[nodiscard]] - static bool to_json(const t& self, const std::filesystem::path& pathname); + static void to_json(const t& self, const std::filesystem::path& pathname); }; template<typename t> -std::tuple<t, bool> json_helper::from_json(const std::filesystem::path& pathname) { +t json_helper::from_json(const std::filesystem::path& pathname) +{ using Corrade::Utility::Error; std::ifstream s; s.exceptions(s.exceptions() | std::ios::failbit | std::ios::badbit); - try { - s.open(pathname, std::ios_base::in); - } catch (const std::ios::failure& e) { - Error{} << "failed to open" << pathname << "for reading:" << e.what(); - return {}; - } + s.open(pathname, std::ios_base::in); t ret; nlohmann::json j; s >> j; ret = j; - return { std::move(ret), true }; + return ret; } template<typename t> -bool json_helper::to_json(const t& self, const std::filesystem::path& pathname) { +void json_helper::to_json(const t& self, const std::filesystem::path& pathname) +{ using Corrade::Utility::Error; nlohmann::json j = self; std::ofstream s; s.exceptions(s.exceptions() | std::ios::failbit | std::ios::badbit); - try { - s.open(pathname, std::ios_base::out | std::ios_base::trunc); - } catch (const std::ios::failure& e) { - Error{} << "failed to open" << pathname << "for writing:" << e.what(); - return false; - } + s.open(pathname, std::ios_base::out | std::ios_base::trunc); s << j.dump(4); s << '\n'; s.flush(); - return true; } |