diff --git a/src/json.hpp b/src/json.hpp index bea5e6167..e7e8444fd 100644 --- a/src/json.hpp +++ b/src/json.hpp @@ -350,7 +350,7 @@ class basic_json // if object is wanted but impossible, throw an exception if (manual_type == value_t::object and not is_object) { - throw std::logic_error("cannot create JSON object"); + throw std::logic_error("cannot create JSON object from initializer list"); } } @@ -379,15 +379,7 @@ class basic_json inline static basic_json object(list_init_t l = list_init_t()) { - // if more than one element is in the initializer list, wrap it - if (l.size() > 1) - { - return basic_json({l}, false, value_t::object); - } - else - { - return basic_json(l, false, value_t::object); - } + return basic_json(l, false, value_t::object); } /////////////////////////////////////// diff --git a/test/unit.cpp b/test/unit.cpp index 1ac0fcf4c..3c0d81850 100644 --- a/test/unit.cpp +++ b/test/unit.cpp @@ -3,6 +3,8 @@ #include "json.hpp" +#include +#include #include using nlohmann::json; @@ -62,6 +64,49 @@ TEST_CASE() json array_not_object = { json::array({"currency", "USD"}), json::array({"value", 42.99}) }; std::cerr << "array_not_object: " << array_not_object << std::endl; } + { + CHECK_THROWS_AS(json::object({1, 2, 3}), std::logic_error); + } + { + CHECK(json::object({{"foo", 1}, {"bar", 2}, {"baz", 3}}).size() == 3); + CHECK(json::object({{"foo", 1}}).size() == 1); + CHECK(json::object().size() == 0); + } + { + json j = json::object({{"foo", 1}, {"bar", 2}, {"baz", 3}}); + { + CHECK(j["foo"] == json(1)); + CHECK(j.at("foo") == json(1)); + } + { + std::map m = j; + auto k = j.get>(); + CHECK(m == k); + } + { + std::unordered_map m = j; + auto k = j.get>(); + CHECK(m == k); + } + } + + { + json j = {1, 2, 3, 4, 5}; + { + CHECK(j[0] == json(1)); + CHECK(j.at(0) == json(1)); + } + { + std::vector m = j; + auto k = j.get>(); + CHECK(m == k); + } + { + std::set m = j; + auto k = j.get>(); + CHECK(m == k); + } + } } TEST_CASE("null")