rapidjson读取类

//
/// 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__```

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值