c++ rapidjson库的使用

//rapijson
#include 
#include 
#include "rapidjson/error/en.h"
#include 


#define has_uint(val, key)    (val.HasMember(key) && val[key].IsUint())
#define has_object(val, key)  (val.HasMember(key) && val[key].IsObject())
#define has_string(val, key)  (val.HasMember(key) && val[key].IsString())

bool Config::parse(const std::string& file_name) {
  using namespace rapidjson;

  std::ifstream ifs(file_name);
  IStreamWrapper isw(ifs);
  Document doc;
  ParseResult ok = doc.ParseStream(isw);
  if (!ok) {
    std::cout << "Config parse error:" << GetParseError_En(ok.Code()) << " " << ok.Offset();
    return false;
  }

  // udp
  if (!has_object(doc, "udp")) {
        std::cout  << "Config parse error:udp";
    return false;
  }
  auto udp = doc["udp"].GetObject();
  if (!has_string(udp, "listen_ip")|| !has_uint(udp, "listen_port")) {
        std::cout  << "Config parse error:udp ip:port";
    return false;
  }
  listen_ip_ = udp["listen_ip"].GetString();
  listen_port_ = udp["listen_port"].GetUint();
  if (has_uint(udp, "interval_msec")) {
    interval_msec_ = udp["interval_msec"].GetUint();
  }

  return true;
}

你可能感兴趣的:(c++)