add pair/tuple conversions

This commit is contained in:
Théo DELRIEU 2017-06-17 14:24:07 +02:00 committed by Théo DELRIEU
parent c98364834f
commit 6e4910d5c5
No known key found for this signature in database
GPG key ID: C64D5E244E08ADC6
2 changed files with 121 additions and 0 deletions

View file

@ -239,6 +239,58 @@ TEST_CASE("constructors")
CHECK(j == j_reference);
}
SECTION("std::pair")
{
std::pair<float, std::string> p{1.0, "string"};
json j(p);
CHECK(j.type() == json::value_t::array);
REQUIRE(j.size() == 2);
CHECK(j[0] == std::get<0>(p));
CHECK(j[1] == std::get<1>(p));
}
SECTION("std::pair with discarded values")
{
json j{1, 2.0, "string"};
const auto p = j.get<std::pair<int, float>>();
CHECK(p.first == j[0]);
CHECK(p.second == j[1]);
}
SECTION("std::tuple")
{
const auto t = std::make_tuple(1.0, "string", 42, std::vector<int> {0, 1});
json j(t);
CHECK(j.type() == json::value_t::array);
REQUIRE(j.size() == 4);
CHECK(j[0] == std::get<0>(t));
CHECK(j[1] == std::get<1>(t));
CHECK(j[2] == std::get<2>(t));
CHECK(j[3][0] == 0);
CHECK(j[3][1] == 1);
}
SECTION("std::tuple with discarded values")
{
json j{1, 2.0, "string", 42};
const auto t = j.get<std::tuple<int, float, std::string>>();
CHECK(std::get<0>(t) == j[0]);
CHECK(std::get<1>(t) == j[1]);
CHECK(std::get<2>(t) == j[2]);
}
SECTION("std::pair/tuple failures")
{
json j{1};
CHECK_THROWS((j.get<std::pair<int, int>>()));
CHECK_THROWS((j.get<std::tuple<int, int>>()));
}
SECTION("std::forward_list<json>")
{
std::forward_list<json> a {json(1), json(1u), json(2.2), json(false), json("string"), json()};