From 1c24ee8e166f6a407a42cb646cf01fa2b4fbaa38 Mon Sep 17 00:00:00 2001 From: MistEO Date: Thu, 26 Oct 2023 23:06:05 +0800 Subject: [PATCH] feat: one-step constructure (#30) (fix #25) Co-authored-by: zzyyyl --- include/json.hpp | 14 +++++++++++++- sample/sample.cpp | 6 +++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/include/json.hpp b/include/json.hpp index a5dff33..8cc0001 100644 --- a/include/json.hpp +++ b/include/json.hpp @@ -107,7 +107,19 @@ class basic_value template basic_value(value_type type, args_t&&... args); - template >>> + template ::value_type, + utils::range_value_t>, + bool> = true> + basic_value(collection_t&& collection) : basic_value(basic_array(std::forward(collection))) + {} + template ::value_type, + utils::range_value_t>, + bool> = true> + basic_value(map_t&& map) : basic_value(basic_object(std::forward(map))) + {} + + template >, bool> = true> basic_value(value_t) = delete; // I don't know if you want to convert char to string or number, so I delete these constructors. diff --git a/sample/sample.cpp b/sample/sample.cpp index 7e655eb..1cc89d4 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);