RapidJSON 是一个 C++ 的 JSON 解析器及生成器,它是腾讯公司开发的一款高效的 C++ JSON 解析/生成器,提供 SAX 及 DOM 风格 API,中文官网地址为:http://rapidjson.org/zh-cn/,从这里可以看到它的详细说明文档;对应的Github地址为:https://github.com/Tencent/rapidjson,从rapidjson-github上面获取它的最新的源代码,然后把include目录下的rapidjson目录放在自己指定项目下或者自己项目工程对应的include等目录下,使用时包含rapid目录下对应的头文件就可以了,无需编译成静态库文件。它的灵感来自 RapidXml。
可以在Linux发行版CentOS7下使用git clone https://github.com/Tencent/rapidjson命令从github上面下载对应的rapidjson的最新代码,以下是我在我的腾讯云主机上下载rapidjson的代码,以及rapidjson项目的目录结构,如下图所示:
从上图rapidjson项目的目录中可以看出,include文件包含的rapidjson文件就是我们使用rapidjson进行json字符串操作时所需要引入的头文件,example是一些代码示例,可供参考。
// simpledom.cpp
// JSON simple example
// This example does not handle errors.
#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"
#include
using namespace rapidjson;
int main() {
// 1. Parse a JSON string into DOM.
const char* json = "{\"project\":\"rapidjson\",\"stars\":10}";
Document d;
d.Parse(json);
// 2. Modify it by DOM.
Value& s = d["stars"];
s.SetInt(s.GetInt() + 1);
// 3. Stringify the DOM
StringBuffer buffer;
Writer<StringBuffer> writer(buffer);
d.Accept(writer);
// Output {"project":"rapidjson","stars":11}
std::cout << buffer.GetString() << std::endl;
return 0;
}
之前使用过rapidjson读取过激光雷达的数据,最近在实际C++项目开发过程中需要将从设备客户端发送的HJ212报警数据解析后生成指定的JSON格式,如下所示:
{
"Stcode": "SS1311054",
"Timestamp": "2020-02-26 13:50:41",
"Alarm": [{
"paramCode": "EP001",
"value": 1.0,
"mark": "N"
}, {
"paramCode": "EP002",
"value": 0.0,
"mark": "N"
}, {
"paramCode": "EP003",
"value": 0.0,
"mark": "N"
}, {
"paramCode": "EP004",
"value": 0.0,
"mark": "N"
}, {
"paramCode": "EP005",
"value": 0.0,
"mark": "N"
}, {
"paramCode": "EP006",
"value": 0.0,
"mark": "N"
}, {
"paramCode": "EP007",
"value": 1.0,
"mark": "N"
}, {
"paramCode": "EP008",
"value": 0.0,
"mark": "N"
}, {
"paramCode": "EP009",
"value": 0.0,
"mark": "N"
}, {
"paramCode": "EP010",
"value": 21.2,
"mark": "N"
}, {
"paramCode": "EP011",
"value": 46.2,
"mark": "N"
}, {
"paramCode": "EP012",
"value": 26.5,
"mark": "N"
}, {
"paramCode": "EP013",
"value": 41.6,
"mark": "N"
}, {
"paramCode": "EP014",
"value": 2.297,
"mark": "N"
}, {
"paramCode": "EP015",
"value": 3.6,
"mark": "N"
}, {
"paramCode": "EP016",
"value": 4.313,
"mark": "N"
}, {
"paramCode": "EP017",
"value": 236.18,
"mark": "N"
}, {
"paramCode": "EP018",
"value": 238.15,
"mark": "N"
}, {
"paramCode": "EP019",
"value": 236.2,
"mark": "N"
}, {
"paramCode": "EP020",
"value": 0.52,
"mark": "N"
}]
}
上面的json字符串格式其实蛮简单的,Stcode表示站点编码,Timestamp表示数据时间,Alarm元素是一个数组,其中paramCode表示报警监测因子编码,value表示报警值,mark表示标记位,N表示正常,B表示异常。
通常rapidjson生成json有两种方式,如下:
使用rapidjson生成上面类似的C++代码如下:
#include
#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"
#include
void test2()
{
rapidjson::Document document;
document.SetObject();
rapidjson::Document::AllocatorType& allocator = document.GetAllocator();
rapidjson::Value object1(rapidjson::kObjectType);
document.AddMember("StCode", "SS1211054", allocator);
document.AddMember("Timestamp", "2020-02-24 13:50:41", allocator);
// 数组
rapidjson::Value alarmArray(rapidjson::kArrayType);
for (int i = 0 ; i < 10; i++)
{
char strParamCode[128] = {
0 };
sprintf(strParamCode, "param%d", i + 1);
rapidjson::Value objectTemp(rapidjson::kObjectType);
rapidjson::Value valueParamCode(strParamCode, document.GetAllocator());
objectTemp.AddMember("paramCode", valueParamCode, allocator);
objectTemp.AddMember("value", i + 10, allocator);
objectTemp.AddMember("mark", "N", allocator);
alarmArray.PushBack(objectTemp, allocator);
}
document.AddMember("Alarm", alarmArray, allocator);
StringBuffer buffer;
rapidjson::Writer<StringBuffer> writer(buffer);
document.Accept(writer);
std::string jsonStr2 = buffer.GetString();
// 打印生成的json字符串
std::cout << "test2(), strJson2为: " << jsonStr2.c_str() << std::endl;
}
int main(int argc, char* argv[])
{
test2();
return 0;
}
使用rapidjson生成上面类似的C++代码如下:
#include
#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"
#include
void test3()
{
rapidjson::StringBuffer s;
rapidjson::Writer<rapidjson::StringBuffer> writer(s);
writer.StartObject();
// Stcode部分
writer.Key("Stcode");
writer.String("SS1211054");
// 构造Timestamp
writer.Key("Timestamp");
writer.String("2020-02-27 23:50:41");
writer.Key("Alarm");
writer.StartArray();
// 报警监测数据数组列表
for (int i = 0; i < 5; i++)
{
char strParamCode[128] = {
0 };
sprintf(strParamCode, "param%d", i);
writer.StartObject();
writer.Key("paramCode");
writer.String(strParamCode);
writer.Key("value");
writer.Double(i*10 + 3.14);
writer.Key("mark");
writer.String("N");
writer.EndObject();
}
writer.EndArray();
writer.EndObject();
std::string strJson3 = s.GetString();
// 打印生成的json字符串
std::cout << "test3(), strJson3为: " << strJson3.c_str() << std::endl;
}
int main(int argc, char* argv[])
{
test2();
return 0;
}
在VS2017中的程序运行截图如下:
从使用上来说,个人觉得第二种方式:使用rapidjson::Writer写入json更加方便。