Skip to content

Commit

Permalink
feat: more convert for array and object
Browse files Browse the repository at this point in the history
  • Loading branch information
MistEO committed Feb 4, 2024
1 parent 19c542c commit 9ec3ed1
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
27 changes: 27 additions & 0 deletions include/common/array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,33 @@ class basic_array
bool operator==(const basic_array<string_t>& rhs) const;
bool operator!=(const basic_array<string_t>& rhs) const { return !(*this == rhs); }

template <typename value_t, template <typename...> typename collection_t = std::vector,
std::enable_if_t<_utils::is_collection<collection_t<value_t>>, bool> = true>
explicit operator collection_t<value_t>() const
{
return as_collection<value_t, collection_t>();
}
template <typename jsonization_t,
std::enable_if_t<_utils::has_from_json_in_member<jsonization_t, string_t>::value, bool> = true>
explicit operator jsonization_t() const
{
jsonization_t dst {};
if (!dst.from_json(*this)) {
throw exception("Wrong JSON");
}
return dst;
}
template <typename jsonization_t,
std::enable_if_t<_utils::has_from_json_in_templ_spec<jsonization_t, string_t>::value, bool> = true>
explicit operator jsonization_t() const
{
jsonization_t dst {};
if (!ext::jsonization<jsonization_t>().from_json(*this, dst)) {
throw exception("Wrong JSON");
}
return dst;
}

private:
template <typename... key_then_default_value_t, size_t... keys_indexes_t>
auto get(std::tuple<key_then_default_value_t...> keys_then_default_value,
Expand Down
27 changes: 27 additions & 0 deletions include/common/object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,33 @@ class basic_object
bool operator==(const basic_object<string_t>& rhs) const;
bool operator!=(const basic_object<string_t>& rhs) const { return !(*this == rhs); }

template <typename value_t, template <typename...> typename map_t = std::map,
std::enable_if_t<_utils::is_map<map_t<string_t, value_t>>, bool> = true>
explicit operator map_t<string_t, value_t>() const
{
return as_map<value_t, map_t>();
}
template <typename jsonization_t,
std::enable_if_t<_utils::has_from_json_in_member<jsonization_t, string_t>::value, bool> = true>
explicit operator jsonization_t() const
{
jsonization_t dst {};
if (!dst.from_json(*this)) {
throw exception("Wrong JSON");
}
return dst;
}
template <typename jsonization_t,
std::enable_if_t<_utils::has_from_json_in_templ_spec<jsonization_t, string_t>::value, bool> = true>
explicit operator jsonization_t() const
{
jsonization_t dst {};
if (!ext::jsonization<jsonization_t>().from_json(*this, dst)) {
throw exception("Wrong JSON");
}
return dst;
}

private:
template <typename... key_then_default_value_t, size_t... keys_indexes_t>
auto get(std::tuple<key_then_default_value_t...> keys_then_default_value,
Expand Down
17 changes: 17 additions & 0 deletions test/serializing_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,5 +254,22 @@ bool jsonizing()
return false;
}

std::vector<std::filesystem::path> paths = { "/root/dir1/dir2/filename", "/root/dir1/dir2/filename2" };
json::array jpaths = paths;
std::vector<std::filesystem::path> new_paths = (std::vector<std::filesystem::path>)jpaths;
if (new_paths != paths) {
std::cerr << "error new_paths" << std::endl;
return false;
}

std::map<std::string, std::filesystem::path> path_map = { { "key1", "/root/dir1/dir2/filename" },
{ "key2", "/root/dir1/dir2/filename2" } };
json::object jpath_map = path_map;
std::map<std::string, std::filesystem::path> new_path_map = (std::map<std::string, std::filesystem::path>)jpath_map;
if (new_path_map != path_map) {
std::cerr << "error new_path_map" << std::endl;
return false;
}

return true;
}

0 comments on commit 9ec3ed1

Please sign in to comment.