From c2467d222ec08ddc4c6f79ea01773496090f809f Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Tue, 31 Oct 2023 23:02:10 +0100 Subject: Add FIC decoder, present ensemble info for monitoring --- lib/Json.cpp | 75 +++++++++++++++++++++++++++++++++++++----------------------- lib/Json.h | 4 +++- 2 files changed, 50 insertions(+), 29 deletions(-) (limited to 'lib') diff --git a/lib/Json.cpp b/lib/Json.cpp index b2315b6..4dc2f25 100644 --- a/lib/Json.cpp +++ b/lib/Json.cpp @@ -22,7 +22,6 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . */ -#include #include #include #include @@ -66,33 +65,7 @@ namespace json { } ss << "\"" << escape_json(element.first) << "\": "; - - const auto& value = element.second.v; - if (std::holds_alternative(value)) { - ss << "\"" << escape_json(std::get(value)) << "\""; - } - else if (std::holds_alternative(value)) { - ss << std::fixed << std::get(value); - } - else if (std::holds_alternative(value)) { - ss << std::get(value); - } - else if (std::holds_alternative(value)) { - ss << std::get(value); - } - else if (std::holds_alternative(value)) { - ss << (std::get(value) ? "true" : "false"); - } - else if (std::holds_alternative(value)) { - ss << "null"; - } - else if (std::holds_alternative >(value)) { - const map_t& v = *std::get >(value); - ss << map_to_json(v); - } - else { - throw std::logic_error("variant alternative not handled"); - } + ss << value_to_json(element.second); ix++; } @@ -100,4 +73,50 @@ namespace json { return ss.str(); } + + std::string value_to_json(const value_t& value) + { + std::ostringstream ss; + + if (std::holds_alternative(value.v)) { + ss << "\"" << escape_json(std::get(value.v)) << "\""; + } + else if (std::holds_alternative(value.v)) { + ss << std::fixed << std::get(value.v); + } + else if (std::holds_alternative(value.v)) { + ss << std::get(value.v); + } + else if (std::holds_alternative(value.v)) { + ss << std::get(value.v); + } + else if (std::holds_alternative(value.v)) { + ss << (std::get(value.v) ? "true" : "false"); + } + else if (std::holds_alternative(value.v)) { + ss << "null"; + } + else if (std::holds_alternative >(value.v)) { + const auto& vec = std::get >(value.v); + ss << "[ "; + size_t list_ix = 0; + for (const auto& list_element : vec) { + if (list_ix > 0) { + ss << ","; + } + ss << value_to_json(list_element); + list_ix++; + } + ss << "]"; + } + else if (std::holds_alternative >(value.v)) { + const map_t& v = *std::get >(value.v); + ss << map_to_json(v); + } + else { + throw std::logic_error("variant alternative not handled"); + } + + return ss.str(); + } } diff --git a/lib/Json.h b/lib/Json.h index 706394f..65aa668 100644 --- a/lib/Json.h +++ b/lib/Json.h @@ -31,7 +31,7 @@ # include "config.h" #endif -#include +#include #include #include #include @@ -47,6 +47,7 @@ namespace json { struct value_t { std::variant< std::shared_ptr>, + std::vector, std::string, double, size_t, @@ -58,4 +59,5 @@ namespace json { using map_t = std::unordered_map; std::string map_to_json(const map_t& values); + std::string value_to_json(const value_t& value); } -- cgit v1.2.3