RapidJSON介绍
1.简介
RapidJSON 是一个 C++ 的 JSON 解析库,由腾讯开源。
- 支持 SAX 和 DOM 风格的 API,并且可以解析、生成和查询 JSON 数据。
- RapidJSON 快。它的性能可与strlen() 相比。可支持 SSE2/SSE4.2 加速。
- RapidJSON 独立。它不依赖于 BOOST 等外部库。它甚至不依赖于STL。
- RapidJSON 对内存友好。在大部分 32/64 位机器上,每个 JSON 值只占 16字节(除字符串外)。它预设使用一个快速的内存分配器,令分析器可以紧凑地分配内存。
- RapidJSON 对 Unicode 友好。它支持UTF-8、UTF-16、UTF-32 (大端序/小端序),并内部支持这些编码的检测、校验及转码。例如,RapidJSON 可以在分析一个。
- RapidJSON 是跨平台的。
2.环境搭建
下载地址:https://github.com/Tencent/rapidjson/tree/v1.1.0
这里使用的版本1.1.0

下载完成之后解压目录如下,将整个include目录拷贝到我们的工程目录下。

拷贝完成之后如下图所示:

配置文件路径。C/C++ ->常规 ->附加包含目录

3.代码示例
DOM接口示例
#include "rapidjson/document.h" // rapidjson's DOM-style API
#include "rapidjson/prettywriter.h" // for stringify JSON
#include <iostream>using namespace rapidjson;int main()
{// 1. Parse a JSON text string to a document.const char json[] = " { \"hello\" : \"world\", \"t\" : true , \"f\" : false, \"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3, 4] } ";printf("Original JSON:\n %s\n", json);Document document; // Default template parameter uses UTF8 and MemoryPoolAllocator.// In-situ parsing, decode strings directly in the source string. Source must be string.char buffer[sizeof(json)];memcpy(buffer, json, sizeof(json));if (document.ParseInsitu(buffer).HasParseError())return 1;printf("\nParsing to document succeeded.\n");// 2. Access values in document. printf("\nAccess values in document:\n");assert(document.IsObject()); // Document is a JSON value represents the root of DOM. Root can be either an object or array.assert(document.HasMember("hello"));assert(document["hello"].IsString());printf("hello = %s\n", document["hello"].GetString());// Since version 0.2, you can use single lookup to check the existing of member and its value:Value::MemberIterator hello = document.FindMember("hello");assert(hello != document.MemberEnd());assert(hello->value.IsString());assert(strcmp("world", hello->value.GetString()) == 0);(void)hello;assert(document["t"].IsBool()); // JSON true/false are bool. Can also uses more specific function IsTrue().printf("t = %s\n", document["t"].GetBool() ? "true" : "false");assert(document["f"].IsBool());printf("f = %s\n", document["f"].GetBool() ? "true" : "false");printf("n = %s\n", document["n"].IsNull() ? "null" : "?");assert(document["i"].IsNumber()); // Number is a JSON type, but C++ needs more specific type.assert(document["i"].IsInt()); // In this case, IsUint()/IsInt64()/IsUint64() also return true.printf("i = %d\n", document["i"].GetInt()); // Alternative (int)document["i"]assert(document["pi"].IsNumber());assert(document["pi"].IsDouble());printf("pi = %g\n", document["pi"].GetDouble());{const Value& a = document["a"]; // Using a reference for consecutive access is handy and faster.assert(a.IsArray());for (SizeType i = 0; i < a.Size(); i++) // rapidjson uses SizeType instead of size_t.printf("a[%d] = %d\n", i, a[i].GetInt());int y = a[0].GetInt();(void)y;// Iterating array with iteratorsprintf("a = ");for (Value::ConstValueIterator itr = a.Begin(); itr != a.End(); ++itr)printf("%d ", itr->GetInt());printf("\n");}// Iterating object membersstatic const char* kTypeNames[] = { "Null", "False", "True", "Object", "Array", "String", "Number" };for (Value::ConstMemberIterator itr = document.MemberBegin(); itr != document.MemberEnd(); ++itr)printf("Type of member %s is %s\n", itr->name.GetString(), kTypeNames[itr->value.GetType()]);// 3. Modify values in document.// Change i to a bigger number{uint64_t f20 = 1; // compute factorial of 20for (uint64_t j = 1; j <= 20; j++)f20 *= j;document["i"] = f20; // Alternate form: document["i"].SetUint64(f20)assert(!document["i"].IsInt()); // No longer can be cast as int or uint.}// Adding values to array.{Value& a = document["a"]; // This time we uses non-const reference.Document::AllocatorType& allocator = document.GetAllocator();for (int i = 5; i <= 10; i++)a.PushBack(i, allocator); // May look a bit strange, allocator is needed for potentially realloc. We normally uses the document's.// Fluent APIa.PushBack("Lua", allocator).PushBack("Mio", allocator);}// Making string values.// This version of SetString() just store the pointer to the string.// So it is for literal and string that exists within value's life-cycle.{document["hello"] = "rapidjson"; // This will invoke strlen()// Faster version:// document["hello"].SetString("rapidjson", 9);}// This version of SetString() needs an allocator, which means it will allocate a new buffer and copy the the string into the buffer.Value author;{char buffer2[10];int len = sprintf(buffer2, "%s %s", "Milo", "Yip"); // synthetic example of dynamically created string.author.SetString(buffer2, static_cast<SizeType>(len), document.GetAllocator());// Shorter but slower version:// document["hello"].SetString(buffer, document.GetAllocator());// Constructor version: // Value author(buffer, len, document.GetAllocator());// Value author(buffer, document.GetAllocator());memset(buffer2, 0, sizeof(buffer2)); // For demonstration purpose.}// Variable 'buffer' is unusable now but 'author' has already made a copy.document.AddMember("author", author, document.GetAllocator());assert(author.IsNull()); // Move semantic for assignment. After this variable is assigned as a member, the variable becomes null.// 4. Stringify JSONprintf("\nModified JSON with reformatting:\n");StringBuffer sb;PrettyWriter<StringBuffer> writer(sb);document.Accept(writer); // Accept() traverses the DOM and generates Handler events.puts(sb.GetString());return 0;
}
运行结果:

SAX接口示例:
#include "rapidjson/document.h" // rapidjson's DOM-style API
#include "rapidjson/prettywriter.h" // for stringify JSON
#include "rapidjson/reader.h"
#include "rapidjson/error/en.h"
#include <iostream>
#include <string>
#include <map>using namespace std;
using namespace rapidjson;typedef map<string, string> MessageMap;#if defined(__GNUC__)
RAPIDJSON_DIAG_PUSH
RAPIDJSON_DIAG_OFF(effc++)
#endif#ifdef __clang__
RAPIDJSON_DIAG_PUSH
RAPIDJSON_DIAG_OFF(switch - enum)
#endifstruct MessageHandler : public BaseReaderHandler<UTF8<>, MessageHandler>
{MessageHandler() : messages_(), state_(kExpectObjectStart), name_() {}bool StartObject() {switch (state_) {case kExpectObjectStart:state_ = kExpectNameOrObjectEnd;return true;default:return false;}}bool String(const char* str, SizeType length, bool){switch (state_) {case kExpectNameOrObjectEnd:name_ = string(str, length);state_ = kExpectValue;return true;case kExpectValue:messages_.insert(MessageMap::value_type(name_, string(str, length)));state_ = kExpectNameOrObjectEnd;return true;default:return false;}}bool EndObject(SizeType) { return state_ == kExpectNameOrObjectEnd; }bool Default() { return false; } // All other events are invalid.MessageMap messages_;enum State{kExpectObjectStart,kExpectNameOrObjectEnd,kExpectValue}state_;std::string name_;
};#if defined(__GNUC__)
RAPIDJSON_DIAG_POP
#endif#ifdef __clang__
RAPIDJSON_DIAG_POP
#endifstatic void ParseMessages(const char* json, MessageMap& messages)
{Reader reader;MessageHandler handler;StringStream ss(json);if (reader.Parse(ss, handler))messages.swap(handler.messages_); // Only change it if success.else {ParseErrorCode e = reader.GetParseErrorCode();size_t o = reader.GetErrorOffset();cout << "Error: " << GetParseError_En(e) << endl;;cout << " at offset " << o << " near '" << string(json).substr(o, 10) << "...'" << endl;}
}int main()
{MessageMap messages;const char* json1 = "{ \"greeting\" : \"Hello!\", \"farewell\" : \"bye-bye!\" }";cout << json1 << endl;ParseMessages(json1, messages);for (MessageMap::const_iterator itr = messages.begin(); itr != messages.end(); ++itr)cout << itr->first << ": " << itr->second << endl;cout << endl << "Parse a JSON with invalid schema." << endl;const char* json2 = "{ \"greeting\" : \"Hello!\", \"farewell\" : \"bye-bye!\", \"foo\" : {} }";cout << json2 << endl;ParseMessages(json2, messages);return 0;
}

4.更多参考
libVLC 专栏介绍-CSDN博客
Qt+FFmpeg+opengl从零制作视频播放器-1.项目介绍_qt opengl视频播放器-CSDN博客
QCharts -1.概述-CSDN博客
相关文章:
RapidJSON介绍
1.简介 RapidJSON 是一个 C 的 JSON 解析库,由腾讯开源。 支持 SAX 和 DOM 风格的 API,并且可以解析、生成和查询 JSON 数据。RapidJSON 快。它的性能可与strlen() 相比。可支持 SSE2/SSE4.2 加速。RapidJSON 独立。它不依赖于 BOOST 等外部库。它甚至…...
大型企业总分支多区域数据传输,效率为先还是安全为先?
大型企业为了业务拓展需要,会在全国乃至全球各地设立分公司和办事机构,以便更好地处理当地事务,并进行市场的开拓和客户维护,此时,企业内部就衍生出了新的业务需求,即多区域数据传输。 多区域很难准确定义&…...
C语言例题35、反向输出字符串(指针方式),例如:输入abcde,输出edcba
#include <stdio.h>void reverse(char *p) {int len 0;while (*p ! \0) { //取得字符串长度p;len;}while (len > 0) { //反向打印到终端printf("%c", *--p);len--;} }int main() {char s[255];printf("请输入一个字符串:");gets(s)…...
场景文本检测识别学习 day09(Swin Transformer论文精读)
Patch & Window 在Swin Transformer中,不同层级的窗口内部的补丁数量是固定的,补丁内部的像素数量也是固定的,如上图的红色框就是不同的窗口(Window),窗口内部的灰色框就是补丁(Patch&#…...
抖音小店个人店和个体店有什么不同?区别问题,新手必须了解!
哈喽~我是电商月月 新手开抖音小店入驻时会发现,选择入驻形式时有三个选择,个人店,个体店和企业店 其中,个人店和个体店只差了一个字,但个人店不需要营业执照,是不是入驻时选择个人店会更好一点呢&#x…...
动态规划入门和应用示例
文章目录 前言斐波那契数列爬楼梯总结优点:缺点: 前言 动态规划(Dynamic Programming,DP)是运筹学的一个分支,是求解决策过程最优化的数学方法。它主要用于解决一类具有重叠子问题和最优子结构性质的问题。…...
【C语言】精品练习题
目录 题目一: 题目二: 题目三: 题目四: 题目五: 题目六: 题目七: 题目八: 题目九: 题目十: 题目十一: 题目十二: 题目十…...
数据库(MySQL)—— DML语句
数据库(MySQL)—— DML语句 什么是DML语句添加数据给全部字段添加数据批量添加数据 修改数据删除数据 什么是DML语句 在MySQL中,DML(Data Manipulation Language,数据操纵语言)语句主要用于对数据库中的数…...
【最大公约数 并集查找 调和级数】1998. 数组的最大公因数排序
本文涉及知识点 最大公约数 并集查找 调和级数 LeetCode1998. 数组的最大公因数排序 给你一个整数数组 nums ,你可以在 nums 上执行下述操作 任意次 : 如果 gcd(nums[i], nums[j]) > 1 ,交换 nums[i] 和 nums[j] 的位置。其中 gcd(nums…...
iOS实现一个高性能的跑马灯
效果图 该跑马灯完全通过CATextLayer 实现,轻量级,并且通过 系统的位移动画实现滚动效果,避免了使用displaylink造成的性能瓶颈,使用系统动画,系统自动做了很多性能优化,实现更好的性能,并使用…...
MySQL的视图、存储过程、触发器
视图 介绍 视图是一种虚拟存在的表。视图中的数据并不在数据库中实际存在,行和列数据来自定义视图的查询中使用的表,并且是在使用视图时动态生成的。通俗的讲,视图只保存了查询的SQL逻辑,不保存查询结果。所以我们在创建视图的时…...
【图像特征点匹配】
图像特征点匹配 图像特征点匹配是计算机视觉中的一项关键技术,它涉及在两个或多个图像之间寻找并匹配具有独特属性的点,这些点被称为特征点。 立体视觉:通过匹配同一场景的不同视角图像中的特征点,可以重建场景的三维结构。物体识别:通过匹配物体表面的特征点,可以识别和…...
GZIPOutputStream JSON压缩
一、背景 小王瞥了一眼历史记录表,不禁惊呼:“这表怎么这么大?”同事们闻声纷纷围拢过来查看。仔细一瞧,发现这个表的大小竟然超过了3G。主管随即指示小王打开相应的表数据检查,发现其中存储了用户的权限信息…...
毫米波雷达原理(含代码)(含ARS548 4D毫米波雷达数据demo和可视化视频)
毫米波雷达原理 1. 传统毫米波雷达1.1 雷达工作原理1.2 单目标距离估计1.3 单目标速度估计1.4 单目标角度估计1.5 多目标距离估计1.6 多目标速度估计1.7多目标角度估计1.7 总结 3. FMCW雷达数据处理算法4. 毫米波雷达的目标解析(含python代码)5. ARS548 4D毫米波雷达数据demo(含…...
3.1 Gateway之路由请求和转发
1.依赖坐标 <!--网关--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-gateway</artifactId></dependency><!--服务注册和发现--><dependency><groupId>com.alibab…...
人脸识别开源算法库和开源数据库
目录 1. 人脸识别开源算法库 1.1 OpenCV人脸识别模块 1.2 Dlib人脸识别模块 1.3 SeetaFace6 1.4 DeepFace 1.5 InsightFace 2. 人脸识别开源数据库 2.1 CelebA 2.2 LFW 2.3 MegaFace 2.4 Glint360K 2.5 WebFace260M 人脸识别 (Face Recognition) 是一种基于人的面部…...
Excel 中用于在一个范围中查找特定的值,并返回同一行中指定列的值 顺序不一样 可以处理吗
一、需求 Excel 中,在一列(某范围内)查找另一列特定的值,并返回同一行中另一指定列的值, 查找列和返回列的顺序不一样 二、 实现 1、下面是一个使用 INDEX 和 MATCH 函数的例子: 假设你有以下数据&…...
MySql-日期分组
一、分别统计各时间各类型数据条数 数据库的 request_time字段 数据类型:timestamp 默认值:CURRENT_TIMESTAMP 例子: 2024-01-26 08:25:48 原数据: 1、将数据按照日期(年月日)形式输出 按照request_…...
有哪些方法可以在运行时动态生成一个Java类?
使用 Java 反射 API🚩: Java 的反射 API 允许在运行时查询和操作类和对象。虽然反射 API 本身不直接提供生成新类的功能,但可以用于动态调用构造函数、方法和访问字段,这在某些情况下可以作为动态生成类的一部分。 字节码操作库&…...
JAVA两个线程交替打印实现
方案1 Semaphore 机制 通过信息号机制来 协调两个线程,一个线程打印后,给另一个线程释放一个信号量 Semaphore semaphorea new Semaphore(1);Semaphore semaphoreb new Semaphore(0);Thread threada new Thread(new Runnable() {Overridepublic void…...
第19节 Node.js Express 框架
Express 是一个为Node.js设计的web开发框架,它基于nodejs平台。 Express 简介 Express是一个简洁而灵活的node.js Web应用框架, 提供了一系列强大特性帮助你创建各种Web应用,和丰富的HTTP工具。 使用Express可以快速地搭建一个完整功能的网站。 Expre…...
VB.net复制Ntag213卡写入UID
本示例使用的发卡器:https://item.taobao.com/item.htm?ftt&id615391857885 一、读取旧Ntag卡的UID和数据 Private Sub Button15_Click(sender As Object, e As EventArgs) Handles Button15.Click轻松读卡技术支持:网站:Dim i, j As IntegerDim cardidhex, …...
通过Wrangler CLI在worker中创建数据库和表
官方使用文档:Getting started Cloudflare D1 docs 创建数据库 在命令行中执行完成之后,会在本地和远程创建数据库: npx wranglerlatest d1 create prod-d1-tutorial 在cf中就可以看到数据库: 现在,您的Cloudfla…...
基于服务器使用 apt 安装、配置 Nginx
🧾 一、查看可安装的 Nginx 版本 首先,你可以运行以下命令查看可用版本: apt-cache madison nginx-core输出示例: nginx-core | 1.18.0-6ubuntu14.6 | http://archive.ubuntu.com/ubuntu focal-updates/main amd64 Packages ng…...
学校招生小程序源码介绍
基于ThinkPHPFastAdminUniApp开发的学校招生小程序源码,专为学校招生场景量身打造,功能实用且操作便捷。 从技术架构来看,ThinkPHP提供稳定可靠的后台服务,FastAdmin加速开发流程,UniApp则保障小程序在多端有良好的兼…...
Java多线程实现之Callable接口深度解析
Java多线程实现之Callable接口深度解析 一、Callable接口概述1.1 接口定义1.2 与Runnable接口的对比1.3 Future接口与FutureTask类 二、Callable接口的基本使用方法2.1 传统方式实现Callable接口2.2 使用Lambda表达式简化Callable实现2.3 使用FutureTask类执行Callable任务 三、…...
智能分布式爬虫的数据处理流水线优化:基于深度强化学习的数据质量控制
在数字化浪潮席卷全球的今天,数据已成为企业和研究机构的核心资产。智能分布式爬虫作为高效的数据采集工具,在大规模数据获取中发挥着关键作用。然而,传统的数据处理流水线在面对复杂多变的网络环境和海量异构数据时,常出现数据质…...
A2A JS SDK 完整教程:快速入门指南
目录 什么是 A2A JS SDK?A2A JS 安装与设置A2A JS 核心概念创建你的第一个 A2A JS 代理A2A JS 服务端开发A2A JS 客户端使用A2A JS 高级特性A2A JS 最佳实践A2A JS 故障排除 什么是 A2A JS SDK? A2A JS SDK 是一个专为 JavaScript/TypeScript 开发者设计的强大库ÿ…...
C#中的CLR属性、依赖属性与附加属性
CLR属性的主要特征 封装性: 隐藏字段的实现细节 提供对字段的受控访问 访问控制: 可单独设置get/set访问器的可见性 可创建只读或只写属性 计算属性: 可以在getter中执行计算逻辑 不需要直接对应一个字段 验证逻辑: 可以…...
Redis:现代应用开发的高效内存数据存储利器
一、Redis的起源与发展 Redis最初由意大利程序员Salvatore Sanfilippo在2009年开发,其初衷是为了满足他自己的一个项目需求,即需要一个高性能的键值存储系统来解决传统数据库在高并发场景下的性能瓶颈。随着项目的开源,Redis凭借其简单易用、…...
