jsoncpp 读写指定值

jsoncpp读写指定值封装

/************************************************************************/
/* json配置文件                                                                     */
/************************************************************************/
class CConfigJson:public Config
{
public:
    CConfigJson(){}
    CConfigJson(const std::string &strFileName);
    std::string GetValue(std::string key) override;//从内存获取某个配置的值
    int SetValue(std::string key, std::string value) override;

protected:
    bool ReadJsonFile(const char *jsonFileName, Json::Value *jsonInfo);
    bool WriteJsonFile(const char *jsonFileName, Json::Value *jsonInfo);
    std::vector<std::string> split(std::string str, std::string pattern);
    void ChangeValue(Json::Value& v, const std::vector<std::string>& vec, int n, std::string value);

private:
    Json::Value root;
    std::string m_strFileName;
};

cpp实现


CConfigJson::CConfigJson(const std::string &strFileName)
{
    m_strFileName = strFileName;
    ReadJsonFile(strFileName.c_str(), &root);
}

std::string CConfigJson::GetValue(std::string key) //从内存获取某个配置的值
{
    Json::Value res = root;
    std::vector<std::string> result = split(key, "/");
    for (int k = 0; k < result.size(); k++)
    {
        res = res[result[k]];
    }


    return res.asString();
}



int CConfigJson::SetValue(std::string key, std::string value)
{
    Json::Value& res = root;

    //递归修改指定值
    std::vector<std::string> result = split(key, "/");
    ChangeValue(res ,result, 0, value);
    WriteJsonFile(m_strFileName.c_str(), &root);
    return 0;
}

bool CConfigJson::ReadJsonFile(const char *jsonFileName, Json::Value *jsonInfo)
{
    std::ifstream jsonFile(jsonFileName, std::ios::binary);
    if (!jsonFile.is_open())
    {
        printf("OPEN %s ERROR\n", jsonFileName);
        return false;
    }
    else
    {
        Json::Reader reader;
        if (reader.parse(jsonFile, *jsonInfo))
        {
            jsonFile.close();
            return true;
        }
        else
        {
            printf("parse json ERROR\n");
            jsonFile.close();
            return false;
        }

    }
    return true;
}

bool CConfigJson::WriteJsonFile(const char *jsonFileName, Json::Value *jsonInfo)
{

    Json::StyledWriter sw;
    ofstream os;
    os.open(jsonFileName, std::ios::out | std::ios::trunc);
    if (!os.is_open())
        cout << "error:can not find or create the file which named \" demo.json\"." << endl;
    os << sw.write(*jsonInfo);
    os.close();

    return true;
}

std::vector<std::string> CConfigJson::split(std::string str, std::string pattern)
{
    std::string::size_type pos;
    std::vector<std::string> result;
    str += pattern;//扩展字符串以方便操作
    int size = str.size();
    for (int i = 0; i < size; i++)
    {
        pos = str.find(pattern, i);
        if (pos < size)
        {
            std::string s = str.substr(i, pos - i);
            result.push_back(s);
            i = pos + pattern.size() - 1;
        }
    }
    return result;
}

void CConfigJson::ChangeValue(Json::Value& v, const std::vector<std::string>& vec, int n, std::string value)
{
    if (n > vec.size())
    {
        return;
    }
    if (v.isMember(vec[vec.size() - 1]))
    {
         if (v[vec[vec.size() - 1]].isInt())
        {
            v[vec[vec.size() - 1]] = Json::Value(std::stoi(value));
        }
        else if (v[vec[vec.size() - 1]].isString())
        {
            v[vec[vec.size() - 1]] = Json::Value(value);
        }
        return;
    }
    else
    {
        ChangeValue(v[vec[n]], vec, n + 1, value);
    }
}

示例

    CCfgInstance::Get()->GetValue("aa/bb");
    CCfgInstance::Get()->SetValue("aa/bb", "x");
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值