summaryrefslogtreecommitdiffhomepage
path: root/serialize
diff options
context:
space:
mode:
Diffstat (limited to 'serialize')
-rw-r--r--serialize/json-helper.cpp19
-rw-r--r--serialize/json-helper.hpp12
-rw-r--r--serialize/tile.cpp7
-rw-r--r--serialize/tile.hpp12
-rw-r--r--serialize/world.cpp46
-rw-r--r--serialize/world.hpp7
6 files changed, 6 insertions, 97 deletions
diff --git a/serialize/json-helper.cpp b/serialize/json-helper.cpp
index 8738065f..dae3ad96 100644
--- a/serialize/json-helper.cpp
+++ b/serialize/json-helper.cpp
@@ -25,23 +25,4 @@ void json_helper::to_json_(const json& j, const fspath& pathname, int indent)
(open_stream<std::ofstream, fspath, std::ios_base::out>(pathname) << j.dump(indent, '\t') << '\n').flush();
}
-#define FORMAT cbor
-
-#define JOIN2(prefix, fmt) prefix ## fmt
-#define JOIN(prefix, fmt) JOIN2(prefix, fmt)
-#define FROM JOIN(from_, FORMAT)
-#define TO JOIN(to_, FORMAT)
-
-auto json_helper::from_binary_(const fspath& pathname) -> json
-{
- return json::FROM(open_stream<std::ifstream, fspath, std::ios_base::in>(pathname));
-}
-
-void json_helper::to_binary_(const json& j, const fspath& pathname)
-{
- auto s = open_stream<std::ofstream, fspath, std::ios_base::out>(pathname);
- json::TO(j, s);
- s.flush();
-}
-
} // namespace floormat
diff --git a/serialize/json-helper.hpp b/serialize/json-helper.hpp
index b6fe1da2..9c9d208d 100644
--- a/serialize/json-helper.hpp
+++ b/serialize/json-helper.hpp
@@ -10,20 +10,12 @@ struct json_helper final {
using fspath = std::filesystem::path;
template<typename T> static T from_json(const fspath& pathname);
- template<typename T, int indent = 1> static void to_json(const T& self, 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_binary(const fspath& pathname);
- template<typename T> static void to_binary(const T& self, const fspath& pathname);
- static json from_binary_(const fspath& pathname);
- static void to_binary_(const json& j, const fspath& pathname);
};
template<typename T> T json_helper::from_json(const fspath& pathname) { return from_json_(pathname); }
-template<typename T, int indent> void json_helper::to_json(const T& self, const fspath& pathname) { to_json_(json(self), pathname, indent); }
-
-template<typename T> T json_helper::from_binary(const fspath& pathname) { return from_binary_(pathname); }
-template<typename T> void json_helper::to_binary(const T& self, const fspath& pathname) { to_binary_(json(self), pathname); }
+template<typename T> void json_helper::to_json(const T& self, const fspath& pathname, int indent) { to_json_(json(self), pathname, indent); }
} // namespace floormat
diff --git a/serialize/tile.cpp b/serialize/tile.cpp
index 8a5374ab..629ce52d 100644
--- a/serialize/tile.cpp
+++ b/serialize/tile.cpp
@@ -19,13 +19,8 @@ namespace nlohmann {
void adl_serializer<tile_image>::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<tile_image>::from_json(const json& j, tile_image& val) { using nlohmann::from_json; if (j.is_null()) val = {}; else from_json(j, val); }
-void adl_serializer<tile>::to_json(json& j, const tile& val) { using nlohmann::to_json; to_json(j, val); }
-void adl_serializer<tile>::from_json(const json& j, tile& val) { using nlohmann::from_json; from_json(j, val); }
-
-void adl_serializer<chunk>::to_json(json& j, const chunk& val) { using nlohmann::to_json; to_json(j, val.tiles()); }
-void adl_serializer<chunk>::from_json(const json& j, chunk& val) { using nlohmann::from_json; from_json(j, val.tiles()); }
-
void adl_serializer<local_coords>::to_json(json& j, const local_coords& val) { using nlohmann::to_json; to_json(j, val); }
void adl_serializer<local_coords>::from_json(const json& j, local_coords& val) { using nlohmann::from_json; from_json(j, val); }
} // namespace nlohmann
+
diff --git a/serialize/tile.hpp b/serialize/tile.hpp
index 09c23f9a..01673b2b 100644
--- a/serialize/tile.hpp
+++ b/serialize/tile.hpp
@@ -19,18 +19,6 @@ struct adl_serializer<floormat::tile_image> {
};
template<>
-struct adl_serializer<floormat::tile> {
- static void to_json(json& j, const floormat::tile& val);
- static void from_json(const json& j, floormat::tile& val);
-};
-
-template<>
-struct adl_serializer<floormat::chunk> {
- static void to_json(json& j, const floormat::chunk& val);
- static void from_json(const json& j, floormat::chunk& val);
-};
-
-template<>
struct adl_serializer<floormat::local_coords> {
static void to_json(json& j, const floormat::local_coords& val);
static void from_json(const json& j, floormat::local_coords& val);
diff --git a/serialize/world.cpp b/serialize/world.cpp
index 7bdf8ba6..cd6ebd6b 100644
--- a/serialize/world.cpp
+++ b/serialize/world.cpp
@@ -1,10 +1,6 @@
#include "world.hpp"
-#include "serialize/tile.hpp"
-#include "serialize/tile-atlas.hpp"
#include "src/global-coords.hpp"
-#include "src/chunk.hpp"
-#include "src/world.hpp"
-#include <memory>
+#include "serialize/tile.hpp"
#include <nlohmann/json.hpp>
#if defined _MSC_VER
@@ -30,49 +26,11 @@ 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);
- static void from_json(const json& j, std::shared_ptr<chunk>& x);
-};
-
-void adl_serializer<std::shared_ptr<chunk>>::to_json(json& j, const std::shared_ptr<const chunk>& val)
-{
- using nlohmann::to_json;
- if (!val)
- j = nullptr;
- else
- j = *val;
-}
-
-void adl_serializer<std::shared_ptr<chunk>>::from_json(const json& j, std::shared_ptr<chunk>& val)
-{
- using nlohmann::from_json;
- 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); }
void adl_serializer<chunk_coords>::from_json(const json& j, chunk_coords& val) { using nlohmann::from_json; from_json(j, val); }
void adl_serializer<global_coords>::to_json(json& j, const global_coords& val) { using nlohmann::to_json; to_json(j, global_coords_{val.chunk(), val.local()}); }
void adl_serializer<global_coords>::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<world>::to_json(json& j, const world& val)
-{
- using nlohmann::to_json;
- to_json(j, val.chunks());
-}
-
-void adl_serializer<world>::from_json(const json& j, world& val)
-{
- using T = std::remove_cvref_t<decltype(val.chunks())>;
- T x{};
- using nlohmann::from_json;
- from_json(j, x);
- val = world{std::move(x)};
-}
-
} // namespace nlohmann
+
diff --git a/serialize/world.hpp b/serialize/world.hpp
index dd7f5078..39abaaca 100644
--- a/serialize/world.hpp
+++ b/serialize/world.hpp
@@ -24,10 +24,5 @@ struct adl_serializer<floormat::global_coords> {
static void from_json(const json& j, floormat::global_coords& val);
};
-template<>
-struct adl_serializer<floormat::world> {
- static void to_json(json& j, const floormat::world& val);
- static void from_json(const json& j, floormat::world& val);
-};
-
} // namespace nlohmann
+