今天复习一下jsoncpp解析库的用法
文章目录
1.Github搜jsoncpp下载工程源码
Github传送门
下载工程源码:
2.解压得到需要使用的两个目录include和src
3. 创建新测试工程:
4.解决方案目录下创建新目录json,存储上文拿到的json文件:
5.测试工程添加筛选器,并且添加进去得到的jsoncpp文件:
6.工程属性,附加包含两个目录:
7.添加解析类 JsoncppAssistant
#pragma once
#include "json.h"
#include <vector>
#include <mutex>
using namespace std;
struct Score
{
string subject;
float score;
bool bChecked;
Score(const string& nm, float fS,bool bV = true)
:subject(nm),
score(fS),
bChecked(bV)
{
bChecked = true;
}
};
struct Student
{
string name;
vector<Score> arr;
};
//两个全局互斥锁保证多线程调用Read()、Write()方法线程安全
extern std::mutex g_read_mutex;
extern std::mutex g_write_mutex;
class JsoncppAssistant
{
private:
JsoncppAssistant() = default;
~JsoncppAssistant() = default;
JsoncppAssistant(const JsoncppAssistant& src) = delete;
JsoncppAssistant& operator=(const JsoncppAssistant& src) = delete;
static JsoncppAssistant* instance;
static std::mutex m_mutex;
public:
/*
双重检查锁定(double-checked locking)模式和互斥锁 m_mutex
确保在多线程环境下只创建一个 JsoncppAssistant
*/
static JsoncppAssistant* GetInstance()
{
if (instance == nullptr)
{
//锁定互斥量
std::lock_guard<std::mutex> lock(m_mutex);
if (instance == nullptr)
{
instance = new JsoncppAssistant();
}
}
return instance;
}
bool Read(vector<Student>& arr, string path = "");
bool Write(const vector<Student>& arr);
};
extern JsoncppAssistant* GetJscppMgr();
#include "JsoncppAssistant.h"
#include <ostream>
#include <fstream>
std::mutex g_read_mutex;
std::mutex g_write_mutex;
JsoncppAssistant* JsoncppAssistant::instance = nullptr;
std::mutex JsoncppAssistant::m_mutex;
JsoncppAssistant* GetJscppMgr()
{
return JsoncppAssistant::GetInstance();
}
bool JsoncppAssistant::Read(vector<Student>& arr, string path)
{
std::lock_guard<std::mutex> lock(g_read_mutex);
arr.clear();
Json::Reader reader;
Json::Value root;
ifstream ifs;
ifs.open("test.json");
if (!ifs.is_open())
{
return false;
}
if (!reader.parse(ifs, root))
{
return false;
}
if (root["data"].isNull()) return false;
int nSize = root["data"].size();
for (int i = 0; i < nSize; i++)
{
Student stu;
stu.name = root["data"][i]["name"].asString();
if (!root["data"][i]["Score"].isNull())
{
int nSz2 = root["data"][i]["Score"].size();
stu.arr.reserve(nSz2);
for (int j = 0; j < nSz2; j++)
{
stu.arr.emplace_back(Score(root["data"][i]["Score"][j]["subject"].asString(),
root["data"][i]["Score"][j]["score"].asFloat(),
root["data"][i]["Score"][j]["bChecked"].asBool()));
}
}
arr.push_back(stu);
}
return true;
}
bool JsoncppAssistant::Write(const vector<Student>& arr)
{
std::lock_guard<std::mutex> lock(g_write_mutex);
if (arr.empty()) return false;
Json::Value root;
for (const auto& it : arr)
{
Json::Value JsStu;
JsStu["name"] = it.name;
for (const auto& it2 : it.arr)
{
Json::Value JsScore;
JsScore["subject"] = it2.subject;
JsScore["score"] = it2.score;
JsScore["bChecked"] = it2.bChecked;
JsStu["Score"].append(JsScore);
}
root["data"].append(JsStu);
}
Json::StreamWriterBuilder writerBuilder;
std::string json_file = Json::writeString(writerBuilder, root);
ofstream ofs("test.json", std::ios::out | std::ios::trunc);
if (!ofs.is_open())
{
return false;
}
ofs << json_file;
ofs.close();
return true;
}
8.测试看看:
8.1写文件
void Write()
{
vector<Student> arr;
Student aaa;
aaa.name = "aaa";
aaa.arr.push_back(Score("c", 60.5));
aaa.arr.push_back(Score("cpp", 88.5));
aaa.arr.push_back(Score("python", 99.5));
arr.push_back(aaa);
Student bbb;
bbb.name = "bbb";
bbb.arr.push_back(Score("c", 77.5));
bbb.arr.push_back(Score("cpp", 67));
bbb.arr.push_back(Score("python", 95));
arr.push_back(bbb);
Student ccc;
ccc.name = "ccc";
ccc.arr.push_back(Score("c", 85));
ccc.arr.push_back(Score("cpp", 76.5));
ccc.arr.push_back(Score("python", 69.5));
arr.push_back(ccc);
GetJscppMgr()->Write(arr);
}
8.2读文件:
bool JsoncppAssistant::Read(vector<Student>& arr, string path)
{
arr.clear();
Json::Reader reader;
Json::Value root;
ifstream ifs;
ifs.open("test.json");
if (!ifs.is_open())
{
return false;
}
if (!reader.parse(ifs, root))
{
return false;
}
if (root["data"].isNull()) return false;
int nSize = root["data"].size();
for (int i = 0; i < nSize; i++)
{
Student stu;
stu.name = root["data"][i]["name"].asString();
if (!root["data"][i]["Score"].isNull())
{
int nSz2 = root["data"][i]["Score"].size();
stu.arr.reserve(nSz2);
for (int j = 0; j < nSz2; j++)
{
stu.arr.push_back(Score(root["data"][i]["Score"][j]["subject"].asString(),
root["data"][i]["Score"][j]["score"].asFloat(),
root["data"][i]["Score"][j]["bChecked"].asBool()));
}
}
arr.push_back(stu);
}
return true;
}