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…...
k8s从入门到放弃之Ingress七层负载
k8s从入门到放弃之Ingress七层负载 在Kubernetes(简称K8s)中,Ingress是一个API对象,它允许你定义如何从集群外部访问集群内部的服务。Ingress可以提供负载均衡、SSL终结和基于名称的虚拟主机等功能。通过Ingress,你可…...
3.3.1_1 检错编码(奇偶校验码)
从这节课开始,我们会探讨数据链路层的差错控制功能,差错控制功能的主要目标是要发现并且解决一个帧内部的位错误,我们需要使用特殊的编码技术去发现帧内部的位错误,当我们发现位错误之后,通常来说有两种解决方案。第一…...
大语言模型如何处理长文本?常用文本分割技术详解
为什么需要文本分割? 引言:为什么需要文本分割?一、基础文本分割方法1. 按段落分割(Paragraph Splitting)2. 按句子分割(Sentence Splitting)二、高级文本分割策略3. 重叠分割(Sliding Window)4. 递归分割(Recursive Splitting)三、生产级工具推荐5. 使用LangChain的…...
css的定位(position)详解:相对定位 绝对定位 固定定位
在 CSS 中,元素的定位通过 position 属性控制,共有 5 种定位模式:static(静态定位)、relative(相对定位)、absolute(绝对定位)、fixed(固定定位)和…...
【RockeMQ】第2节|RocketMQ快速实战以及核⼼概念详解(二)
升级Dledger高可用集群 一、主从架构的不足与Dledger的定位 主从架构缺陷 数据备份依赖Slave节点,但无自动故障转移能力,Master宕机后需人工切换,期间消息可能无法读取。Slave仅存储数据,无法主动升级为Master响应请求ÿ…...
JVM暂停(Stop-The-World,STW)的原因分类及对应排查方案
JVM暂停(Stop-The-World,STW)的完整原因分类及对应排查方案,结合JVM运行机制和常见故障场景整理而成: 一、GC相关暂停 1. 安全点(Safepoint)阻塞 现象:JVM暂停但无GC日志,日志显示No GCs detected。原因:JVM等待所有线程进入安全点(如…...
Spring AI与Spring Modulith核心技术解析
Spring AI核心架构解析 Spring AI(https://spring.io/projects/spring-ai)作为Spring生态中的AI集成框架,其核心设计理念是通过模块化架构降低AI应用的开发复杂度。与Python生态中的LangChain/LlamaIndex等工具类似,但特别为多语…...
MySQL用户和授权
开放MySQL白名单 可以通过iptables-save命令确认对应客户端ip是否可以访问MySQL服务: test: # iptables-save | grep 3306 -A mp_srv_whitelist -s 172.16.14.102/32 -p tcp -m tcp --dport 3306 -j ACCEPT -A mp_srv_whitelist -s 172.16.4.16/32 -p tcp -m tcp -…...
dify打造数据可视化图表
一、概述 在日常工作和学习中,我们经常需要和数据打交道。无论是分析报告、项目展示,还是简单的数据洞察,一个清晰直观的图表,往往能胜过千言万语。 一款能让数据可视化变得超级简单的 MCP Server,由蚂蚁集团 AntV 团队…...
Spring Cloud Gateway 中自定义验证码接口返回 404 的排查与解决
Spring Cloud Gateway 中自定义验证码接口返回 404 的排查与解决 问题背景 在一个基于 Spring Cloud Gateway WebFlux 构建的微服务项目中,新增了一个本地验证码接口 /code,使用函数式路由(RouterFunction)和 Hutool 的 Circle…...
