这里主要记录几个要点,后面来补充吧,很晚了
源码是鹅厂大佬写的,佩服佩服~

一、RapidJSON介绍及资料

RapidJSON是腾讯开源的C++ JSON解析及生成器,只有头文件的C++库,跨平台。
RapidJSON 是一个 C++ 的 JSON 解析器及生成器。它的灵感来自 RapidXml。
特点:

  • 小而全。 同时支持SAX和DOM风格API
  • 快。性能可与 strlen() 相比
  • 独立。不依赖BOOST 等外部库。它甚至不依赖于 STL
  • 对内存友好。在大部分 32/64 位机器上,每个 JSON 值只占 16 字节(除字符串外)。它预设使用一个快速的内存分配器,令分析器可以紧凑地分配内存。【和rapidxml内存池思想类似】
  • 对Unicode友好,支持各种编码。

学习资料:
官方GitHub:https://github.com/Tencent/rapidjson
官方文档:http://rapidjson.org/zh-cn/index.html

二、安装

官方手册:https://legacy.gitbook.com/book/miloyip/rapidjson/details/zh-cn

执行 git submodule update --init 去获取 thirdparty submodules (google test)。
在 rapidjson 目渌下,建立一个 build 目录。
在 build 目录下执行 cmake .. 命令以设置生成。Windows 用户可使用 cmake-gui 应用程序。
在 Windows 下,编译生成在 build 目录中的 solution。在 Linux 下,于 build 目录运行 make。

参考rapidjson使用简介和RapidJSON简介及使用
这个可用学习判断成员是否存在、判断类型、最后取值的思想,避免错误使用,引起内核core dump。

三、实现

demo1:

// rapidjson/example/simpledom/simpledom.cpp`
#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"
#include <iostream>
using namespace rapidjson;
int main() {// 1. 把 JSON 解析至 DOM。
const char* json = "{\"project\":\"rapidjson\",\"stars\":10}";
Document d;
d.Parse(json);
// 2. 利用 DOM 作出修改。
Value& s = d["stars"];
s.SetInt(s.GetInt() + 1);
// 3. 把 DOM 转换(stringify)成 JSON。
StringBuffer buffer;
Writer<StringBuffer> writer(buffer);
d.Accept(writer);
// Output {"project":"rapidjson","stars":11}
std::cout << buffer.GetString() << std::endl;
return 0;
}

demo2:
针对如下的json串:

dom树实现:

代码:

// Hello World example
// This example shows basic usage of DOM-style API.#include "rapidjson/document.h"     // rapidjson's DOM-style API
#include "rapidjson/prettywriter.h" // for stringify JSON
#include <cstdio>using namespace rapidjson;
using namespace std;int main(int, char*[]) {// 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.#if 0// "normal" parsing, decode strings to new buffers. Can use other input stream via ParseStream().if (document.Parse(json).HasParseError())return 1;
#else// 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;
#endifprintf("\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;
}

rapidjson安装学习相关推荐

  1. Win10下安装学习、开发可用的mysql

    Win10下安装学习.开发可用的mysql Win10下安装学习.开发可用的mysql 你好! 这是一篇自我整理的mysql安装以及部分排错 安装准备 一个 硬盘版1 的mysql.(点击蓝色的小1或 ...

  2. PHP - Xdebug安装 - 学习/实践

    1.应用场景 主要用于学习和安装xdebug扩展. 2.学习/操作 1. 文档 Xdebug - Debugger and Profiler Tool for PHP Xdebug: Document ...

  3. puppet最新源码包安装学习笔记

    最近去面试了2家大的电商网站的运维工作,公司对系统的自动化管理,虚拟化,mysql集群,系统性能的优化和shell及python编程的要求都比较高,虽然没有面试成功,但也明白了2013年自己学习的目标 ...

  4. Sql Server 2008开发版(Developer Edition)过期升级企业版(Enterprise Edition)失败后安装学习版...

     最近一个多月,甚是悠哉,无事可做.上线的网站系统也没接到客户的反馈,反而觉得无聊之极了.上周五早上,一上QQ,就收到客户发来消息,管理平台无法登陆了.心里一惊,立马开始查找故障原因.翻看了系统日志, ...

  5. 18、HTTP基本操作及源码编译安装 学习笔记

    HTTPHyper Text Transfer Protocol超文本传输协议 HTMLHyper Text Mark-up Language超文本标记语言 URIUniform Resource I ...

  6. Neo4j Desktop版本的安装学习

    之前学习使用neo4j图数据库用的一直是neo4j 社区版本,在网页上打开.今天发现还有neo4j desktop这个版本.故而学习如下. neo4j官网 发现neo4j community serv ...

  7. R安装学习及包的管理

    本篇博客是我2018年1月所写,中途关进了小黑屋,今日拿出来回首一下!希望不会打扰到各位 初级入门,新手上路 首先来讲,对于如何使用R,怎么安装R呢?其实这也是一个头疼的问题,因为我是一个菜鸟,对电脑 ...

  8. 达梦数据库安装学习总结--DCA上篇

    达梦数据库DCA概述 达梦数据库是一款完全自主原创的国产数据库,国产的操作系统及国产的数据库在国内使用不断扩大,升级及发展也日新月异,其中达梦数据库在政府及事业单位就得到广泛应用. DCA课程学习使用 ...

  9. OpenStack Pike安装学习笔记

    此文原创,绝大部分资料翻译自OpenStack官方安装GUIDE,转载请注明出处. 目录 目录... 1 前言... 4 OpenStack参考架构... 4 硬件架构及配置... 5 控制器节点.. ...

最新文章

  1. 自动驾驶开发云平台业务分析
  2. 那些年让你迷惑的阻塞、非阻塞、异步、同步
  3. C#温故而知新学习系列之.NET框架高级特性—概述.NET框架中的反射(一)
  4. vue的matcher_vue-router工作原理概述和问题分析
  5. ASP.Net Core Web API 如何返回 File。
  6. CodeCraft-20 (Div. 2) D. Nash Matrix 构造 + dfs
  7. Javascript 调试技巧
  8. c/c++整理--c++面向对象(2)
  9. pe估值 python_Python编程学习笔记(7)
  10. SPOJ1716 GSS3(线段树)
  11. FBString分析与使用
  12. [译]git fetch
  13. matlab画全连接网络,无标度网络及MATLAB建模.pdf
  14. 华为手机USB调试与Log
  15. 小程序调用腾讯视频插件
  16. 【Java 代码实例 14】BeanUtils用法详解,附源码分析
  17. 恒讯科技讲解:量子云计算是什么?
  18. [Go语言入门] 02 Go语言程序结构
  19. Android果冻效果(阻尼动画)
  20. 带你了解一下PHP搭建的电商商城系统

热门文章

  1. Spring LTW 切面 Configurable autowire null 自动注入失败的原因及解决方案
  2. 教你怎么区分狗狗的“真假”感冒
  3. Educoder - JSON基础
  4. 港科喜讯|香港科大10个学科位列香港第1世界前50-QS2022学科排名
  5. 分享毕业实习学子“ERP实施工程师”总结,都是肺腑之言——知识是体现人的价值,学校学习的每一个课程学科都是有意义
  6. 2021年金属非金属矿井通风复审考试及金属非金属矿井通风模拟考试
  7. CSS样式——盒子模型
  8. 【大数据存储与处理】
  9. CANoe.DiVa简单配置
  10. http是什么_作用以及报文详情_http的常见状态码