From b1165d784c446dc505a100d861cb5151bebdda15 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Sat, 22 Oct 2022 16:03:10 +0200 Subject: serializer work --- serialize/tile-atlas.cpp | 16 ++++++--------- serialize/tile-atlas.hpp | 2 +- serialize/tile.cpp | 4 ++-- serialize/world.cpp | 53 +++++++++++++++++++++++++++++++++++++++++++++--- serialize/world.hpp | 8 ++++++++ 5 files changed, 67 insertions(+), 16 deletions(-) (limited to 'serialize') diff --git a/serialize/tile-atlas.cpp b/serialize/tile-atlas.cpp index 4c80a212..d5350851 100644 --- a/serialize/tile-atlas.cpp +++ b/serialize/tile-atlas.cpp @@ -2,29 +2,25 @@ #include "serialize/tile-atlas.hpp" #include "serialize/magnum-vector2i.hpp" #include "loader.hpp" +#include "compat/assert.hpp" #include #include -using namespace Magnum; using namespace floormat; namespace nlohmann { using proxy_atlas = std::tuple; -using shared_atlas = std::shared_ptr; -void adl_serializer::to_json(json& j, const shared_atlas& x) +void adl_serializer>::to_json(json& j, const std::shared_ptr& x) { - if (!x) - j = nullptr; - else { - using nlohmann::to_json; - to_json(j, proxy_atlas{x->name(), x->num_tiles2()}); - } + fm_assert(x); + using nlohmann::to_json; + to_json(j, proxy_atlas{x->name(), x->num_tiles2()}); } -void adl_serializer::from_json(const json& j, shared_atlas& x) +void adl_serializer>::from_json(const json& j, std::shared_ptr& x) { proxy_atlas proxy = j; const auto& [name, num_tiles] = proxy; diff --git a/serialize/tile-atlas.hpp b/serialize/tile-atlas.hpp index f4549d7c..02e5b966 100644 --- a/serialize/tile-atlas.hpp +++ b/serialize/tile-atlas.hpp @@ -8,7 +8,7 @@ namespace nlohmann { template<> struct adl_serializer> final { - static void to_json(json& j, const std::shared_ptr& x); + static void to_json(json& j, const std::shared_ptr& x); static void from_json(const json& j, std::shared_ptr& x); }; diff --git a/serialize/tile.cpp b/serialize/tile.cpp index 2e94bfb8..11473548 100644 --- a/serialize/tile.cpp +++ b/serialize/tile.cpp @@ -12,10 +12,10 @@ NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(local_coords, x, y) } // namespace floormat -namespace nlohmann { - using namespace floormat; +namespace nlohmann { + void adl_serializer::to_json(json& j, const tile_image& val) { using nlohmann::to_json; if (val.atlas) to_json(j, val); else j = nullptr; } void adl_serializer::from_json(const json& j, tile_image& val) { using nlohmann::from_json; from_json(j, val); } diff --git a/serialize/world.cpp b/serialize/world.cpp index b52fe3b9..835a80f8 100644 --- a/serialize/world.cpp +++ b/serialize/world.cpp @@ -1,8 +1,16 @@ #include "world.hpp" -#include "tile.hpp" -#include "global-coords.hpp" +#include "serialize/tile.hpp" +#include "serialize/tile-atlas.hpp" +#include "src/global-coords.hpp" +#include "src/chunk.hpp" +#include "src/world.hpp" +#include #include +#ifdef __GNUG__ +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wdeprecated-declarations" +#endif namespace floormat { NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(chunk_coords, x, y) @@ -16,9 +24,29 @@ NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(global_coords_, chunk, local) } // namespace floormat +using namespace floormat; + namespace nlohmann { -using namespace floormat; +template<> +struct adl_serializer> final { + static void to_json(json& j, const std::shared_ptr& x); + static void from_json(const json& j, std::shared_ptr& x); +}; + +void adl_serializer>::to_json(json& j, const std::shared_ptr& val) +{ + fm_assert(val); + using nlohmann::to_json; + j = *val; +} + +void adl_serializer>::from_json(const json& j, std::shared_ptr& val) +{ + val = std::make_shared(); + using nlohmann::from_json; + *val = j; +} void adl_serializer::to_json(json& j, const chunk_coords& val) { using nlohmann::to_json; to_json(j, val); } void adl_serializer::from_json(const json& j, chunk_coords& val) { using nlohmann::from_json; from_json(j, val); } @@ -26,4 +54,23 @@ void adl_serializer::from_json(const json& j, chunk_coords& val) { void adl_serializer::to_json(json& j, const global_coords& val) { using nlohmann::to_json; to_json(j, global_coords_{val.chunk(), val.local()}); } void adl_serializer::from_json(const json& j, global_coords& val) { using nlohmann::from_json; global_coords_ x; from_json(j, x); val = {x.chunk, x.local}; } +void adl_serializer::to_json(json& j, const world& val) +{ + using nlohmann::to_json; + to_json(j, val.chunks()); +} + +void adl_serializer::from_json(const json& j, world& val) +{ + using T = std::remove_cvref_t; + T x{}; + using nlohmann::from_json; + from_json(j, x); + val = world{std::move(x)}; +} + } // namespace nlohmann + +#ifdef __GNUG__ +#pragma GCC diagnostic pop +#endif diff --git a/serialize/world.hpp b/serialize/world.hpp index 05c88b40..dd7f5078 100644 --- a/serialize/world.hpp +++ b/serialize/world.hpp @@ -5,6 +5,8 @@ namespace floormat { struct chunk_coords; struct global_coords; +struct chunk; +struct world; } // namespace floormat @@ -22,4 +24,10 @@ struct adl_serializer { static void from_json(const json& j, floormat::global_coords& val); }; +template<> +struct adl_serializer { + static void to_json(json& j, const floormat::world& val); + static void from_json(const json& j, floormat::world& val); +}; + } // namespace nlohmann -- cgit v1.2.3