From 6731ab0243ba437595062558e56b800d5eca9cf5 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Wed, 5 Oct 2022 15:47:29 +0200 Subject: a --- serialize/vector.hpp | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 serialize/vector.hpp (limited to 'serialize/vector.hpp') diff --git a/serialize/vector.hpp b/serialize/vector.hpp new file mode 100644 index 00000000..39924a9b --- /dev/null +++ b/serialize/vector.hpp @@ -0,0 +1,41 @@ +#include +#include +#include +#include +#include +#include + +namespace nlohmann { + +template +struct adl_serializer> final { + static void to_json(json& j, const Magnum::Math::Vector2& x); + static void from_json(const json& j, Magnum::Math::Vector2& x); +}; + +template +void adl_serializer>::to_json(json& j, const Magnum::Math::Vector2& val) +{ + char buf[64]; + snprintf(buf, sizeof(buf), "%d x %d", val[0], val[1]); + j = buf; +} + +template +void adl_serializer>::from_json(const json& j, Magnum::Math::Vector2& 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 -- cgit v1.2.3