在项目开发过程中经常会通过配置文件去设置一些外部参数,前期也有讲到过用yaml配置参数,这里写了一个用json配置参数的示例。
配置参数:
{
"info": {
"name": "I am Mr.liu",
"year": 18,
"id": 123456,
"chinese": 98,
"math": 99,
"home_path": "/home/lzy/",
"flag_pass": 0
}
}
头文件:
#include "CJsonObject.hpp"
#ifndef M_CONFIG_H
#define M_CONFIG_H
namespace config
{
#define JSON_STRING_INFO "info"
struct info_config
{
std::string name;
int year;
int id;
double chinese;
double math;
std::string home_path;
int flag_pass;
};
class student_info_config
{
public:
student_info_config() = default;
virtual ~student_info_config() {}
student_info_config(const student_info_config&) = delete;
student_info_config& operator=(const student_info_config&) = delete;
bool load_info_config(const std::string& config_file, CxxJson::CJsonObject& json_object);
void get_info_config(info_config& config);
protected:
CxxJson::CJsonObject m_json_object;
};
}
#endif
实现文件:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include "studentInfo_config.h"
namespace config
{
bool student_info_config::load_info_config(const std::string& config_file, CxxJson::CJsonObject& json_object)
{
bool result = false;
unsigned long file_size = -1;
struct stat statbuff;
if (stat(config_file.c_str(), &statbuff) < 0)
{
printf("stat info config json file failed\n");
return false;
}
else
{
file_size = statbuff.st_size;
}
int fd = open(config_file.c_str(), O_RDONLY);
if (fd > 0)
{
char *p_data_buf = new char[file_size];
int ret = read(fd, p_data_buf, file_size);
if (ret > 0)
{
m_json_object.Parse(p_data_buf);
json_object = m_json_object;
result = true;
}
else
{
printf("read info config json file failed\n");
}
close(fd);
delete[] p_data_buf;
}
else
{
printf("open info config json file failed\n");
}
return result;
}
void student_info_config::get_info_config(info_config& config)
{
config.name = m_json_object[JSON_STRING_INFO]["name"].StringValue();
config.year = m_json_object[JSON_STRING_INFO]["year"].IntValue();
config.id = m_json_object[JSON_STRING_INFO]["id"].IntValue();
config.chinese = m_json_object[JSON_STRING_INFO]["chinese"].DoubleValue();
config.math = m_json_object[JSON_STRING_INFO]["match"].DoubleValue();
config.home_path = m_json_object[JSON_STRING_INFO]["home_path"].StringValue();
config.flag_pass = m_json_object[JSON_STRING_INFO]["flag_pass"].IntValue();
}
}
函数调用
#include <unistd.h>
#include <signal.h>
#include <thread>
#include "studentInfo_config.h"
std::unique_ptr<config::student_info_config> g_config_object;
std::string g_configure_file_path;
int main(int argc, char**argv)
{
if(argc < 3)
{
printf("configure file path param hasn't been set\n");
return -1;
}
else if (std::string(argv[1]) == "-config")
{
g_configure_file_path = std::string(argv[2]);
printf("configure file path:%s\n",g_configure_file_path.c_str());
}
CxxJson::CJsonObject json_object;
g_config_object = std::make_unique<config::student_info_config>();
g_config_object->load_info_config(g_configure_file_path, json_object);
config::info_config config_option;
g_config_object->get_info_config(config_option);
return 0;
}
运行./json_test_node -config ***/configuration_files/student_info.json即可验证。
完整工程链接:https://github.com/liuzy1/C-example-test