Skip to content

Commit

Permalink
feat: tuple to json
Browse files Browse the repository at this point in the history
  • Loading branch information
MistEO committed Dec 24, 2024
1 parent 1ab557e commit 70a82f0
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 10 deletions.
17 changes: 17 additions & 0 deletions include/common/array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,17 @@ class basic_array
{
}

template <typename... elem_ts>
basic_array(const std::tuple<elem_ts...>& tup)
{
foreach_tuple(tup, std::make_index_sequence<std::tuple_size_v<std::tuple<elem_ts...>>>());
}
template <typename first_t, typename second_t>
basic_array(std::pair<first_t, second_t> pair)
: _array_data({ std::move(pair.first), std::move(pair.second) })
{
}

~basic_array() noexcept = default;

bool empty() const noexcept { return _array_data.empty(); }
Expand Down Expand Up @@ -238,6 +249,12 @@ class basic_array
template <size_t index, typename tuple_t>
void set_tuple(tuple_t& tup) const;

template <typename Tuple, std::size_t... Is>
void foreach_tuple(const Tuple& t, std::index_sequence<Is...>)
{
(_array_data.emplace_back(std::get<Is>(t)), ...);
}

string_t format(size_t indent, size_t indent_times) const;

private:
Expand Down
5 changes: 0 additions & 5 deletions include/common/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,6 @@ constexpr bool is_collection = false;
template <typename T>
constexpr bool is_collection<T> = is_container<T> && !is_map<T> && !is_fixed_array<T>;

template <typename T, template <typename...> typename Ref, typename = void>
constexpr bool is_specialization = false;
template <template <typename...> typename T, template <typename...> typename Ref, typename... Args>
constexpr bool is_specialization<T<Args...>, Ref> = std::is_same_v<T<Args...>, Ref<Args...>>;

template <typename T>
class has_to_json_in_member
{
Expand Down
14 changes: 9 additions & 5 deletions include/common/value.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,15 @@ class basic_value
{
}

template <
typename tuple_t,
std::enable_if_t<_utils::is_specialization<tuple_t, std::tuple>, bool> = true>
basic_value(tuple_t&& tup)
: basic_value(basic_array<string_t>(std::forward<tuple_t>(tup)))
template <typename... elem_ts>
basic_value(std::tuple<elem_ts...>&& tup)
: basic_value(basic_array<string_t>(std::forward<std::tuple<elem_ts...>>(tup)))
{
}

template <typename elem1_t, typename elem2_t>
basic_value(std::pair<elem1_t, elem2_t>&& pair)
: basic_value(basic_array<string_t>(std::pair<elem1_t, elem2_t>(pair)))
{
}

Expand Down
8 changes: 8 additions & 0 deletions test/serializing_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,14 @@ bool jsonizing()
auto t3 = std::tuple<int, std::string>(tuple_val);
auto p3 = std::tuple<int, std::string>(tuple_val);

auto new_tuple_arr = (json::array)t;
auto new_tuple_val = (json::value)t;
new_tuple_val.as<std::tuple<int, std::string>>();
new_tuple_val.as<std::pair<int, std::string>>();

auto new_pair_arr = (json::array)p;
auto new_pair_val = (json::value)p;

return true;
}

Expand Down

0 comments on commit 70a82f0

Please sign in to comment.