1 // ANNA - Anna is Not Nothingness Anymore //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
9 #ifndef anna_json_SaxConsumer_hpp
10 #define anna_json_SaxConsumer_hpp
15 #include <anna/json/json.hpp> // nlohmann::json (https://github.com/nlohmann/json)
16 #include <anna/core/util/defines.hpp>
22 class SaxConsumer : public nlohmann::json::json_sax_t
26 std::stringstream result_;
27 std::stringstream current_object_;
28 std::stack<std::string> nodes_stack_;
32 const std::string & get_top() { return nodes_stack_.top(); }
37 SaxConsumer(char attrPrefix = '@') : attr_prefix_(attrPrefix),
38 has_attributes_(false),
39 indent_(-ANNA_XML_INDENTATION_SPACES) {};
41 const std::stringstream & getResult() const { return result_; }
45 current_object_ << "<null>";
46 has_attributes_ = true;
50 bool boolean(bool val) override
52 current_object_ << std::quoted(val ? "true" : "false");
53 has_attributes_ = true;
57 bool number_integer(number_integer_t val) override
59 current_object_ << std::quoted(std::to_string(val));
60 has_attributes_ = true;
64 bool number_unsigned(number_unsigned_t val) override
66 current_object_ << std::quoted(std::to_string(val));
67 has_attributes_ = true;
71 bool number_float(number_float_t val, const string_t& s) override
73 current_object_ << std::quoted(s);
74 has_attributes_ = true;
78 bool string(string_t& val) override
80 current_object_ << std::quoted(val);
81 has_attributes_ = true;
85 bool start_object(std::size_t elements) override
87 if (key_ == "") return true; // global object condition (first start object)
88 nodes_stack_.push(key_); // push on starts
89 indent_ += ANNA_XML_INDENTATION_SPACES; // increase indentation on object start
92 // New object when previous hadn't attributes:
93 if (current_object_.str().empty() && !has_attributes_) result_ <<">";
96 result_ << std::string(indent_, ' ') << "<" << get_top();
98 has_attributes_ = false;
102 bool end_object() override
104 if (indent_ < 0) return true;
105 if (current_object_.str().empty()) {
106 result_ << "\n" << std::string(indent_, ' ') << "</" << get_top() << ">";
109 result_ << current_object_.str() << "/>";
110 current_object_.str("");
113 nodes_stack_.pop(); // pop on ends
114 indent_ -= ANNA_XML_INDENTATION_SPACES; // decrease indentation on object end
118 bool start_array(std::size_t elements) override
120 nodes_stack_.push(key_); // push on starts
122 result_ << current_object_.str();
124 current_object_.str("");
125 has_attributes_ = false;
130 bool end_array() override
132 nodes_stack_.pop(); // pop on ends
136 bool key(string_t& val) override
138 if (val[0] != attr_prefix_) {
142 current_object_ << " " << val.substr(1) << "=";
147 bool parse_error(std::size_t position, const std::string& last_token, const nlohmann::json::exception& ex) override
149 result_ << std::string(ex.what());