From c0965f70207096109d655c90465bce9a0979f815 Mon Sep 17 00:00:00 2001 From: MistEO Date: Sat, 21 Oct 2023 06:35:54 +0800 Subject: [PATCH] feat: crazy serialization --- include/json.hpp | 9 +++++---- sample/sample.cpp | 6 +++--- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/include/json.hpp b/include/json.hpp index fa6d4d8..571d575 100644 --- a/include/json.hpp +++ b/include/json.hpp @@ -2577,21 +2577,22 @@ namespace _serialization_helper 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 { if constexpr (std::is_constructible_v) { return string_t(std::forward(arg)); } - else if constexpr (stream && has_output_operator::value) { + else if constexpr (!loose) { + static_assert(!sizeof(input_t), "Unable to convert type to string."); + } + else if constexpr (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."); + return serialize(std::forward(arg), *this).dumps(); } } }; diff --git a/sample/sample.cpp b/sample/sample.cpp index 3aeb740..77d2b98 100644 --- a/sample/sample.cpp +++ b/sample/sample.cpp @@ -241,9 +241,9 @@ bool serializing() std::vector>> complex { { { 1, 2, 3 }, { 4, 5 } }, { { 6 }, { 7, 8 } } }; root["complex"] = json::serialize(complex); - std::map>> more_complex { - { "key1", { { 1, { 0.1, 0.2 } }, { 2, { 0.2, 0.3 } } } }, - { "key2", { { 3, { 0.4 } }, { 4, { 0.5, 0.6, 0.7 } } } }, + std::map, std::map>> more_complex { + { { "i", "am", "key1" }, { { 1, { 0.1, 0.2 } }, { 2, { 0.2, 0.3 } } } }, + { { "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 "loose" of "serialize" to true, which will make a more relaxed conversion.