diff options
Diffstat (limited to 'serialize')
-rw-r--r-- | serialize/json-helper.cpp | 15 | ||||
-rw-r--r-- | serialize/json-helper.hpp | 15 |
2 files changed, 14 insertions, 16 deletions
diff --git a/serialize/json-helper.cpp b/serialize/json-helper.cpp index dae3ad96..538bcdcb 100644 --- a/serialize/json-helper.cpp +++ b/serialize/json-helper.cpp @@ -1,28 +1,27 @@ #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) +template<typename T, std::ios_base::openmode mode> +static T open_stream(StringView filename) { T s; s.exceptions(s.exceptions() | std::ios::failbit | std::ios::badbit); - s.open(filename, open_mode); + s.open(filename, mode); return s; } -auto json_helper::from_json_(const fspath& pathname) -> json +auto json_helper::from_json_(StringView filename) -> json { json j; - open_stream<std::ifstream, fspath, std::ios_base::in>(pathname) >> j; + open_stream<std::ifstream, std::ios_base::in>(filename) >> j; return j; } -void json_helper::to_json_(const json& j, const fspath& pathname, int indent) +void json_helper::to_json_(const json& j, StringView filename, int indent) { - (open_stream<std::ofstream, fspath, std::ios_base::out>(pathname) << j.dump(indent, '\t') << '\n').flush(); + (open_stream<std::ofstream, std::ios_base::out>(filename) << j.dump(indent, '\t') << '\n').flush(); } } // namespace floormat diff --git a/serialize/json-helper.hpp b/serialize/json-helper.hpp index e0e2c0ca..90ce2e9a 100644 --- a/serialize/json-helper.hpp +++ b/serialize/json-helper.hpp @@ -1,20 +1,19 @@ #pragma once #include <nlohmann/json.hpp> -#include <filesystem> +#include <Corrade/Containers/StringView.h> namespace floormat { struct json_helper final { using json = nlohmann::json; - using fspath = std::filesystem::path; - template<typename T> static T from_json(const fspath& pathname); - template<typename T> static void to_json(const T& self, const fspath& pathname, int indent = 1); - static json from_json_(const fspath& pathname); - static void to_json_(const json& j, const fspath& pathname, int indent); + 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> T json_helper::from_json(const fspath& pathname) { return from_json_(pathname); } -template<typename T> void json_helper::to_json(const T& self, const fspath& pathname, int indent) { to_json_(json(self), pathname, indent); } +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); } } // namespace floormat |