Skip to content

Commit 70a82f0

Browse files
committed
feat: tuple to json
1 parent 1ab557e commit 70a82f0

File tree

4 files changed

+34
-10
lines changed

4 files changed

+34
-10
lines changed

include/common/array.hpp

+17
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,17 @@ class basic_array
7474
{
7575
}
7676

77+
template <typename... elem_ts>
78+
basic_array(const std::tuple<elem_ts...>& tup)
79+
{
80+
foreach_tuple(tup, std::make_index_sequence<std::tuple_size_v<std::tuple<elem_ts...>>>());
81+
}
82+
template <typename first_t, typename second_t>
83+
basic_array(std::pair<first_t, second_t> pair)
84+
: _array_data({ std::move(pair.first), std::move(pair.second) })
85+
{
86+
}
87+
7788
~basic_array() noexcept = default;
7889

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

252+
template <typename Tuple, std::size_t... Is>
253+
void foreach_tuple(const Tuple& t, std::index_sequence<Is...>)
254+
{
255+
(_array_data.emplace_back(std::get<Is>(t)), ...);
256+
}
257+
241258
string_t format(size_t indent, size_t indent_times) const;
242259

243260
private:

include/common/utils.hpp

-5
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,6 @@ constexpr bool is_collection = false;
8484
template <typename T>
8585
constexpr bool is_collection<T> = is_container<T> && !is_map<T> && !is_fixed_array<T>;
8686

87-
template <typename T, template <typename...> typename Ref, typename = void>
88-
constexpr bool is_specialization = false;
89-
template <template <typename...> typename T, template <typename...> typename Ref, typename... Args>
90-
constexpr bool is_specialization<T<Args...>, Ref> = std::is_same_v<T<Args...>, Ref<Args...>>;
91-
9287
template <typename T>
9388
class has_to_json_in_member
9489
{

include/common/value.hpp

+9-5
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,15 @@ class basic_value
122122
{
123123
}
124124

125-
template <
126-
typename tuple_t,
127-
std::enable_if_t<_utils::is_specialization<tuple_t, std::tuple>, bool> = true>
128-
basic_value(tuple_t&& tup)
129-
: basic_value(basic_array<string_t>(std::forward<tuple_t>(tup)))
125+
template <typename... elem_ts>
126+
basic_value(std::tuple<elem_ts...>&& tup)
127+
: basic_value(basic_array<string_t>(std::forward<std::tuple<elem_ts...>>(tup)))
128+
{
129+
}
130+
131+
template <typename elem1_t, typename elem2_t>
132+
basic_value(std::pair<elem1_t, elem2_t>&& pair)
133+
: basic_value(basic_array<string_t>(std::pair<elem1_t, elem2_t>(pair)))
130134
{
131135
}
132136

test/serializing_test.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,14 @@ bool jsonizing()
364364
auto t3 = std::tuple<int, std::string>(tuple_val);
365365
auto p3 = std::tuple<int, std::string>(tuple_val);
366366

367+
auto new_tuple_arr = (json::array)t;
368+
auto new_tuple_val = (json::value)t;
369+
new_tuple_val.as<std::tuple<int, std::string>>();
370+
new_tuple_val.as<std::pair<int, std::string>>();
371+
372+
auto new_pair_arr = (json::array)p;
373+
auto new_pair_val = (json::value)p;
374+
367375
return true;
368376
}
369377

0 commit comments

Comments
 (0)