需要安装配置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. Appium环境搭建python篇(mac系统)

    1.安装Appium 通过终端安装: 安装nodejs,下载地址:https://nodejs.org/download/,安装完成后打开终端输入node -v,检查是否安装成功 安装npm,打开终端 ...

  2. 转:sqlserver 临时表、表变量、CTE的比较

    1.临时表 1.1 临时表包括:以#开头的局部临时表,以##开头的全局临时表. 1.2 存储 不管是局部临时表,还是全局临时表,都会放存在tempdb数据库中. 1.3 作用域 局部临时表:对当前连接 ...

  3. 从外部导入django模块

    import os import sys sys.path.append("D:\\pyweb\\sf"); # 项目位置(不是app) os.environ.setdefault ...

  4. Linux 文本处理命令

    最近在使用 BASH 进行处理 文本文件的时候,对于文本处理真的是力不从心,今天进行搜集一下linux 中文本处理相关的命令,这样你在进行书写shell 脚本的时候,就能写出更好的方案. 命令搜集: ...

  5. 56_实现类似spring的可配置的AOP框架

    > config.properties  配置文件   key=类名 > BeanFactory  Bean工厂,负责得到bean  getBean("xxx") &g ...

  6. vbs常用函数

    aa '删除文件夹 sub DeleteFolder(objFolder) call OutputLog(objFolder.Path,true) err.Clear On Error Resume ...

  7. Netty入门(四)ByteBuf 字节级别的操作

     Netty 中使用 ByteBuf 代替 Java NIO 提供的 ByteBuffer 作为字节的容器. 一.索引 ByteBuf 提供两个指针变量支持读和写操作,读操作使用 readerInde ...

  8. java使用纯命令行打包项目

    1: javac -d 编译之后的class文件输出目录   指定源文件位置即可.例如 对于多个包下面的源码编译,貌似javac不支持迭代编译,可能需要一次传入多个源码位置进行编译.一种便捷方法就是使 ...

  9. 【Odoo 8开发教程】第一章:Odoo 8.0安装

    转载请注明原文地址:https://www.cnblogs.com/cnodoo/p/10779733.html odoo有三种常见的安装方式:打包程序安装.源码安装以及Docker镜像安装. 一:打 ...

  10. leetcode15—3Sum

    Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find ...