//
/// These functions are C++ ports.
/// At 2020年12月30日09:39:24
/// Version 1.0.0.1
/// Author __闹闹
/// Brief 读取json配置文件的类
Revision history
/// modify_author: 闹闹
/// modify_time: 2020年12月30日09:40:10
/// modify_content 将函数整合为类,放在一个hpp文件
/// modify_version: 1.0.0.2
#pragma once
#ifndef __PARSEJSON_H__
#define __PARSEJSON_H__
/*
注意json文件的编码格式,采用ANSI编码,
格式:每个json对象({}括起来的是一个json对象)的 每个变量后面加逗号结尾,最后一个后面不加逗号
*/
/*
参考博客:
https://blog.csdn.net/qq_27385759/article/details/79277434
https://blog.csdn.net/fengbingchun/article/details/91139889
https://www.cnblogs.com/sea-stream/p/11105387.html
https://blog.csdn.net/diaokua8472/article/details/101640329
https://blog.csdn.net/elloop/article/details/49908689
*/
#include "rapidjson/document.h"
#include "rapidjson/prettywriter.h"
#include "rapidjson/stringbuffer.h"
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
class ParseJson
{
public:
static std::string ReadFile(const char* filename);
static std::string ParseCharacter(const char *jsonstr, const char* character);
static std::vector<std::string> ParseArray(const char *jsonstr, const char* arrayName, const char* keyName);
static bool ParseBool(const char *jsonstr, const char* boolName);
private:
//私有构造函数,拷贝构造函数和拷贝赋值函数,禁止在类外声明实例
ParseJson();
~ParseJson();
ParseJson(const ParseJson &other) = delete;
ParseJson& operator=(const ParseJson &other) = delete;
};
//= = = = = = = = = = = = = = = = = = = =
// @FuncName:
// @Brief: 读取json文件,返回整个文件的string字符串
// @InputParameter:
// @OutputParameter:
// @Returns:
// @Others:
//= = = = = = = = = = = = = = = = = = = =
std::string ParseJson::ReadFile(const char* filename)
{
std::ifstream in(filename);
if (!in.is_open()) {
fprintf(stderr, "fail to read json file: %s\n", filename);
return "";
}
std::string json_content((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());
in.close();
//std::cout << json_content << std::endl;
return json_content;
}
//= = = = = = = = = = = = = = = = = = = =
// @FuncName:
// @Brief: 解析字符串
// @InputParameter: read文件之后的字符串,需要解析的字符串key键
// @OutputParameter:
// @Returns:
// @Others:
//= = = = = = = = = = = = = = = = = = = =
std::string ParseJson::ParseCharacter(const char *jsonstr, const char* character)
{
rapidjson::Document d;
if (d.Parse(jsonstr).HasParseError()) {
rapidjson::ParseErrorCode code = d.GetParseError();
printf("jsonstr parse error! when it is character\n");
return "";
}
if (!d.IsObject()) {
printf("jsonstr should be an object! when it is character\n");
return "";
}
if (d.HasMember(character)) {
rapidjson::Value &m = d[character];
std::string v = m.GetString();
return v;
}
else{
printf("character parse error! when it is character\n");
return "";
}
return "";
}
//= = = = = = = = = = = = = = = = = = = =
// @FuncName:
// @Brief: 解析字符串数组
// @InputParameter: json在read之后的返回值,数组名,键值。
// @OutputParameter:
// @Returns:
// @Others:
//= = = = = = = = = = = = = = = = = = = =
std::vector<std::string> ParseJson::ParseArray(const char *jsonstr, const char* arrayName, const char* keyName)
{
rapidjson::Document d;
std::vector<std::string> result;
if (d.Parse(jsonstr).HasParseError()) {
rapidjson::ParseErrorCode code = d.GetParseError();
printf("jsonstr parse error! when it is Array\n");
return result;
}
if (!d.IsObject()) {
printf("jsonstr should be an object! when it is Array\n");
return result;
}
rapidjson::Value& m = d[arrayName];
if (m.IsArray()){
for (rapidjson::SizeType i = 0; i < m.Size(); ++i){
rapidjson::Value& v = m[i];
if (v.HasMember(keyName) && v[keyName].IsString()){
std::string dst = v[keyName].GetString();
result.push_back(dst);
}
}
return result;
}
return result;
}
//= = = = = = = = = = = = = = = = = = = =
// @FuncName:
// @Brief: 解析bool值
// @InputParameter: json在read之后的返回值,键值。
// @OutputParameter:
// @Returns:
// @Others:
//= = = = = = = = = = = = = = = = = = = =
bool ParseJson::ParseBool(const char *jsonstr, const char* boolName)
{
rapidjson::Document d;
if (d.Parse(jsonstr).HasParseError()) {
rapidjson::ParseErrorCode code = d.GetParseError();
printf("jsonstr parse error! when it is boolValue\n");
return false;
}
if (!d.IsObject()) {
printf("jsonstr should be an object! when it is boolValue\n");
return "";
}
if (d.HasMember(boolName)) {
rapidjson::Value &m = d[boolName];
bool v = m.GetBool();
return v;
}
else{
printf("character parse error! when it is boolValue\n");
return false;
}
return false;
}
#endif //__PARSEJSON_H__```
rapidjson读取类
最新推荐文章于 2021-12-21 16:15:05 发布