需要安装配置RapidJSON库

/*******************************************************************
* summery: 提供便捷的方法操作rapidjson
* author: hejl
* date: 2017-02-17
* description: 有效避免string(NULL), element=NULL, strdup(NULL)判断
******************************************************************/ #ifndef _JSON_HPP_
#define _JSON_HPP_
#include <string>
#include "document.h"
#include "rapidjson.h"
#include "prettywriter.h" using namespace rapidjson;
using std::string; /*****************************
关键字说明:
Object : json中的{key: value}形式类型
Array : json中的[e1, e2, e3]形式类型
Value : 对json数据类型的抽象,包括Object,Array,int,string,是Object和Array的基类
GetStr/GetInt : 获取json内部某一部分的字符串值,通通1st参数返回,最后一参数是父节点Value,
中间参数用于定位第几元素(参数类型是int)或指定key对应的值(参数类型是const char*) usage example:
Document doc;
char jsonstr[] = "{ \"key1\": 123, \"key2\": \"string value\", \"key3\": [100, 200, \"str300\"] }"
doc.Parse<0>(jsonstr.c_str());
if (doc.HasParseError()) { ... }
// get key2 string value
std::string val2;
int result = Rjson::GetValue(val2, "key2", &doc);
// get key1 integer value
int val1;
int result = Rjson::GetValue(val1, "key1", &doc);
// get array [1] integer of key3
int val3_1;
int result = Rjson::GetValue(val3_1, "key3", 1, &doc);
// get array [2] string of key3
string val3_2;
int result = Rjson::GetValue(val3_2, "key3", 2, &doc);
*****************************/ class Rjson
{
public:
static int GetValue(const Value** value, const char* name, const Value* parent)
{
if (parent && name && parent->IsObject())
{
Value::ConstMemberIterator itr = parent->FindMember(name);
if (itr != parent->MemberEnd())
{
*value = &(itr->value);
return ;
}
} return -;
} static int GetValue(const Value** value, int idx, const Value* parent)
{
if (parent && idx >= && parent->IsArray() && idx < (int)parent->Size())
{
*value = &( (*parent)[idx]);
return ;
} return -;
} template<typename T>
static int GetObject(const Value** value, T t, const Value* parent)
{
if ( == GetValue(value, t, parent) && (*value)->IsObject())
{
return ;
} *value = NULL;
return -;
} template<typename T>
static int GetArray(const Value** value, T t, const Value* parent)
{
if ( == GetValue(value, t, parent) && (*value)->IsArray())
{
return ;
} *value = NULL;
return -;
} template<typename T>
static int GetStr(string& str, T t, const Value* parent)
{
const Value* value = NULL;
if ( == GetValue(&value, t, parent) && value->IsString())
{
str = value->GetString();
return ;
} return -;
} template<typename T>
static int GetInt(int& n, T t, const Value* parent)
{
const Value* value = NULL;
if ( == GetValue(&value, t, parent) && value->IsInt())
{
n = value->GetInt();
return ;
} return -;
} /////////////////////////////////////////// template<typename T1, typename T2>
static int GetValue(const Value** value, T1 t1, T2 t2, const Value* parent)
{
const Value* tmpv = NULL;
int ret = GetValue(&tmpv, t1, parent);
if ( == ret)
{
return GetValue(value, t2, tmpv);
} return -;
} template<typename T1, typename T2>
static int GetObject(const Value** value, T1 t1, T2 t2, const Value* parent)
{
if ( == GetValue(value, t1, t2, parent) && (*value)->IsObject())
{
return ;
}
*value = NULL;
return -;
} template<typename T1, typename T2>
static int GetArray(const Value** value, T1 t1, T2 t2, const Value* parent)
{
if ( == GetValue(value, t1, t2, parent) && (*value)->IsArray())
{
return ;
}
*value = NULL;
return -;
} template<typename T1, typename T2>
static int GetStr(string& str, T1 t1, T2 t2, const Value* parent)
{
const Value* value = NULL;
if ( == GetValue(&value, t1, t2, parent) && value->IsString())
{
str = value->GetString();
return ;
} return -;
} template<typename T1, typename T2>
static int GetInt(int& n, T1 t1, T2 t2, const Value* parent)
{
const Value* value = NULL;
if ( == GetValue(&value, t1, t2, parent) && value->IsInt())
{
n = value->GetInt();
return ;
} return -;
} /////////////////////////////////////////////// static int ToString(string& str, const Value* node)
{
if (node)
{
StringBuffer sb;
Writer<StringBuffer> writer(sb); // PrettyWriter
node->Accept(writer);
str = sb.GetString();
return ;
} return -;
} static string ToString(const Value* node)
{
if (node)
{
StringBuffer sb;
Writer<StringBuffer> writer(sb); // PrettyWriter
node->Accept(writer);
return sb.GetString();
} return "";
}
}; #endif

基于RapidJSON的操作库的更多相关文章

  1. C++ 基于rapidjson对json字符串的进行序列化与反序列化

    json字符串的解析以封装在我们开发过程中经常见到, 尤其在socket通信上面, 在一次项目中碰到json字符串的进行解析, 而公司有没有封装好的库, 于是就自己基于开源的库进行了一次封装, 接下是 ...

  2. 2014 年10个最佳的PHP图像操作库

    2014 年10个最佳的PHP图像操作库   Thomas Boutell 以及众多的开发者创造了以GD图形库闻名的一个图形软件库,用于动态的图形计算. GD提供了对于诸如C, Perl, Pytho ...

  3. 2014 年10个最佳的PHP图像操作库--留着有用

    Thomas Boutell 以及众多的开发者创造了以GD图形库闻名的一个图形软件库,用于动态的图形计算. GD提供了对于诸如C, Perl, Python, PHP, OCaml等等诸多编程语言的支 ...

  4. 10个最佳的PHP图像操作库

    Thomas Boutell 以及众多的开发者创造了以GD图形库闻名的一个图形软件库,用于动态的图形计算. GD提供了对于诸如C, Perl, Python, PHP, OCaml等等诸多编程语言的支 ...

  5. 利用cocoapods创建基于git的私有库

    上一篇文章记录了我利用cocoapods创建基于SVN的私有库的全部过程,今天我再记录一下基于git创建的过程. 整体先说明一下创建一个私有的podspec包括如下那么几个步骤: 创建并设置一个私有的 ...

  6. 利用cocoapods创建基于git的私有库Spec Repo

    上一篇文章记录了我利用cocoapods创建基于SVN的私有库的全部过程,今天我再记录一下基于git创建的过程. 整体先说明一下创建一个私有的podspec包括如下那么几个步骤: 创建并设置一个私有的 ...

  7. IOTutility 一个轻量级的 IOT 基础操作库

    IOTutility 一个轻量级的 IOT 基础操作库 Base utility for IOT devices, networking, controls etc... IOTutility 的目的 ...

  8. Element没更新了?Element没更新,基于El的扩展库更新

    think-vuele 基于Vue和ElementUI框架进行整合二次开发的一个框架.提供一些elementUI没有的或当时没有的控件.优化了或简化了便于2B软件开发的一些控件 demo:http:/ ...

  9. php版的redis操作库predis操作大全

    转载于:http://www.itxuexiwang.com/a/shujukujishu/redis/2016/0216/146.html predis是php连接redis的操作库,由于它完全使用 ...

随机推荐

  1. Ubuntu 18.04 Server 设置静态IP

    一.背景 Netplan是Ubuntu 17.10中引入的一种新的命令行网络配置实用程序,用于在Ubuntu系统中轻松管理和配置网络设置.它允许您使用YAML抽象来配置网络接口.它可与NetworkM ...

  2. Jboss 4.2.3配置与优化

    1      Jboss内存优化 修改这个两参数,给jvm分配适当的内存,一般为服务器的3/4内存量,推荐至少使用4G内存. 另外添加两个参数 -XX:+UseParallelGC -XX:+UseP ...

  3. Docker容器学习与分享04

    Docker容器的基本操作(2) 基于docker分享03的centos容器,接着学习docker容器的基本操作. docker分享03中创建了一个centos镜像,如果想要查看容器的具体信息就要使用 ...

  4. COM动态添加删除成员,类似JavaScript中调用的对象

    在JavaScript中调用对象时,可动态添加删除成员如: var obj=new Object; obj.member1='aaaaa'; obj.fun1=function() { alert(' ...

  5. 更改win系统的鼠标样式

    一.找一个你心仪的鼠标样式(.cur文件),并放到 C:\Windows\Cursors 目录下 二.打开,控制面板 -> 硬件和声音 -> 鼠标 ,如下图: 三.浏览鼠标目录,找到你存放 ...

  6. windows下安装python3 新手上路

    本文只针对刚刚拿到“驾照”的实习生 老司机回去开车.. 下载python 地址:https://www.python.org/ 选择Downloads下的windows 选择自己合适的版本  下面的是 ...

  7. 【Python】【unittest】unittest测试框架中setup,teardown与setupclass,teardownclass的区别

    # -*- coding:utf-8 -*- import unittest def runTest(testcaseclass,testcase=[]): suite = unittest.Test ...

  8. CompletionService简讲

    背景 最近在项目中看到太多后台task中使用Executor框架,提交任务后,把future都一个个加入到list,再一个个get这些future的代码. 这个的问题在于一方面没有时限,可能会被某些运 ...

  9. requirejs原理深究以及r.js和gulp的打包【转】

    转自:http://blog.csdn.net/why_fly/article/details/75088378 requirejs原理 requirejs的用法和原理分析:https://githu ...

  10. objc.io 待看文章

    https://objccn.io/issues/ https://objccn.io/issues/ 使用 VIPER 构建 iOS 应用 并发编程