From 67a602a43c18c95b5e59a0e901e0794674e90c85 Mon Sep 17 00:00:00 2001 From: MistEO Date: Sat, 21 Oct 2023 05:44:40 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E8=BF=9B=E4=B8=80=E6=AD=A5=E4=BC=98?= =?UTF-8?q?=E5=8C=96=20serialize?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/json.hpp | 56 ++++++++++++++++++++++------------------------- sample/sample.cpp | 2 +- 2 files changed, 27 insertions(+), 31 deletions(-) diff --git a/include/json.hpp b/include/json.hpp index 0c95ae4..59eac9b 100644 --- a/include/json.hpp +++ b/include/json.hpp @@ -565,9 +565,9 @@ namespace literals template const basic_value invalid_value(); -template > -basic_value serialize(any_t&& arg, streamer_t&& streamer = streamer_t()); +template > +basic_value serialize(any_t&& arg, string_converter_t&& string_converter = {}); // ****************************** // * basic_value impl * @@ -2527,7 +2527,7 @@ MEOJSON_INLINE const basic_value invalid_value() return basic_value(basic_value::value_type::invalid, typename basic_value::var_t()); } -namespace _serialization_helper +namespace serialization_helper { template class has_output_operator @@ -2566,40 +2566,36 @@ namespace _serialization_helper template constexpr bool is_sequence_container = is_container && !is_associative_container; - template - struct string_streamer + template + struct string_converter { using char_t = typename string_t::value_type; using ostringstream_t = std::basic_ostringstream, std::allocator>; + static constexpr bool stream = loose; + template string_t operator()(input_t&& arg) const { - ostringstream_t os; - os << std::forward(arg); - return std::move(os).str(); + if constexpr (std::is_constructible_v) { + return arg; + } + else if constexpr (stream && has_output_operator::value) { + ostringstream_t os; + os << std::forward(arg); + return std::move(os).str(); + } + else { + static_assert(!sizeof(input_t), "Unable to convert type to string."); + } } }; - - template - MEOJSON_INLINE string_t to_string(input_t&& arg, streamer_t&& streamer) - { - if constexpr (std::is_constructible_v) { - return arg; - } - else if constexpr (stream) { - return streamer(std::forward(arg)); - } - else { - static_assert(!sizeof(input_t), "Unable to convert type to string."); - } - } } -template -MEOJSON_INLINE basic_value serialize(any_t&& arg, streamer_t&& streamer) +template +MEOJSON_INLINE basic_value serialize(any_t&& arg, string_converter_t&& string_converter) { - using namespace _serialization_helper; + using namespace serialization_helper; if constexpr (std::is_constructible_v, any_t>) { return arg; @@ -2615,7 +2611,7 @@ MEOJSON_INLINE basic_value serialize(any_t&& arg, streamer_t&& streame for (auto&& val : arg) { using value_t = decltype(val); - result.emplace(serialize(std::forward(val), streamer)); + result.emplace(serialize(std::forward(val), string_converter)); } return result; } @@ -2625,13 +2621,13 @@ MEOJSON_INLINE basic_value serialize(any_t&& arg, streamer_t&& streame using key_t = decltype(key); using value_t = decltype(val); - result.emplace(to_string(std::forward(key), streamer), - serialize(std::forward(val), streamer)); + result.emplace(string_converter(std::forward(key)), + serialize(std::forward(val), string_converter)); } return result; } else { - return to_string(std::forward(arg), streamer); + return string_converter(std::forward(arg)); } } diff --git a/sample/sample.cpp b/sample/sample.cpp index 5bddff5..3aeb740 100644 --- a/sample/sample.cpp +++ b/sample/sample.cpp @@ -246,7 +246,7 @@ bool serializing() { "key2", { { 3, { 0.4 } }, { 4, { 0.5, 0.6, 0.7 } } } }, }; // the "std::map" cannot be converted to json because the key is "int", - // you can set the template parameter "stream" of "serialize" to true, which will make a more relaxed conversion. + // you can set the template parameter "loose" of "serialize" to true, which will make a more relaxed conversion. root["more_complex"] = json::serialize(more_complex); // for test