From 1f610e91029512c17466a85b41a090141f7bbf39 Mon Sep 17 00:00:00 2001 From: "Eduardo Ramos Testillano (eramedu)" Date: Mon, 4 May 2020 19:36:17 +0200 Subject: [PATCH] Fix bug in json to xml convert library Bad management for node-names stack (FIFO). Both object and array shall push keys and also shall pop them when entering/finishing the callbacks. Also, new member key_ will store the key (and it won't be pushed when detected, only when starting object/array). --- include/anna/json/SaxConsumer.hpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/include/anna/json/SaxConsumer.hpp b/include/anna/json/SaxConsumer.hpp index 110a097..33473ed 100644 --- a/include/anna/json/SaxConsumer.hpp +++ b/include/anna/json/SaxConsumer.hpp @@ -26,6 +26,7 @@ class SaxConsumer : public nlohmann::json::json_sax_t std::stringstream result_; std::stringstream current_object_; std::stack nodes_stack_; + std::string key_; public: @@ -71,6 +72,7 @@ class SaxConsumer : public nlohmann::json::json_sax_t bool start_object(std::size_t elements) override { + nodes_stack_.push(key_); if (!started_) { started_ = true ; return true; } // ignore first start object (which is whole json object {) indent_ += ANNA_XML_INDENTATION_SPACES; result_ << std::string(indent_, ' ') << "<" << nodes_stack_.top(); @@ -87,11 +89,13 @@ class SaxConsumer : public nlohmann::json::json_sax_t if (close == "") result_ << ""; result_ << "\n"; current_object_.str(""); + nodes_stack_.pop(); return true; } bool start_array(std::size_t elements) override { + nodes_stack_.push(key_); result_ << current_object_.str() << ">\n"; current_object_.str(""); return true; @@ -107,7 +111,7 @@ class SaxConsumer : public nlohmann::json::json_sax_t bool key(string_t& val) override { if (val[0] != '@') { - nodes_stack_.push(val); + key_ = val; } else { current_object_ << " " << val.substr(1) << "="; -- 2.20.1