Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

from json support variant #247

Merged
merged 1 commit into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading