/*******************************************************************
* 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,