first version of mstch to json written

This commit is contained in:
moneroexamples 2018-06-17 10:53:47 +08:00
parent d308b31d74
commit d3e3084272
1 changed files with 43 additions and 45 deletions

View File

@ -273,81 +273,76 @@ struct tx_details
}; };
class my_render_node: public boost::static_visitor<std::string> { class my_render_node: public boost::static_visitor<json> {
public: public:
std::string operator()(const int& value) const { // my_render_node(json& _j) : j {_j}
return std::to_string(value); // {}
json operator()(const int& value) const {
//return std::to_string(value);
return json {value};
} }
std::string operator()(const double& value) const { json operator()(const double& value) const {
std::stringstream ss; return json {value};
ss << value;
return ss.str();
} }
std::string operator()(const uint64_t& value) const { json operator()(const uint64_t& value) const {
std::stringstream ss; return json {value};
ss << value;
return ss.str();
} }
std::string operator()(const int64_t& value) const { json operator()(const int64_t& value) const {
std::stringstream ss; return json {value};
ss << value;
return ss.str();
} }
std::string operator()(const uint32_t& value) const { json operator()(const uint32_t& value) const {
std::stringstream ss; return json {value};
ss << value;
return ss.str();
} }
std::string operator()(const bool& value) const { json operator()(const bool& value) const {
return value ? "true" : "false"; return json {value};
} }
std::string operator()(const string& value) const { json operator()(const string& value) const {
return value; return json {value};
} }
std::string operator()(const mstch::map& n_map) const json operator()(const mstch::map& n_map) const
{ {
std::stringstream ss; json j;
for (auto const& kv: n_map) for (auto const& kv: n_map)
{ {
ss << "k:" << kv.first j[kv.first] = boost::apply_visitor(my_render_node(), kv.second);
<< ", v: " << boost::apply_visitor(my_render_node(), kv.second)
<< " ";
} }
return ss.str(); return j;
} }
std::string operator()(const mstch::array& n_array) const json operator()(const mstch::array& n_array) const
{ {
std::stringstream ss; json j;
for (auto const& v: n_array)
{
string s = boost::apply_visitor(my_render_node(), v);
ss << " a: " << s << " ";
}
return ss.str(); for (auto const& v: n_array)
j.push_back(boost::apply_visitor(my_render_node(), v));
return j;
} }
std::string operator()(const mstch::lambda& value) const { json operator()(const mstch::lambda& value) const {
return "lambda"; return json {"lambda"};
} }
template<class T> template<class T>
std::string operator()(const T&) const { json operator()(const T&) const {
return "unknown type"; return json {};
} }
//private:
// json &j;
}; };
@ -4329,18 +4324,21 @@ public:
std::string view{"{{#bold}}{{yay}} :){{/bold}}"}; std::string view{"{{#bold}}{{yay}} :){{/bold}}"};
json j;
//% boost::apply_visitor(my_render_node(j), kv.second);
for (auto const& kv: tx_context) for (auto const& kv: tx_context)
{ {
//j_data[kv.first] = boost::apply_visitor(render_node2(), kv.second); //j_data[kv.first] = boost::apply_visitor(render_node2(), kv.second);
string a = boost::apply_visitor(my_render_node(), kv.second); //string a = boost::apply_visitor(my_render_node(), kv.second);
j[kv.first] = boost::apply_visitor(my_render_node(), kv.second);
cout << kv.first << " = " << a << endl; //cout << kv.first << " = " << a << endl;
} }
cout << j.dump() << '\n';
j_response["status"] = "success"; j_response["status"] = "success";
return j_response; return j_response;