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

fix from_json implementation for pair/tuple #708

Merged
merged 1 commit into from
Aug 24, 2017
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: 4 additions & 4 deletions src/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1235,16 +1235,16 @@ void from_json(const BasicJsonType& j, ArithmeticType& val)
}
}

template<typename BasicJsonType, typename... Args>
void from_json(const BasicJsonType& j, std::pair<Args...>& p)
template<typename BasicJsonType, typename A1, typename A2>
void from_json(const BasicJsonType& j, std::pair<A1, A2>& p)
{
p = {j.at(0), j.at(1)};
p = {j.at(0).template get<A1>(), j.at(1).template get<A2>()};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible that we'd have to support something like pair<int,int>? I think you'll save yourself a patch if you use get<0> and get<1> instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I understood your point. Are you talking about std::get?
I don't see how it could be used here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Er, you're right. I was talking about std::get here, which is not what you're doing at all. I mis-read it. What you have looks good!

}

template<typename BasicJsonType, typename Tuple, std::size_t... Idx>
void from_json_tuple_impl(const BasicJsonType& j, Tuple& t, index_sequence<Idx...>)
{
t = std::make_tuple(j.at(Idx)...);
t = std::make_tuple(j.at(Idx).template get<typename std::tuple_element<Idx, Tuple>::type>()...);
}

template<typename BasicJsonType, typename... Args>
Expand Down
4 changes: 3 additions & 1 deletion test/src/unit-constructor1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ TEST_CASE("constructors")
json j(p);

CHECK(j.type() == json::value_t::array);
CHECK(j.get<decltype(p)>() == p);
REQUIRE(j.size() == 2);
CHECK(j[0] == std::get<0>(p));
CHECK(j[1] == std::get<1>(p));
Expand All @@ -262,11 +263,12 @@ TEST_CASE("constructors")

SECTION("std::tuple")
{
const auto t = std::make_tuple(1.0, "string", 42, std::vector<int> {0, 1});
const auto t = std::make_tuple(1.0, std::string{"string"}, 42, std::vector<int> {0, 1});
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need to change "string" to std::string{"string"} here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because it was deduced as a const char* which you can not retrieve with get.

json j(t);

CHECK(j.type() == json::value_t::array);
REQUIRE(j.size() == 4);
CHECK(j.get<decltype(t)>() == t);
CHECK(j[0] == std::get<0>(t));
CHECK(j[1] == std::get<1>(t));
CHECK(j[2] == std::get<2>(t));
Expand Down