Skip to content

Commit

Permalink
Merge pull request #326 from fesily/fix-json--declare-from_json_impl-…
Browse files Browse the repository at this point in the history
…definitions-in-advance-when-variant
  • Loading branch information
qicosmos authored Nov 19, 2024
2 parents ae5a951 + 0a084ca commit 46ab4d8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
5 changes: 4 additions & 1 deletion iguana/json_reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ IGUANA_INLINE void from_json_impl(U &value, It &&it, It &&end);
template <typename U, typename It, std::enable_if_t<smart_ptr_v<U>, int> = 0>
IGUANA_INLINE void from_json_impl(U &value, It &&it, It &&end);

template <typename U, typename It, std::enable_if_t<variant_v<U>, int> = 0>
IGUANA_INLINE void from_json_impl(U &value, It &&it, It &&end);

template <typename U, typename It,
std::enable_if_t<ylt_refletable_v<U>, int> = 0>
IGUANA_INLINE void from_json_impl(U &value, It &&it, It &&end) {
Expand Down Expand Up @@ -546,7 +549,7 @@ IGUANA_INLINE void from_json_variant(U &value, It &it, It &end,
end = temp_end;
}

template <typename U, typename It, std::enable_if_t<variant_v<U>, int> = 0>
template <typename U, typename It, std::enable_if_t<variant_v<U>, int>>
IGUANA_INLINE void from_json_impl(U &value, It &&it, It &&end) {
from_json_variant(value, it, end,
std::make_index_sequence<
Expand Down
15 changes: 13 additions & 2 deletions test/test_some.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,22 +217,33 @@ TEST_CASE("test throw while parsing an illegal number") {
}
}

struct my_variant_nest_t {
std::string a;
bool operator==(const my_variant_nest_t &o) const { return o.a == a; }
};
YLT_REFL(my_variant_nest_t, a);

struct my_variant_t {
std::string name;
std::variant<int, std::string> var;
std::vector<std::variant<int, my_variant_nest_t>> var1;
};
YLT_REFL(my_variant_t, name, var);
YLT_REFL(my_variant_t, name, var, var1);

TEST_CASE("test variant") {
std::variant<int, std::string> var;
std::variant<int, my_variant_nest_t> var1;
var = 1;
my_variant_t v{"Hi", var}, v2;
var1 = my_variant_nest_t{"1234"};
my_variant_t v{"Hi", var, {var1}}, v2;
std::string s;
iguana::to_json(v, s);
std::cout << s << std::endl;

iguana::from_json(v2, s);
CHECK(v.var == v2.var);
CHECK(v.var1.size() == v2.var1.size());
CHECK(v.var1[0] == v2.var1[0]);
}

TEST_CASE("test from issues") {
Expand Down

0 comments on commit 46ab4d8

Please sign in to comment.