From 5c9863cf0998b5f1b6107ce09b54cd3e8b484221 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Fri, 30 Sep 2022 18:48:50 +0200 Subject: . --- anim/serialize.cpp | 83 +++--------------------------------------------------- 1 file changed, 4 insertions(+), 79 deletions(-) (limited to 'anim/serialize.cpp') diff --git a/anim/serialize.cpp b/anim/serialize.cpp index b0082c3c..83d2b2f9 100644 --- a/anim/serialize.cpp +++ b/anim/serialize.cpp @@ -1,50 +1,12 @@ #include "serialize.hpp" -#include -#include -#include -#include - #include #include -#include "../fake-json.hpp" +#include "../json-magnum.hpp" using Corrade::Utility::Error; -namespace nlohmann { - -template<> -struct adl_serializer final { - static void to_json(json& j, const Magnum::Vector2i& x); - static void from_json(const json& j, Magnum::Vector2i& x); -}; - -void adl_serializer::to_json(json& j, const Magnum::Vector2i& val) -{ - char buf[64]; - snprintf(buf, sizeof(buf), "%d x %d", val[0], val[1]); - j = buf; -} - -void adl_serializer::from_json(const json& j, Magnum::Vector2i& val) -{ - std::string str = j; - int x = 0, y = 0, n = 0; - int ret = std::sscanf(str.c_str(), "%d x %d%n", &x, &y, &n); - if (ret != 2 || (std::size_t)n != str.size()) - { - std::string msg; msg.reserve(64 + str.size()); - msg += "failed to parse string '"; - msg += str; - msg += "' as Magnum::Vector2i"; - throw std::invalid_argument(msg); - } - val = { x, y }; -} - -} // namespace nlohmann - #if defined __clang__ || defined __CLION_IDE__ # pragma clang diagnostic push # pragma clang diagnostic ignored "-Wweak-vtables" @@ -61,47 +23,10 @@ NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(anim, name, nframes, actionframe, fps, groups std::tuple anim::from_json(const std::filesystem::path& pathname) noexcept { - using namespace nlohmann; - std::ifstream s; - s.exceptions(s.exceptions() | std::ios::failbit | std::ios::badbit); - try { - s.open(pathname, std::ios_base::in); - } catch (const std::ios::failure& e) { - Error{Error::Flag::NoSpace} << "failed to open " << pathname << ": " << e.what(); - return { {}, false }; - } - anim ret; - try { - json j; - s >> j; - using nlohmann::from_json; - from_json(j, ret); - } catch (const std::exception& e) { - Error{Error::Flag::NoSpace} << "failed to parse " << pathname << ": " << e.what(); - return { {}, false }; - } - return { std::move(ret), true }; + return json_helper::from_json(pathname); } -bool anim::to_json(const std::filesystem::path& pathname) noexcept +bool anim::to_json(const std::filesystem::path& pathname) const noexcept { - try { - nlohmann::json j = *this; - - std::ofstream s; - s.exceptions(s.exceptions() | std::ios::failbit | std::ios::badbit); - try { - s.open(pathname, std::ios_base::out | std::ios_base::trunc); - } catch (const std::ios::failure& e) { - Error{} << "failed to open" << pathname << "for writing:" << e.what(); - return false; - } - s << j.dump(4); - s.flush(); - } catch (const std::exception& e) { - Error{Error::Flag::NoSpace} << "failed writing to " << pathname << ": " << e.what(); - return false; - } - - return true; + return json_helper::to_json(*this, pathname); } -- cgit v1.2.3