当前位置: 首页 > news >正文

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 解析库&#xff0c;由腾讯开源。 支持 SAX 和 DOM 风格的 API&#xff0c;并且可以解析、生成和查询 JSON 数据。RapidJSON 快。它的性能可与strlen() 相比。可支持 SSE2/SSE4.2 加速。RapidJSON 独立。它不依赖于 BOOST 等外部库。它甚至…...

大型企业总分支多区域数据传输,效率为先还是安全为先?

大型企业为了业务拓展需要&#xff0c;会在全国乃至全球各地设立分公司和办事机构&#xff0c;以便更好地处理当地事务&#xff0c;并进行市场的开拓和客户维护&#xff0c;此时&#xff0c;企业内部就衍生出了新的业务需求&#xff0c;即多区域数据传输。 多区域很难准确定义&…...

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("请输入一个字符串&#xff1a;");gets(s)…...

场景文本检测识别学习 day09(Swin Transformer论文精读)

Patch & Window 在Swin Transformer中&#xff0c;不同层级的窗口内部的补丁数量是固定的&#xff0c;补丁内部的像素数量也是固定的&#xff0c;如上图的红色框就是不同的窗口&#xff08;Window&#xff09;&#xff0c;窗口内部的灰色框就是补丁&#xff08;Patch&#…...

抖音小店个人店和个体店有什么不同?区别问题,新手必须了解!

哈喽~我是电商月月 新手开抖音小店入驻时会发现&#xff0c;选择入驻形式时有三个选择&#xff0c;个人店&#xff0c;个体店和企业店 其中&#xff0c;个人店和个体店只差了一个字&#xff0c;但个人店不需要营业执照&#xff0c;是不是入驻时选择个人店会更好一点呢&#x…...

动态规划入门和应用示例

文章目录 前言斐波那契数列爬楼梯总结优点&#xff1a;缺点&#xff1a; 前言 动态规划&#xff08;Dynamic Programming&#xff0c;DP&#xff09;是运筹学的一个分支&#xff0c;是求解决策过程最优化的数学方法。它主要用于解决一类具有重叠子问题和最优子结构性质的问题。…...

【C语言】精品练习题

目录 题目一&#xff1a; 题目二&#xff1a; 题目三&#xff1a; 题目四&#xff1a; 题目五&#xff1a; 题目六&#xff1a; 题目七&#xff1a; 题目八&#xff1a; 题目九&#xff1a; 题目十&#xff1a; 题目十一&#xff1a; 题目十二&#xff1a; 题目十…...

数据库(MySQL)—— DML语句

数据库&#xff08;MySQL&#xff09;—— DML语句 什么是DML语句添加数据给全部字段添加数据批量添加数据 修改数据删除数据 什么是DML语句 在MySQL中&#xff0c;DML&#xff08;Data Manipulation Language&#xff0c;数据操纵语言&#xff09;语句主要用于对数据库中的数…...

【最大公约数 并集查找 调和级数】1998. 数组的最大公因数排序

本文涉及知识点 最大公约数 并集查找 调和级数 LeetCode1998. 数组的最大公因数排序 给你一个整数数组 nums &#xff0c;你可以在 nums 上执行下述操作 任意次 &#xff1a; 如果 gcd(nums[i], nums[j]) > 1 &#xff0c;交换 nums[i] 和 nums[j] 的位置。其中 gcd(nums…...

iOS实现一个高性能的跑马灯

效果图 该跑马灯完全通过CATextLayer 实现&#xff0c;轻量级&#xff0c;并且通过 系统的位移动画实现滚动效果&#xff0c;避免了使用displaylink造成的性能瓶颈&#xff0c;使用系统动画&#xff0c;系统自动做了很多性能优化&#xff0c;实现更好的性能&#xff0c;并使用…...

MySQL的视图、存储过程、触发器

视图 介绍 视图是一种虚拟存在的表。视图中的数据并不在数据库中实际存在&#xff0c;行和列数据来自定义视图的查询中使用的表&#xff0c;并且是在使用视图时动态生成的。通俗的讲&#xff0c;视图只保存了查询的SQL逻辑&#xff0c;不保存查询结果。所以我们在创建视图的时…...

【图像特征点匹配】

图像特征点匹配 图像特征点匹配是计算机视觉中的一项关键技术,它涉及在两个或多个图像之间寻找并匹配具有独特属性的点,这些点被称为特征点。 立体视觉:通过匹配同一场景的不同视角图像中的特征点,可以重建场景的三维结构。物体识别:通过匹配物体表面的特征点,可以识别和…...

GZIPOutputStream JSON压缩

一、背景 小王瞥了一眼历史记录表&#xff0c;不禁惊呼&#xff1a;“这表怎么这么大&#xff1f;”同事们闻声纷纷围拢过来查看。仔细一瞧&#xff0c;发现这个表的大小竟然超过了3G。主管随即指示小王打开相应的表数据检查&#xff0c;发现其中存储了用户的权限信息&#xf…...

毫米波雷达原理(含代码)(含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 中&#xff0c;在一列&#xff08;某范围内&#xff09;查找另一列特定的值&#xff0c;并返回同一行中另一指定列的值&#xff0c; 查找列和返回列的顺序不一样 二、 实现 1、下面是一个使用 INDEX 和 MATCH 函数的例子&#xff1a; 假设你有以下数据&…...

MySql-日期分组

一、分别统计各时间各类型数据条数 数据库的 request_time字段 数据类型&#xff1a;timestamp 默认值&#xff1a;CURRENT_TIMESTAMP 例子&#xff1a; 2024-01-26 08:25:48 原数据&#xff1a; 1、将数据按照日期&#xff08;年月日&#xff09;形式输出 按照request_…...

有哪些方法可以在运行时动态生成一个Java类?

使用 Java 反射 API&#x1f6a9;&#xff1a; Java 的反射 API 允许在运行时查询和操作类和对象。虽然反射 API 本身不直接提供生成新类的功能&#xff0c;但可以用于动态调用构造函数、方法和访问字段&#xff0c;这在某些情况下可以作为动态生成类的一部分。 字节码操作库&…...

JAVA两个线程交替打印实现

方案1 Semaphore 机制 通过信息号机制来 协调两个线程&#xff0c;一个线程打印后&#xff0c;给另一个线程释放一个信号量 Semaphore semaphorea new Semaphore(1);Semaphore semaphoreb new Semaphore(0);Thread threada new Thread(new Runnable() {Overridepublic void…...

云计算——弹性云计算器(ECS)

弹性云服务器&#xff1a;ECS 概述 云计算重构了ICT系统&#xff0c;云计算平台厂商推出使得厂家能够主要关注应用管理而非平台管理的云平台&#xff0c;包含如下主要概念。 ECS&#xff08;Elastic Cloud Server&#xff09;&#xff1a;即弹性云服务器&#xff0c;是云计算…...

css实现圆环展示百分比,根据值动态展示所占比例

代码如下 <view class""><view class"circle-chart"><view v-if"!!num" class"pie-item" :style"{background: conic-gradient(var(--one-color) 0%,#E9E6F1 ${num}%),}"></view><view v-else …...

K8S认证|CKS题库+答案| 11. AppArmor

目录 11. AppArmor 免费获取并激活 CKA_v1.31_模拟系统 题目 开始操作&#xff1a; 1&#xff09;、切换集群 2&#xff09;、切换节点 3&#xff09;、切换到 apparmor 的目录 4&#xff09;、执行 apparmor 策略模块 5&#xff09;、修改 pod 文件 6&#xff09;、…...

逻辑回归:给不确定性划界的分类大师

想象你是一名医生。面对患者的检查报告&#xff08;肿瘤大小、血液指标&#xff09;&#xff0c;你需要做出一个**决定性判断**&#xff1a;恶性还是良性&#xff1f;这种“非黑即白”的抉择&#xff0c;正是**逻辑回归&#xff08;Logistic Regression&#xff09;** 的战场&a…...

1688商品列表API与其他数据源的对接思路

将1688商品列表API与其他数据源对接时&#xff0c;需结合业务场景设计数据流转链路&#xff0c;重点关注数据格式兼容性、接口调用频率控制及数据一致性维护。以下是具体对接思路及关键技术点&#xff1a; 一、核心对接场景与目标 商品数据同步 场景&#xff1a;将1688商品信息…...

深入理解JavaScript设计模式之单例模式

目录 什么是单例模式为什么需要单例模式常见应用场景包括 单例模式实现透明单例模式实现不透明单例模式用代理实现单例模式javaScript中的单例模式使用命名空间使用闭包封装私有变量 惰性单例通用的惰性单例 结语 什么是单例模式 单例模式&#xff08;Singleton Pattern&#…...

Caliper 配置文件解析:config.yaml

Caliper 是一个区块链性能基准测试工具,用于评估不同区块链平台的性能。下面我将详细解释你提供的 fisco-bcos.json 文件结构,并说明它与 config.yaml 文件的关系。 fisco-bcos.json 文件解析 这个文件是针对 FISCO-BCOS 区块链网络的 Caliper 配置文件,主要包含以下几个部…...

根据万维钢·精英日课6的内容,使用AI(2025)可以参考以下方法:

根据万维钢精英日课6的内容&#xff0c;使用AI&#xff08;2025&#xff09;可以参考以下方法&#xff1a; 四个洞见 模型已经比人聪明&#xff1a;以ChatGPT o3为代表的AI非常强大&#xff0c;能运用高级理论解释道理、引用最新学术论文&#xff0c;生成对顶尖科学家都有用的…...

【JavaWeb】Docker项目部署

引言 之前学习了Linux操作系统的常见命令&#xff0c;在Linux上安装软件&#xff0c;以及如何在Linux上部署一个单体项目&#xff0c;大多数同学都会有相同的感受&#xff0c;那就是麻烦。 核心体现在三点&#xff1a; 命令太多了&#xff0c;记不住 软件安装包名字复杂&…...

Element Plus 表单(el-form)中关于正整数输入的校验规则

目录 1 单个正整数输入1.1 模板1.2 校验规则 2 两个正整数输入&#xff08;联动&#xff09;2.1 模板2.2 校验规则2.3 CSS 1 单个正整数输入 1.1 模板 <el-formref"formRef":model"formData":rules"formRules"label-width"150px"…...