summaryrefslogtreecommitdiffhomepage
path: root/serialize
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2022-12-02 17:36:12 +0100
committerStanislaw Halik <sthalik@misaki.pl>2022-12-02 18:58:28 +0100
commita4fb316879c88f07f7adf219a0f21b5f06776a63 (patch)
treeb6b7d933c02f7f9c8030a17caa43113bc6f9d49f /serialize
parent5c7fa846385370236e0774766d8a7f404b5ed764 (diff)
serialize: use exceptions in json-helper
Diffstat (limited to 'serialize')
-rw-r--r--serialize/json-helper.cpp12
-rw-r--r--serialize/json-helper.hpp12
2 files changed, 13 insertions, 11 deletions
diff --git a/serialize/json-helper.cpp b/serialize/json-helper.cpp
index c3f861a4..9c3b5034 100644
--- a/serialize/json-helper.cpp
+++ b/serialize/json-helper.cpp
@@ -1,8 +1,9 @@
#include "json-helper.hpp"
-#include "compat/assert.hpp"
+#include "compat/exception.hpp"
#include <cerrno>
#include <cstring>
#include <fstream>
+#include <Corrade/Containers/StringStlView.h>
namespace floormat {
@@ -10,6 +11,7 @@ template<typename T, std::ios_base::openmode mode>
static T open_stream(StringView filename)
{
T s;
+ s.exceptions(std::ios_base::failbit | std::ios_base::badbit);
s.open(filename.data(), mode);
if (!s)
{
@@ -25,21 +27,21 @@ static T open_stream(StringView filename)
};
const char* mode_str = (mode & std::ios_base::out) == std::ios_base::out ? "writing" : "reading";
(void)get_error_string(errbuf);
- fm_error("can't open file '%s' for %s: %s", filename.data(), mode_str, errbuf);
+ fm_throw("can't open file '{}' for {}: {}"_cf, filename, mode_str, errbuf);
}
return s;
}
-auto json_helper::from_json_(StringView filename) -> json
+auto json_helper::from_json_(StringView filename) noexcept(false) -> json
{
json j;
open_stream<std::ifstream, std::ios_base::in>(filename) >> j;
return j;
}
-void json_helper::to_json_(const json& j, StringView filename, int indent)
+void json_helper::to_json_(const json& j, StringView filename) noexcept(false)
{
- (open_stream<std::ofstream, std::ios_base::out>(filename) << j.dump(indent, '\t') << '\n').flush();
+ (open_stream<std::ofstream, std::ios_base::out>(filename) << j.dump(1, '\t') << '\n').flush();
}
} // namespace floormat
diff --git a/serialize/json-helper.hpp b/serialize/json-helper.hpp
index 90ce2e9a..1bbcf429 100644
--- a/serialize/json-helper.hpp
+++ b/serialize/json-helper.hpp
@@ -7,13 +7,13 @@ namespace floormat {
struct json_helper final {
using json = nlohmann::json;
- template<typename T> static T from_json(StringView pathname);
- template<typename T> static void to_json(const T& self, StringView pathname, int indent = 1);
- static json from_json_(StringView pathname);
- static void to_json_(const json& j, StringView pathname, int indent);
+ template<typename T> static T from_json(StringView pathname) noexcept(false);
+ template<typename T> static void to_json(const T& self, StringView pathname) noexcept(false);
+ static json from_json_(StringView pathname) noexcept(false);
+ static void to_json_(const json& j, StringView pathname) noexcept(false);
};
-template<typename T> T json_helper::from_json(StringView pathname) { return from_json_(pathname); }
-template<typename T> void json_helper::to_json(const T& self, StringView pathname, int indent) { to_json_(json(self), pathname, indent); }
+template<typename T> T json_helper::from_json(StringView pathname) noexcept(false) { return from_json_(pathname); }
+template<typename T> void json_helper::to_json(const T& self, StringView pathname) noexcept(false) { to_json_(json(self), pathname); }
} // namespace floormat