summaryrefslogtreecommitdiffhomepage
path: root/serialize/json-helper.hpp
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2022-10-06 17:30:31 +0200
committerStanislaw Halik <sthalik@misaki.pl>2022-10-06 17:30:31 +0200
commit437dd5940bad6133561cb896cd558881fa5c8b44 (patch)
treeb8f8103801d0f232ae05e728e83774673ae8413f /serialize/json-helper.hpp
parentded69f52906990cf975a62c0efbaca4b6cfa5e88 (diff)
a
Diffstat (limited to 'serialize/json-helper.hpp')
-rw-r--r--serialize/json-helper.hpp59
1 files changed, 24 insertions, 35 deletions
diff --git a/serialize/json-helper.hpp b/serialize/json-helper.hpp
index af6fd211..4a9ee0ac 100644
--- a/serialize/json-helper.hpp
+++ b/serialize/json-helper.hpp
@@ -6,57 +6,46 @@
#include <nlohmann/json.hpp>
#include <Corrade/Utility/DebugStl.h>
-template<typename t>
struct json_helper final {
- [[nodiscard]] static std::tuple<t, bool> from_json(const std::filesystem::path& pathname) noexcept;
- [[nodiscard]] static bool to_json(const t& self, const std::filesystem::path& pathname) noexcept;
+ template<typename t>
+ [[nodiscard]]
+ static std::tuple<t, bool> from_json(const std::filesystem::path& pathname);
+
+ template<typename t>
+ [[nodiscard]]
+ static bool to_json(const t& self, const std::filesystem::path& pathname);
};
template<typename t>
-std::tuple<t, bool> json_helper<t>::from_json(const std::filesystem::path& pathname) noexcept {
+std::tuple<t, bool> json_helper::from_json(const std::filesystem::path& pathname) {
using namespace nlohmann;
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{Error::Flag::NoSpace} << "failed to open '" << pathname << "': " << e.what();
- return { {}, false };
- }
+ s.open(pathname, std::ios_base::in);
t ret;
- try {
- json j;
- s >> j;
- using nlohmann::from_json;
- from_json(j, ret);
- } catch (const std::exception& e) {
- Error{Error::Flag::NoSpace} << "failed to parse '" << pathname << "': " << e.what();
- return { {}, false };
- }
+ json j;
+ s >> j;
+ using nlohmann::from_json;
+ from_json(j, ret);
return { std::move(ret), true };
}
template<typename t>
-bool json_helper<t>::to_json(const t& self, const std::filesystem::path& pathname) noexcept {
+bool json_helper::to_json(const t& self, const std::filesystem::path& pathname) {
using Corrade::Utility::Error;
- try {
- nlohmann::json j(self);
+ 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{Error::Flag::NoSpace} << "failed to open '" << pathname << "' for writing: " << e.what();
- return false;
- }
- s << j.dump(4);
- s.flush();
- } catch (const std::exception& e) {
- Error{Error::Flag::NoSpace} << "failed writing to '" << pathname << "': " << e.what();
+ 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{Error::Flag::NoSpace} << "failed to open '" << pathname << "' for writing: " << e.what();
return false;
}
-
+ s << j.dump(4);
+ s.flush();
return true;
}
+