From 9406c5b4f31ef1f6b928cb3e3deab78226e2a88c Mon Sep 17 00:00:00 2001 From: MistEO Date: Mon, 18 Mar 2024 13:54:40 +0800 Subject: [PATCH] fix: deserialize --- include/common/serialization.hpp | 18 +++++----- test/serializing_test.cpp | 62 ++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 9 deletions(-) diff --git a/include/common/serialization.hpp b/include/common/serialization.hpp index 76fe9c8..8f24f0c 100644 --- a/include/common/serialization.hpp +++ b/include/common/serialization.hpp @@ -96,9 +96,6 @@ basic_value serialize(in_t&& in, const serializer_t& serializer = {}) if constexpr (_serialization_helper::is_serializable::value) { return serializer(std::forward(in)); } - else if constexpr (std::is_constructible_v, in_t>) { - return basic_value(std::forward(in)); - } else if constexpr ( _utils::is_collection> || _utils::is_fixed_array>) { basic_array arr; @@ -123,6 +120,9 @@ basic_value serialize(in_t&& in, const serializer_t& serializer = {}) } return obj; } + else if constexpr (std::is_constructible_v, in_t>) { + return basic_value(std::forward(in)); + } else { _serialization_helper::unable_to_serialize(); } @@ -140,10 +140,6 @@ bool deserialize( if constexpr (_serialization_helper::is_deserializable::value) { return deserializer(in, out); } - else if constexpr (std::is_constructible_v>) { - out = out_t(in); - return true; - } else if constexpr (_utils::is_collection>) { if (!in.is_array()) { return false; @@ -189,15 +185,19 @@ bool deserialize( return false; } for (auto&& [key, j_elem] : in.as_object()) { - using elem_t = typename out_t::value_type; + using elem_t = typename out_t::mapped_type; elem_t elem {}; if (!deserialize(j_elem, elem, deserializer)) { return false; } - out.emplace(std::move(elem)); + out.emplace(std::forward(key), std::move(elem)); } return true; } + else if constexpr (std::is_constructible_v>) { + out = out_t(in); + return true; + } else { _serialization_helper::unable_to_deserialize(); } diff --git a/test/serializing_test.cpp b/test/serializing_test.cpp index bcba601..3517db9 100644 --- a/test/serializing_test.cpp +++ b/test/serializing_test.cpp @@ -241,6 +241,8 @@ class jsonization }; } +bool third_party_jsonization_2(); + bool jsonizing() { // then you can use it as json @@ -333,8 +335,68 @@ bool jsonizing() if (jarr.is>()) { std::cerr << "error std::array size" << std::endl; return false; + } + if (!third_party_jsonization_2()) { + std::cerr << "error third_party_jsonization_2" << std::endl; + return false; } return true; } + +struct MyType +{ + int a = 0; +}; + +bool third_party_jsonization_2() +{ + /* If you don't like stupid invasive function, you can use `json::serialize` and + * `json::deserialize` for more elegant conversion: */ + struct Serializer + { + json::value operator()(const MyType& t) const { return t.a; } + }; + + struct Deserializer + { + bool operator()(const json::value& j, MyType& t) const + { + if (!j.is_number()) { + return false; + } + t.a = j.as_integer(); + return true; + } + }; + + std::map third; + third["key"] = { 100 }; + json::value jthird = json::serialize(third, Serializer {}); + + std::cout << jthird << std::endl; + + std::map new_third; + bool ret = json::deserialize(jthird, new_third, Deserializer {}); + if (new_third["key"].a != 100) { + std::cerr << "error new_third[\"key\"].a != 100" << std::endl; + return false; + } + + std::array third_arr {}; + third_arr[4].a = 99; + + json::value jthird_arr = json::serialize(third_arr, Serializer {}); + std::array new_third_arr {}; + bool ret_arr = json::deserialize(jthird_arr, new_third_arr, Deserializer {}); + if (new_third_arr[4].a != 99) { + std::cerr << "error new_third_arr[4].a != 99" << std::endl; + return false; + } + + json::value c = json::serialize(std::array, 10> {}, Serializer {}); + std::cout << c << std::endl; + + return true; +}