diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Json.cpp | 75 | ||||
-rw-r--r-- | lib/Json.h | 4 |
2 files changed, 50 insertions, 29 deletions
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 <https://www.gnu.org/licenses/>. */ -#include <list> #include <string> #include <iostream> #include <sstream> @@ -66,33 +65,7 @@ namespace json { } ss << "\"" << escape_json(element.first) << "\": "; - - const auto& value = element.second.v; - if (std::holds_alternative<std::string>(value)) { - ss << "\"" << escape_json(std::get<std::string>(value)) << "\""; - } - else if (std::holds_alternative<double>(value)) { - ss << std::fixed << std::get<double>(value); - } - else if (std::holds_alternative<ssize_t>(value)) { - ss << std::get<ssize_t>(value); - } - else if (std::holds_alternative<size_t>(value)) { - ss << std::get<size_t>(value); - } - else if (std::holds_alternative<bool>(value)) { - ss << (std::get<bool>(value) ? "true" : "false"); - } - else if (std::holds_alternative<std::nullopt_t>(value)) { - ss << "null"; - } - else if (std::holds_alternative<std::shared_ptr<json::map_t> >(value)) { - const map_t& v = *std::get<std::shared_ptr<json::map_t> >(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<std::string>(value.v)) { + ss << "\"" << escape_json(std::get<std::string>(value.v)) << "\""; + } + else if (std::holds_alternative<double>(value.v)) { + ss << std::fixed << std::get<double>(value.v); + } + else if (std::holds_alternative<ssize_t>(value.v)) { + ss << std::get<ssize_t>(value.v); + } + else if (std::holds_alternative<size_t>(value.v)) { + ss << std::get<size_t>(value.v); + } + else if (std::holds_alternative<bool>(value.v)) { + ss << (std::get<bool>(value.v) ? "true" : "false"); + } + else if (std::holds_alternative<std::nullopt_t>(value.v)) { + ss << "null"; + } + else if (std::holds_alternative<std::vector<json::value_t> >(value.v)) { + const auto& vec = std::get<std::vector<json::value_t> >(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<std::shared_ptr<json::map_t> >(value.v)) { + const map_t& v = *std::get<std::shared_ptr<json::map_t> >(value.v); + ss << map_to_json(v); + } + else { + throw std::logic_error("variant alternative not handled"); + } + + return ss.str(); + } } @@ -31,7 +31,7 @@ # include "config.h" #endif -#include <list> +#include <vector> #include <memory> #include <optional> #include <stdexcept> @@ -47,6 +47,7 @@ namespace json { struct value_t { std::variant< std::shared_ptr<std::unordered_map<std::string, value_t>>, + std::vector<value_t>, std::string, double, size_t, @@ -58,4 +59,5 @@ namespace json { using map_t = std::unordered_map<std::string, value_t>; std::string map_to_json(const map_t& values); + std::string value_to_json(const value_t& value); } |