需要安装配置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. 类与接口(三)java中的接口与嵌套接口

    一.接口 1. 接口简介 接口: 是java的一种抽象类型,是抽象方法的集合.接口比抽象类更加抽象的抽象类型. 接口语法: [修饰符] [abstract] interface 接口名 [extend ...

  2. LeetCode题解之Maximum Depth of N-ary Tree

    1.题目描述 2.问题分析 利用递归fangf 3.代码 int maxDepth(Node* root) { int res = maxdep(root); return res; } int ma ...

  3. python新生类和经典类简单说明

    经典类: #!/usr/bin/env python #*-* coding:utf-8 *-* class A(): def __init__(self): print 'my name is GF ...

  4. C#安全加密类

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.S ...

  5. 转:jQuery选择器大全(48个代码片段+21幅图演示)

    选择器是jQuery最基础的东西,本文中列举的选择器基本上囊括了所有的jQuery选择器,也许各位通过这篇文章能够加深对jQuery选择器的理解,它们本身用法就非常简单,我更希望的是它能够提升个人编写 ...

  6. NGUI和UGUI图片字 艺术字(Bitmap图片转文字)制作方法

    用图片字而不是图片 美术和程序的配合,需要程序能够很快抓住问题重点并提出解决方案.美术出的图片字比我们使用的字体更好好看,那么是否要一个个图片去拼成数字呢? NGUI创建图片字 准备材料 美术提供的数 ...

  7. Linux运维之系统性能---vmstat工具分析内存的瓶颈

    为了提高磁盘存取效率, Linux做了一些精心的设计, 除了对dentry进行缓存(用于VFS,加速文件路径名到inode的转换), 还采取了两种主要Cache方式:Buffer Cache和Page ...

  8. Grafana3.0.1+Zabbix3.0.4监控系统平台搭建

    前言 本文的Zabbix部分知识只介绍它的基础安装,Zabbix的使用以及配置优化并不在本文的介绍范围之内. 本文只介绍在CentOS6系列下的安装和部署,其他发行版与其他版本号暂不涉及 本文默认使用 ...

  9. BZOJ1121:[POI2008]激光发射器SZK(乱搞)

    Description 多边形相邻边垂直,边长为整数,边平行坐标轴.要在多边形的点上放一些激光发射器和接收器.满足下列要求: 1发射器和接收器不能放置在同一点: 2发射器发出激光可以沿壁反射,最终到达 ...

  10. [笔记] 整除分块 & 异或性质

    整除分块 参考资料:整除分块_peng-ym OI生涯中的各种数论算法的证明 公式 求:\(\sum_{i=1}^{n}\lfloor\frac{n}{i}\rfloor\) 对于每个\(\lfloo ...