Add sax converter from json to xml
[anna.git] / include / anna / json / SaxConsumer.hpp
1 // ANNA - Anna is Not Nothingness Anymore                                                         //
2 //                                                                                                //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo                         //
4 //                                                                                                //
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 //
7
8
9 #ifndef anna_json_SaxConsumer_hpp
10 #define anna_json_SaxConsumer_hpp
11
12 #include <iomanip>
13 #include <sstream>
14 #include <stack>
15 #include <anna/json/json.hpp> // nlohmann::json
16 #include <anna/core/util/defines.hpp>
17
18 namespace anna {
19
20 namespace json {
21
22 class SaxConsumer : public nlohmann::json::json_sax_t
23 {
24   int indent_;
25   bool started_;
26   std::stringstream result_;
27   std::stringstream current_object_;
28   std::stack<std::string> nodes_stack_;
29
30   public:
31
32   SaxConsumer() : started_(false), indent_(-ANNA_XML_INDENTATION_SPACES) {}
33
34   const std::stringstream & getResult() const { return result_; }
35
36   bool null() override
37   {
38     current_object_ << "<null>";
39     return true;
40   }
41
42   bool boolean(bool val) override
43   {
44     current_object_ << std::quoted(val ? "true" : "false");
45     return true;
46   }
47
48   bool number_integer(number_integer_t val) override
49   {
50     current_object_ << std::quoted(std::to_string(val));
51     return true;
52   }
53
54   bool number_unsigned(number_unsigned_t val) override
55   {
56     current_object_ << std::quoted(std::to_string(val));
57     return true;
58   }
59
60   bool number_float(number_float_t val, const string_t& s) override
61   {
62     current_object_ << std::quoted(s);
63     return true;
64   }
65
66   bool string(string_t& val) override
67   {
68     current_object_ << std::quoted(val);
69     return true;
70   }
71
72   bool start_object(std::size_t elements) override
73   {
74     if (!started_) { started_ = true ; return true; } // ignore first start object (which is whole json object {)
75     indent_ += ANNA_XML_INDENTATION_SPACES;
76     result_ << std::string(indent_, ' ') << "<" << nodes_stack_.top();
77     return true;
78   }
79
80   bool end_object() override
81   {
82     std::string close = "/>";
83     if (current_object_.str().empty()) close = "";
84     result_ << current_object_.str() << close;
85     if (indent_ < 0) return true;
86     indent_ -= ANNA_XML_INDENTATION_SPACES;
87     if (close == "") result_ << "</" << nodes_stack_.top() << ">";
88     result_ << "\n";
89     current_object_.str("");
90     return true;
91   }
92
93   bool start_array(std::size_t elements) override
94   {
95     result_ << current_object_.str() << ">\n";
96     current_object_.str("");
97     return true;
98   }
99
100   bool end_array() override
101   {
102     result_ << std::string(indent_, ' ');
103     nodes_stack_.pop();
104     return true;
105   }
106
107   bool key(string_t& val) override
108   {
109     if (val[0] != '@') {
110       nodes_stack_.push(val);
111     }
112     else {
113       current_object_ << " " << val.substr(1) << "=";
114     }
115     return true;
116   }
117
118   bool parse_error(std::size_t position, const std::string& last_token, const nlohmann::json::exception& ex) override
119   {
120     result_ << std::string(ex.what());
121     return false;
122   }
123 };
124
125 }
126 }
127
128 #endif