From 5241c91cc8eb54c6b17ce8c0752dd4da3e6f1f1c Mon Sep 17 00:00:00 2001 From: MihailRis Date: Thu, 12 Sep 2024 12:35:24 +0300 Subject: [PATCH] add new json::stringify overloads --- src/coders/json.cpp | 71 +++++++++++++++++++++++++++++++------------- src/coders/json.hpp | 4 +++ src/data/dynamic.cpp | 11 +++++++ src/data/dynamic.hpp | 2 ++ 4 files changed, 67 insertions(+), 21 deletions(-) diff --git a/src/coders/json.cpp b/src/coders/json.cpp index ac18eb3c..ea2d33c9 100644 --- a/src/coders/json.cpp +++ b/src/coders/json.cpp @@ -44,6 +44,14 @@ void stringifyObj( bool nice ); +void stringifyArr( + const List* list, + std::stringstream& ss, + int indent, + const std::string& indentstr, + bool nice +); + void stringifyValue( const Value& value, std::stringstream& ss, @@ -54,26 +62,7 @@ void stringifyValue( if (auto map = std::get_if(&value)) { stringifyObj(map->get(), ss, indent, indentstr, nice); } else if (auto listptr = std::get_if(&value)) { - auto list = *listptr; - if (list->size() == 0) { - ss << "[]"; - return; - } - ss << '['; - for (uint i = 0; i < list->size(); i++) { - Value& value = list->get(i); - if (i > 0 || nice) { - newline(ss, nice, indent, indentstr); - } - stringifyValue(value, ss, indent + 1, indentstr, nice); - if (i + 1 < list->size()) { - ss << ','; - } - } - if (nice) { - newline(ss, true, indent - 1, indentstr); - } - ss << ']'; + stringifyArr(listptr->get(), ss, indent, indentstr, nice); } else if (auto flag = std::get_if(&value)) { ss << (*flag ? "true" : "false"); } else if (auto num = std::get_if(&value)) { @@ -87,6 +76,38 @@ void stringifyValue( } } +void stringifyArr( + const List* list, + std::stringstream& ss, + int indent, + const std::string& indentstr, + bool nice +) { + if (list == nullptr) { + ss << "nullptr"; + return; + } + if (list->values.empty()) { + ss << "[]"; + return; + } + ss << "["; + for (size_t i = 0; i < list->size(); i++) { + if (i > 0 || nice) { + newline(ss, nice, indent, indentstr); + } + const Value& value = list->values[i]; + stringifyValue(value, ss, indent + 1, indentstr, nice); + if (i + 1 < list->size()) { + ss << ','; + } + } + if (nice) { + newline(ss, true, indent - 1, indentstr); + } + ss << ']'; +} + void stringifyObj( const Map* obj, std::stringstream& ss, @@ -103,7 +124,7 @@ void stringifyObj( return; } ss << "{"; - uint index = 0; + size_t index = 0; for (auto& entry : obj->values) { const std::string& key = entry.first; if (index > 0 || nice) { @@ -131,6 +152,14 @@ std::string json::stringify( return ss.str(); } +std::string json::stringify( + const dynamic::List* arr, bool nice, const std::string& indent +) { + std::stringstream ss; + stringifyArr(arr, ss, 1, indent, nice); + return ss.str(); +} + std::string json::stringify( const dynamic::Value& value, bool nice, const std::string& indent ) { diff --git a/src/coders/json.hpp b/src/coders/json.hpp index ef48ac37..1491be70 100644 --- a/src/coders/json.hpp +++ b/src/coders/json.hpp @@ -14,6 +14,10 @@ namespace json { const dynamic::Map* obj, bool nice, const std::string& indent ); + std::string stringify( + const dynamic::List* arr, bool nice, const std::string& indent + ); + std::string stringify( const dynamic::Value& value, bool nice, const std::string& indent ); diff --git a/src/data/dynamic.cpp b/src/data/dynamic.cpp index f0d60bf1..f49a0aee 100644 --- a/src/data/dynamic.cpp +++ b/src/data/dynamic.cpp @@ -9,11 +9,22 @@ std::ostream& operator<<(std::ostream& stream, const dynamic::Value& value) { return stream; } +std::ostream& operator<<(std::ostream& stream, const dynamic::Map& value) { + stream << json::stringify(&value, false, " "); + return stream; +} + std::ostream& operator<<(std::ostream& stream, const dynamic::Map_sptr& value) { stream << json::stringify(value, false, " "); return stream; } +std::ostream& operator<<(std::ostream& stream, const dynamic::List& value) { + stream << json::stringify(&value, false, " "); + return stream; +} + + std::ostream& operator<<( std::ostream& stream, const dynamic::List_sptr& value ) { diff --git a/src/data/dynamic.hpp b/src/data/dynamic.hpp index 65320fbb..9525a898 100644 --- a/src/data/dynamic.hpp +++ b/src/data/dynamic.hpp @@ -160,5 +160,7 @@ namespace dynamic { } std::ostream& operator<<(std::ostream& stream, const dynamic::Value& value); +std::ostream& operator<<(std::ostream& stream, const dynamic::Map& value); std::ostream& operator<<(std::ostream& stream, const dynamic::Map_sptr& value); +std::ostream& operator<<(std::ostream& stream, const dynamic::List& value); std::ostream& operator<<(std::ostream& stream, const dynamic::List_sptr& value);