void json_encode()

in cpp/src/parse.cpp [97:153]


void json_encode(boost::any val, stringstream &ss) {
  if (typeid(map<string, boost::any>) == val.type()) {
    map<string, boost::any> m = boost::any_cast<map<string, boost::any>>(val);
    ss << '{';
    if (!m.empty()) {
      int n = 0;
      for (const auto &it : m) {
        if (n != 0) {
          ss << ", ";
        }
        ss << '"' << it.first << '"' << ": ";
        json_encode(it.second, ss);
        n++;
      }
    }
    ss << '}';
  } else if (typeid(vector<boost::any>) == val.type()) {
    vector<boost::any> v = boost::any_cast<vector<boost::any>>(val);
    ss << '[';
    if (!v.empty()) {
      int n = 0;
      for (const auto &it : v) {
        if (n != 0) {
          ss << ", ";
        }
        json_encode(it, ss);
        n++;
      }
    }
    ss << ']';
  } else if (typeid(int) == val.type()) {
    int i = boost::any_cast<int>(val);
    ss << to_string(i);
  } else if (typeid(long) == val.type()) {
    long l = boost::any_cast<long>(val);
    ss << to_string(l);
  } else if (typeid(double) == val.type()) {
    auto d = boost::any_cast<double>(val);
    ss << to_string(d);
  } else if (typeid(float) == val.type()) {
    auto f = boost::any_cast<float>(val);
    ss << to_string(f);
  } else if (typeid(string) == val.type()) {
    auto s = boost::any_cast<string>(val);
    ss << '"' << s << '"';
  } else if (typeid(bool) == val.type()) {
    auto b = boost::any_cast<bool>(val);
    string c = b ? "true" : "false";
    ss << c;
  } else if (typeid(const char *) == val.type()) {
    auto s = boost::any_cast<const char *>(val);
    ss << '"' << s << '"';
  } else if (typeid(char *) == val.type()) {
    auto s = boost::any_cast<char *>(val);
    ss << '"' << s << '"';
  }
}