Skip to content

Instantly share code, notes, and snippets.

@paulgessinger
Last active February 9, 2018 12:35
Show Gist options
  • Save paulgessinger/a36bd2733644776731f9e8be9a2fe40e to your computer and use it in GitHub Desktop.
Save paulgessinger/a36bd2733644776731f9e8be9a2fe40e to your computer and use it in GitHub Desktop.
// This file is a "Hello, world!" in C++ language by GCC for wandbox.
#include <iostream>
#include <cstdlib>
#include <boost/variant.hpp>
#include <map>
#include <vector>
#include <sstream>
typedef boost::make_recursive_variant<
int,
double,
std::string,
bool,
std::map< std::string, boost::recursive_variant_ >,
std::vector< boost::recursive_variant_ >
>::type gen_structure;
typedef std::map<std::string, gen_structure> gen_map;
typedef std::vector<gen_structure> gen_vector;
class json_visitor
: public boost::static_visitor<>
{
public:
json_visitor(bool pretty_ = false) : json_str(std::stringstream()), pretty(pretty_) {
}
void operator()(const bool& b) {
if(b) {
json_str << "true";
}
else {
json_str << "false";
}
}
void operator()(const int& i)
{
json_str << i;
}
void operator()(const double& d)
{
json_str << d;
}
void operator()(const std::string& str)
{
json_str << "\"" << str << "\"";
}
void operator()(const gen_map& map)
{
json_str << "{";
if (pretty) json_str << std::endl;
size_t i = 0;
depth += 1;
for(const auto& entry : map) {
indent();
json_str << "\"" << entry.first << "\": ";
boost::apply_visitor(*this, entry.second);
if (i < map.size()-1) {
json_str << ", ";
if (pretty) json_str << std::endl;
}
++i;
}
depth -= 1;
if (pretty) json_str << std::endl;
indent();
json_str << "}";
}
void operator()(const gen_vector& vec)
{
json_str << "[";
if (pretty) json_str << std::endl;
size_t i = 0;
depth += 1;
for(const auto& entry : vec) {
indent();
boost::apply_visitor(*this, entry);
if (i < vec.size()-1) {
json_str << ", ";
if (pretty) json_str << std::endl;
}
++i;
}
depth -= 1;
if (pretty) json_str << std::endl;
indent();
json_str << "]";
}
std::string str() const {
return json_str.str();
}
private:
std::stringstream json_str;
bool pretty;
size_t depth = 0;
void indent() {
if(pretty) {
for(size_t d=0;d<depth;d++) {
json_str << " ";
}
}
}
};
std::ostream& operator<<(std::ostream& os, const json_visitor& jv) {
os << jv.str() << std::endl;
return os;
}
std::ostream& operator<<(std::ostream& os, const gen_structure& data) {
json_visitor jv(true);
boost::apply_visitor(jv, data);
os << jv << std::endl;
return os;
}
int main()
{
using namespace std::string_literals;
std::cout << "Hello, Wandbox!" << std::endl;
auto data = gen_map();
data["kint"] = 5;
data["kdouble"] = 5.3;
data["kstring"] = "whoop"s;
data["kbool"] = true;
data["kvector"] = gen_vector({"a"s, 8, "c"s});
data["kmap"] = gen_map({
{"h", 17},
{"l", "hurz"s},
{"b", false},
{"w", gen_map({
{"m", -1}
})
}
});
gen_structure root = data;
//boost::apply_visitor(print_visitor(), root);
std::cout << std::endl << std::endl;
/*json_visitor jv(true);
boost::apply_visitor(jv, root);
std::cout << jv.str() << std::endl;*/
std::cout << root << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment