Skip to content

Commit

Permalink
fix: deserialize
Browse files Browse the repository at this point in the history
  • Loading branch information
MistEO committed Mar 18, 2024
1 parent e6c68e0 commit 9406c5b
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 9 deletions.
18 changes: 9 additions & 9 deletions include/common/serialization.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,6 @@ basic_value<string_t> serialize(in_t&& in, const serializer_t& serializer = {})
if constexpr (_serialization_helper::is_serializable<in_t, serializer_t>::value) {
return serializer(std::forward<in_t>(in));
}
else if constexpr (std::is_constructible_v<basic_value<string_t>, in_t>) {
return basic_value<string_t>(std::forward<in_t>(in));
}
else if constexpr (
_utils::is_collection<std::decay_t<in_t>> || _utils::is_fixed_array<std::decay_t<in_t>>) {
basic_array<string_t> arr;
Expand All @@ -123,6 +120,9 @@ basic_value<string_t> serialize(in_t&& in, const serializer_t& serializer = {})
}
return obj;
}
else if constexpr (std::is_constructible_v<basic_value<string_t>, in_t>) {
return basic_value<string_t>(std::forward<in_t>(in));
}
else {
_serialization_helper::unable_to_serialize<in_t>();
}
Expand All @@ -140,10 +140,6 @@ bool deserialize(
if constexpr (_serialization_helper::is_deserializable<out_t, deserializer_t>::value) {
return deserializer(in, out);
}
else if constexpr (std::is_constructible_v<out_t, basic_value<string_t>>) {
out = out_t(in);
return true;
}
else if constexpr (_utils::is_collection<std::decay_t<out_t>>) {
if (!in.is_array()) {
return false;
Expand Down Expand Up @@ -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<elem_t, deserializer_t, string_t>(j_elem, elem, deserializer)) {
return false;
}
out.emplace(std::move(elem));
out.emplace(std::forward<decltype(key)>(key), std::move(elem));
}
return true;
}
else if constexpr (std::is_constructible_v<out_t, basic_value<string_t>>) {
out = out_t(in);
return true;
}
else {
_serialization_helper::unable_to_deserialize<out_t>();
}
Expand Down
62 changes: 62 additions & 0 deletions test/serializing_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,8 @@ class jsonization<std::filesystem::path>
};
}

bool third_party_jsonization_2();

bool jsonizing()
{
// then you can use it as json
Expand Down Expand Up @@ -333,8 +335,68 @@ bool jsonizing()
if (jarr.is<std::array<int, 5>>()) {
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<std::string, MyType> third;
third["key"] = { 100 };
json::value jthird = json::serialize(third, Serializer {});

std::cout << jthird << std::endl;

std::map<std::string, MyType> 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<MyType, 5> third_arr {};
third_arr[4].a = 99;

json::value jthird_arr = json::serialize(third_arr, Serializer {});
std::array<MyType, 5> 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<std::array<MyType, 5>, 10> {}, Serializer {});
std::cout << c << std::endl;

return true;
}

0 comments on commit 9406c5b

Please sign in to comment.