diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2022-10-23 11:59:24 +0200 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2022-10-23 11:59:24 +0200 |
commit | a62bd981c2a973952f3f5e22ca768dd961991d21 (patch) | |
tree | 5f4bd867de7982247a913a4d3cee44c44f2c5248 | |
parent | 9dec9084e80987749c698b9ff0bcd39df5c8a988 (diff) |
allow serializing empty shared_ptr
-rw-r--r-- | serialize/tile-atlas.cpp | 17 | ||||
-rw-r--r-- | serialize/world.cpp | 16 |
2 files changed, 22 insertions, 11 deletions
diff --git a/serialize/tile-atlas.cpp b/serialize/tile-atlas.cpp index d5350851..fcbb371d 100644 --- a/serialize/tile-atlas.cpp +++ b/serialize/tile-atlas.cpp @@ -15,16 +15,23 @@ using proxy_atlas = std::tuple<std::string, Vector2ub>; void adl_serializer<std::shared_ptr<tile_atlas>>::to_json(json& j, const std::shared_ptr<const tile_atlas>& x) { - fm_assert(x); using nlohmann::to_json; - to_json(j, proxy_atlas{x->name(), x->num_tiles2()}); + if (!x) + j = nullptr; + else + to_json(j, proxy_atlas{x->name(), x->num_tiles2()}); } void adl_serializer<std::shared_ptr<tile_atlas>>::from_json(const json& j, std::shared_ptr<tile_atlas>& x) { - proxy_atlas proxy = j; - const auto& [name, num_tiles] = proxy; - x = loader.tile_atlas(name, num_tiles); + if (j.is_null()) + x = nullptr; + else + { + proxy_atlas proxy = j; + const auto& [name, num_tiles] = proxy; + x = loader.tile_atlas(name, num_tiles); + } } } // namespace nlohmann diff --git a/serialize/world.cpp b/serialize/world.cpp index 8b6f84d7..7bdf8ba6 100644 --- a/serialize/world.cpp +++ b/serialize/world.cpp @@ -26,10 +26,10 @@ NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(global_coords_, chunk, local) } // namespace floormat -using namespace floormat; - namespace nlohmann { +using namespace floormat; + template<> struct adl_serializer<std::shared_ptr<chunk>> final { static void to_json(json& j, const std::shared_ptr<const chunk>& x); @@ -38,16 +38,20 @@ struct adl_serializer<std::shared_ptr<chunk>> final { void adl_serializer<std::shared_ptr<chunk>>::to_json(json& j, const std::shared_ptr<const chunk>& val) { - fm_assert(val); using nlohmann::to_json; - j = *val; + if (!val) + j = nullptr; + else + j = *val; } void adl_serializer<std::shared_ptr<chunk>>::from_json(const json& j, std::shared_ptr<chunk>& val) { - val = std::make_shared<chunk>(); using nlohmann::from_json; - *val = j; + if (j.is_null()) + val = nullptr; + else + *(val = std::make_shared<chunk>()) = j; } void adl_serializer<chunk_coords>::to_json(json& j, const chunk_coords& val) { using nlohmann::to_json; to_json(j, val); } |