diff --git a/header_only/json.h b/header_only/json.h index 824287acf..37b6f64be 100644 --- a/header_only/json.h +++ b/header_only/json.h @@ -151,6 +151,25 @@ class json : type_(value_type::number_float), value_(static_cast(n)) {} + /// create an array object + template ::value and + not std::is_same::value and + std::is_constructible::value, int>::type + = 0> + json(const V& v) : json(array_t(v.begin(), v.end())) + {} + + /// create a JSON object + template ::value and + std::is_constructible::value, int>::type + = 0> + json(const V& v) : json(object_t(v.begin(), v.end())) + {} + /// copy constructor json(const json&); /// move constructor diff --git a/src/json.h b/src/json.h index 65c776861..ec7343de0 100644 --- a/src/json.h +++ b/src/json.h @@ -151,6 +151,25 @@ class json : type_(value_type::number_float), value_(static_cast(n)) {} + /// create an array object + template ::value and + not std::is_same::value and + std::is_constructible::value, int>::type + = 0> + json(const V& v) : json(array_t(v.begin(), v.end())) + {} + + /// create a JSON object + template ::value and + std::is_constructible::value, int>::type + = 0> + json(const V& v) : json(object_t(v.begin(), v.end())) + {} + /// copy constructor json(const json&); /// move constructor diff --git a/test/json_unit.cc b/test/json_unit.cc index dcb1a6352..01d796096 100644 --- a/test/json_unit.cc +++ b/test/json_unit.cc @@ -3,6 +3,15 @@ #include "json.h" +#include +#include +#include +#include +#include +#include +#include +#include + using json = nlohmann::json; TEST_CASE("array") @@ -324,6 +333,54 @@ TEST_CASE("array") CHECK(a1 == json({"one", "two", "three"})); CHECK(a2 == json({1, 2, 3, 4})); } + + SECTION("Construct from sequence objects") + { + std::vector c_vector {1, 2, 3, 4}; + json j_vec(c_vector); + CHECK(j_vec.type() == json::value_type::array); + CHECK(j_vec.size() == 4); + + std::set c_set {"one", "two", "three", "four", "one"}; + json j_set(c_set); + CHECK(j_set.type() == json::value_type::array); + CHECK(j_set.size() == 4); + + std::unordered_set c_uset {"one", "two", "three", "four", "one"}; + json j_uset(c_uset); + CHECK(j_uset.type() == json::value_type::array); + CHECK(j_uset.size() == 4); + + std::multiset c_mset {"one", "two", "one", "four"}; + json j_mset(c_mset); + CHECK(j_mset.type() == json::value_type::array); + CHECK(j_mset.size() == 4); + + std::unordered_multiset c_umset {"one", "two", "one", "four"}; + json j_umset(c_umset); + CHECK(j_umset.type() == json::value_type::array); + CHECK(j_umset.size() == 4); + + std::deque c_deque {1.2, 2.3, 3.4, 5.6}; + json j_deque(c_deque); + CHECK(j_deque.type() == json::value_type::array); + CHECK(j_deque.size() == 4); + + std::list c_list {true, true, false, true}; + json j_list(c_list); + CHECK(j_list.type() == json::value_type::array); + CHECK(j_list.size() == 4); + + std::forward_list c_flist {12345678909876, 23456789098765, 34567890987654, 45678909876543}; + json j_flist(c_flist); + CHECK(j_flist.type() == json::value_type::array); + CHECK(j_flist.size() == 4); + + std::array c_array {{1, 2, 3, 4}}; + json j_array(c_array); + CHECK(j_array.type() == json::value_type::array); + CHECK(j_array.size() == 4); + } } TEST_CASE("object") @@ -744,6 +801,29 @@ TEST_CASE("object") CHECK(o1 == json({ {"one", "eins"}, {"two", "zwei"} })); CHECK(o2 == json({ {"one", 1}, {"two", 2} })); } + + SECTION("Construct from sequence objects") + { + std::map c_map { {"one", 1}, {"two", 2}, {"three", 3} }; + json j_map(c_map); + CHECK(j_map.type() == json::value_type::object); + CHECK(j_map.size() == 3); + + std::unordered_map c_umap { {"one", 1.2}, {"two", 2.3}, {"three", 3.4} }; + json j_umap(c_umap); + CHECK(j_umap.type() == json::value_type::object); + CHECK(j_umap.size() == 3); + + std::multimap c_mmap { {"one", true}, {"two", true}, {"three", false}, {"three", true} }; + json j_mmap(c_mmap); + CHECK(j_mmap.type() == json::value_type::object); + CHECK(j_mmap.size() == 3); + + std::unordered_multimap c_ummap { {"one", true}, {"two", true}, {"three", false}, {"three", true} }; + json j_ummap(c_ummap); + CHECK(j_ummap.type() == json::value_type::object); + CHECK(j_ummap.size() == 3); + } } TEST_CASE("null")