博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
rapidjson使用总结
阅读量:7088 次
发布时间:2019-06-28

本文共 2528 字,大约阅读时间需要 8 分钟。

Reference:  https://blog.csdn.net/elloop/article/details/49908689

rapidjson简介

rapidjson是腾讯的开源json解析框架,用c++实现。由于全部代码仅用header file实现,所以很容易集成到项目中。

rapidjson的性能是很出色的,其作者做了28个C/C++ JSON库的评测,里有测试的结果截图。

rapidjson的另一个特点是对json的标准符合程度是100%的(在开启了full precision选项的情况下)。

这里是官方教程:

这里是原作者对rapidjson代码的剖析:

我之前的项目使用的是jsoncpp,最近在把解析json的代码交叉编译到iOS设备的时候,偶尔会出现crash的情况。虽然经过检查是代码写的有问题,不是jsoncpp的问题,在解决问题过程中尝试了rapidjson这个库,并顺便对比了一下jsoncpp和rapidjson对我项目中json文件的解析速度。

Dom解析示例

下面是我写的一个小例子,从test.json文件中读取内容并解析。其他代码示例也可以查看我的github仓库中关于rapidjson的测试代码:.

// test.json{    "dictVersion": 1,      "content":      [           {"key": "word1", "value": "单词1"} , {"key": "word2", "value": "单词2"} , {"key": "word3", "value": "单词3"} , {"key": "word4", "value": "单词4"} , {"key": "word5", "value": "单词5"} ] }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
// test.cpp#include "rapidjson/document.h"#include "rapidjson/stringbuffer.h"#include "rapidjson/writer.h"#include 
#include
#include
#include
#define psln(x) std::cout << #x " = " << (x) << std::endl void testSimpleDoc() { using std::string; using std::ifstream; // read json content into string. string stringFromStream; ifstream in; in.open("test.json", ifstream::in); if (!in.is_open()) return; string line; while (getline(in, line)) { stringFromStream.append(line + "\n"); } in.close(); // ---------------------------- read json -------------------- // parse json from string. using rapidjson::Document; Document doc; doc.Parse<0>(stringFromStream.c_str()); if (doc.HasParseError()) { rapidjson::ParseErrorCode code = doc.GetParseError(); psln(code); return; } // use values in parse result. using rapidjson::Value; Value & v = doc["dictVersion"]; if (v.IsInt()) { psln(v.GetInt()); } Value & contents = doc["content"]; if (contents.IsArray()) { for (size_t i = 0; i < contents.Size(); ++i) { Value & v = contents[i]; assert(v.IsObject()); if (v.HasMember("key") && v["key"].IsString()) { psln(v["key"].GetString()); } if (v.HasMember("value") && v["value"].IsString()) { psln(v["value"].GetString()); } } } // ---------------------------- write json -------------------- pcln("add a value into array"); Value item(Type::kObjectType); item.AddMember("key", "word5", doc.GetAllocator()); item.AddMember("value", "单词5", doc.GetAllocator()); contents.PushBack(item, doc.GetAllocator()); // convert dom to string. StringBuffer buffer; // in rapidjson/stringbuffer.h Writer
writer(buffer); // in rapidjson/writer.h doc.Accept(writer); psln(buffer.GetString()); }
你可能感兴趣的文章
DevOps 发展 10 年:成熟度和应用趋势调查报告
查看>>
JEPF 软件快速开发平台今日入住 oschina
查看>>
开发者调查: HTML5 增长 Windows 下降
查看>>
《数据库技术原理与应用教程》一第1章 数据、数据管理与数据处理
查看>>
String被设计成不可变和不能被继承的原因
查看>>
《数据科学:R语言实现》——1.7 执行延迟计算
查看>>
《Adobe Illustrator大师班:经典作品与完美技巧赏析》—Helen Huang
查看>>
微软正式开源 DirectX 着色器
查看>>
阿里感悟(一)如何有效解决问题?
查看>>
《精通Spring MVC 4》——第1章 快速搭建Spring Web应用 1.1Spring Tool Suite简介
查看>>
《网站情感化设计与内容策略》一1.3 你好,马斯洛
查看>>
Logic Programming With Prolog学习笔记(一)
查看>>
Java核心技术卷I基础知识3.8.6 中断控制流程语句
查看>>
《Vim实用技巧(第2版)》——2.3 构造可重复的修改
查看>>
恢复高考这些年,关于高考的老照片
查看>>
首届开放科学奖|6个创造性案例示范如何玩转医学大数据
查看>>
《软件功能测试自动化实战教程》—第6章6.4节Action测试输入的参数化
查看>>
如何通过简单的3步恢复Windows 7同时删除Ubuntu
查看>>
网站建设设计前端开发需要学习html和div+css
查看>>
认知应用:大数据的下个转折点
查看>>