From 0c468a802bd6c41bf57bd674cb9f44157a3af155 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Thu, 27 Oct 2022 14:11:54 +0200 Subject: serialize: slim down json-helper header --- serialize/json-helper.cpp | 47 +++++++++++++++++++++++++++++++++++++++ serialize/json-helper.hpp | 56 ++++++++++++++++------------------------------- 2 files changed, 66 insertions(+), 37 deletions(-) create mode 100644 serialize/json-helper.cpp (limited to 'serialize') 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 +#include + +namespace floormat { + +template +static T open_stream(const std::remove_cvref_t

& 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(pathname) >> j; + return j; +} + +void json_helper::to_json_(const json& j, const fspath& pathname, int indent) +{ + (open_stream(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(pathname)); +} + +void json_helper::to_binary_(const json& j, const fspath& pathname) +{ + auto s = open_stream(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 -#include -#include -#include -#include +#include + +namespace std::filesystem { class path; } namespace floormat { struct json_helper final { - template - [[nodiscard]] - static t from_json(const std::filesystem::path& pathname); + using json = nlohmann::json; + using fspath = std::filesystem::path; + + template static T from_json(const fspath& pathname); + template 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 - static void to_json(const t& self, const std::filesystem::path& pathname); + template static T from_binary(const fspath& pathname); + template 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 -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 -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 T json_helper::from_json(const fspath& pathname) { return from_json_(pathname); } +template void json_helper::to_json(const T& self, const fspath& pathname) { to_json_(json(self), pathname, indent); } + +template T json_helper::from_binary(const fspath& pathname) { return from_binary_(pathname); } +template void json_helper::to_binary(const T& self, const fspath& pathname) { to_binary_(json(self), pathname); } } // namespace floormat -- cgit v1.2.3