summaryrefslogtreecommitdiffhomepage
path: root/serialize
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2022-10-27 14:11:54 +0200
committerStanislaw Halik <sthalik@misaki.pl>2022-10-27 14:11:54 +0200
commit0c468a802bd6c41bf57bd674cb9f44157a3af155 (patch)
tree6505c1522040df4880e4ca64b2613b7e8bd23c72 /serialize
parent06f7a92711c114df01745d40accba15b10d2f720 (diff)
serialize: slim down json-helper header
Diffstat (limited to 'serialize')
-rw-r--r--serialize/json-helper.cpp47
-rw-r--r--serialize/json-helper.hpp56
2 files changed, 66 insertions, 37 deletions
diff --git a/serialize/json-helper.cpp b/serialize/json-helper.cpp
new file mode 100644
index 00000000..8738065f
--- /dev/null
+++ b/serialize/json-helper.cpp
@@ -0,0 +1,47 @@
+#include "json-helper.hpp"
+#include <fstream>
+#include <filesystem>
+
+namespace floormat {
+
+template<typename T, typename P, std::ios_base::openmode open_mode>
+static T open_stream(const std::remove_cvref_t<P>& filename)
+{
+ T s;
+ s.exceptions(s.exceptions() | std::ios::failbit | std::ios::badbit);
+ s.open(filename, open_mode);
+ return s;
+}
+
+auto json_helper::from_json_(const fspath& pathname) -> json
+{
+ json j;
+ open_stream<std::ifstream, fspath, std::ios_base::in>(pathname) >> j;
+ return j;
+}
+
+void json_helper::to_json_(const json& j, const fspath& pathname, int indent)
+{
+ (open_stream<std::ofstream, fspath, std::ios_base::out>(pathname) << j.dump(indent, '\t') << '\n').flush();
+}
+
+#define FORMAT cbor
+
+#define JOIN2(prefix, fmt) prefix ## fmt
+#define JOIN(prefix, fmt) JOIN2(prefix, fmt)
+#define FROM JOIN(from_, FORMAT)
+#define TO JOIN(to_, FORMAT)
+
+auto json_helper::from_binary_(const fspath& pathname) -> json
+{
+ return json::FROM(open_stream<std::ifstream, fspath, std::ios_base::in>(pathname));
+}
+
+void json_helper::to_binary_(const json& j, const fspath& pathname)
+{
+ auto s = open_stream<std::ofstream, fspath, std::ios_base::out>(pathname);
+ json::TO(j, s);
+ s.flush();
+}
+
+} // namespace floormat
diff --git a/serialize/json-helper.hpp b/serialize/json-helper.hpp
index ebac9eba..b6fe1da2 100644
--- a/serialize/json-helper.hpp
+++ b/serialize/json-helper.hpp
@@ -1,47 +1,29 @@
#pragma once
-#include <tuple>
-#include <fstream>
-#include <exception>
-#include <filesystem>
-#include <nlohmann/json.hpp>
+#include <nlohmann/json_fwd.hpp>
+
+namespace std::filesystem { class path; }
namespace floormat {
struct json_helper final {
- template<typename t>
- [[nodiscard]]
- static t from_json(const std::filesystem::path& pathname);
+ using json = nlohmann::json;
+ using fspath = std::filesystem::path;
+
+ template<typename T> static T from_json(const fspath& pathname);
+ template<typename T, int indent = 1> static void to_json(const T& self, const fspath& pathname);
+ static json from_json_(const fspath& pathname);
+ static void to_json_(const json& j, const fspath& pathname, int indent);
- template<typename t>
- static void to_json(const t& self, const std::filesystem::path& pathname);
+ template<typename T> static T from_binary(const fspath& pathname);
+ template<typename T> static void to_binary(const T& self, const fspath& pathname);
+ static json from_binary_(const fspath& pathname);
+ static void to_binary_(const json& j, const fspath& pathname);
};
-template<typename t>
-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);
- s.open(pathname, std::ios_base::in);
- t ret;
- nlohmann::json j;
- s >> j;
- ret = j;
- return ret;
-}
-
-template<typename t>
-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);
- s.open(pathname, std::ios_base::out | std::ios_base::trunc);
- s << j.dump(4);
- s << '\n';
- s.flush();
-}
+template<typename T> T json_helper::from_json(const fspath& pathname) { return from_json_(pathname); }
+template<typename T, int indent> void json_helper::to_json(const T& self, const fspath& pathname) { to_json_(json(self), pathname, indent); }
+
+template<typename T> T json_helper::from_binary(const fspath& pathname) { return from_binary_(pathname); }
+template<typename T> void json_helper::to_binary(const T& self, const fspath& pathname) { to_binary_(json(self), pathname); }
} // namespace floormat