Skip to content

Commit

Permalink
from json support variant
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos committed Mar 22, 2024
1 parent dceed1e commit e35cd31
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
8 changes: 8 additions & 0 deletions iguana/json_reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,14 @@ IGUANA_INLINE void skip_object_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) {
std::visit(
[&](auto &val) {
from_json_impl(val, it, end);
},
value);
}
} // namespace detail

template <typename T, typename It, std::enable_if_t<refletable_v<T>, int>>
Expand Down
29 changes: 29 additions & 0 deletions test/test.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include <cstddef>
#include <string>
#include <vector>

#include "iguana/reflection.hpp"
#define DOCTEST_CONFIG_IMPLEMENT
#include <iguana/json_util.hpp>
#include <iguana/json_writer.hpp>
Expand Down Expand Up @@ -161,6 +163,33 @@ void get_value_test_helper(const std::string &json_str, const T &expect) {
CHECK(actual == expect);
}

namespace my_space {
struct inner_struct {
int x;
int y;
int z;
};
REFLECTION(inner_struct, x, y, z);
} // namespace my_space

struct nest_t {
std::string name;
my_space::inner_struct value;
std::variant<int, std::string> var;
};
REFLECTION(nest_t, name, value, var);

TEST_CASE("test variant") {
std::variant<int, std::string> var;
var = 1;
nest_t v{"Hi", {1, 2, 3}, var}, v2;
std::string s;
iguana::to_json(v, s);
std::cout << s << std::endl;
iguana::from_json(v2, s);
CHECK(v.name == v2.name);
}

TEST_CASE("test from issues") {
test test1{};
std::string str1 =
Expand Down

0 comments on commit e35cd31

Please sign in to comment.