From 76f52037b562552c6dcce3cfca709e15ad62214c Mon Sep 17 00:00:00 2001 From: MihailRis Date: Thu, 9 May 2024 22:01:38 +0300 Subject: [PATCH] added operator<< overloads for dynamic::Value --- src/coders/json.cpp | 16 +++++++++++++--- src/coders/json.hpp | 6 ++++++ src/data/dynamic.cpp | 17 +++++++++++++++++ src/data/dynamic.hpp | 4 ++++ 4 files changed, 40 insertions(+), 3 deletions(-) diff --git a/src/coders/json.cpp b/src/coders/json.cpp index b8db4402..f6a7a19e 100644 --- a/src/coders/json.cpp +++ b/src/coders/json.cpp @@ -52,7 +52,7 @@ void stringifyObj( bool nice ); -void stringify( +void stringifyValue( const Value& value, std::stringstream& ss, int indent, @@ -74,7 +74,7 @@ void stringify( if (i > 0 || nice) { newline(ss, nice, indent, indentstr); } - stringify(value, ss, indent+1, indentstr, nice); + stringifyValue(value, ss, indent+1, indentstr, nice); if (i + 1 < list->size()) { ss << ','; } @@ -114,7 +114,7 @@ void stringifyObj( } const Value& value = entry.second; ss << util::escape(key) << ": "; - stringify(value, ss, indent+1, indentstr, nice); + stringifyValue(value, ss, indent+1, indentstr, nice); index++; if (index < obj->values.size()) { ss << ','; @@ -136,6 +136,16 @@ std::string json::stringify( return ss.str(); } +std::string json::stringify( + const dynamic::Value& value, + bool nice, + const std::string& indent +) { + std::stringstream ss; + stringifyValue(value, ss, 1, indent, nice); + return ss.str(); +} + Parser::Parser(std::string_view filename, std::string_view source) : BasicParser(filename, source) { } diff --git a/src/coders/json.hpp b/src/coders/json.hpp index 6dfe9c8e..692117d5 100644 --- a/src/coders/json.hpp +++ b/src/coders/json.hpp @@ -22,6 +22,12 @@ namespace json { bool nice, const std::string& indent ); + + std::string stringify( + const dynamic::Value& value, + bool nice, + const std::string& indent + ); } #endif // CODERS_JSON_HPP_ diff --git a/src/data/dynamic.cpp b/src/data/dynamic.cpp index 228e044a..f1782688 100644 --- a/src/data/dynamic.cpp +++ b/src/data/dynamic.cpp @@ -1,7 +1,24 @@ #include "dynamic.hpp" +#include "../coders/json.hpp" + using namespace dynamic; +std::ostream& operator<<(std::ostream& stream, const dynamic::Value& 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_sptr& value) { + stream << json::stringify(value, false, " "); + return stream; +} + std::string List::str(size_t index) const { const auto& value = values[index]; switch (static_cast(value.index())) { diff --git a/src/data/dynamic.hpp b/src/data/dynamic.hpp index 3fa49d8b..39bb8fec 100644 --- a/src/data/dynamic.hpp +++ b/src/data/dynamic.hpp @@ -137,4 +137,8 @@ namespace dynamic { }; } +std::ostream& operator<<(std::ostream& stream, const dynamic::Value& value); +std::ostream& operator<<(std::ostream& stream, const dynamic::Map_sptr& value); +std::ostream& operator<<(std::ostream& stream, const dynamic::List_sptr& value); + #endif // DATA_DYNAMIC_HPP_