C++ Json实现外部参数配置

在项目开发过程中经常会通过配置文件去设置一些外部参数,前期也有讲到过用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

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值