summaryrefslogtreecommitdiffhomepage
path: root/serialize/json-helper.cpp
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/json-helper.cpp
parent5c7fa846385370236e0774766d8a7f404b5ed764 (diff)
serialize: use exceptions in json-helper
Diffstat (limited to 'serialize/json-helper.cpp')
-rw-r--r--serialize/json-helper.cpp12
1 files changed, 7 insertions, 5 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