diff --git a/include/json.hpp b/include/json.hpp index 41546ae..b91e19e 100644 --- a/include/json.hpp +++ b/include/json.hpp @@ -92,14 +92,13 @@ class basic_value basic_value(basic_object obj); basic_value(std::initializer_list>> init_list); + template + basic_value(value_t&& val); + // Constructed from raw data template basic_value(value_type type, args_t&&... args); - // Prohibit conversion of other types to basic_value - template - basic_value(value_t) = delete; - ~basic_value(); bool valid() const noexcept { return _type != value_type::invalid; } @@ -708,6 +707,25 @@ MEOJSON_INLINE basic_value::basic_value( ; } +template +template +MEOJSON_INLINE basic_value::basic_value(value_t&& val) +{ + constexpr bool is_array_val = std::is_constructible_v, value_t>; + constexpr bool is_object_val = std::is_constructible_v, value_t>; + + static_assert(is_array_val || is_object_val, "value can not constructure a array or object"); + + if constexpr (is_array_val) { + _type = value_type::array; + _raw_data = std::make_unique>(std::forward(val)); + } + else if constexpr (is_object_val) { + _type = value_type::object; + _raw_data = std::make_unique>(std::forward(val)); + } +} + // for Pimpl template MEOJSON_INLINE basic_value::~basic_value() = default; diff --git a/sample/sample.cpp b/sample/sample.cpp index 3aeb740..6c58991 100644 --- a/sample/sample.cpp +++ b/sample/sample.cpp @@ -227,16 +227,16 @@ bool serializing() root["arr"] += json::array { 6, 7 }; std::vector vec = { 1, 2, 3, 4, 5 }; - root["arr from vec"] = json::array(vec); + root["arr from vec"] = vec; std::set set = { "a", "bb\n\nb", "cc\t" }; - root["arr from set"] = json::array(set); + root["arr from set"] = set; std::map map { { "key1", 1 }, { "key2", 2 }, }; - root["obj from map"] = json::object(map); + root["obj from map"] = map; std::vector>> complex { { { 1, 2, 3 }, { 4, 5 } }, { { 6 }, { 7, 8 } } }; root["complex"] = json::serialize(complex);