rapidjson代码封装类

  以下代码封装了rapidjson增删改查等基本操作:

 /************************************************************************/
/* CRJSONWrapper,简单封装rapidjson的一些操作 [6/22/2017 by whg] */
/* 部分代码待调试 [6/23/2017 by whg] */
/************************************************************************/
#pragma once
#ifndef WHG_RJSON_WRAPPER
#define WHG_RJSON_WRAPPER
#include <iostream>
#include <windows.h>
#include <string>
#include <fstream>
#include "document.h"
#include "writer.h"
#include "stringbuffer.h"
#include "filereadstream.h"
#include "filewritestream.h" using namespace rapidjson;
using namespace std;
#define RJSON #ifndef RJSON_NEW_RETURN
#define RJSON_NEW_RETURN(pt,classname,ret_val) \
do{ pt = new classname;\
if(pt == ) {return ret_val;}\
} while ();
#endif typedef Value::ConstMemberIterator RJSON_ValueMemberIter;
typedef Value::ConstValueIterator RJSON_ValueArrayIter; class CRJSONWrapper
{
public:
/* ReadJsonFromFile,本地目录读取json文件,返回读取状态
* ret: 操作状态
* in: 输入本地路径
* out: 输出rapidjson的doc
*/
static bool ReadDocFromFile(__in const char* filepath,__out rapidjson::Document& doc)
{
std::ifstream infile(filepath);
if (infile)
{
std::string str,temp;
while (getline(infile,temp))
str += temp;
infile.close();
rapidjson::StringStream buf(str.c_str());
doc.ParseStream(buf);
return true;
}
return false;
}
#define RJSON_READ_DOC_FROM_FILE(filepath,doc) CRJSONWrapper::ReadDocFromFile(filepath,doc) /* ReadJsonFromString,string读取json文件,返回读取状态
* ret: 操作状态
* in: 输入本地路径
* out: 输出rapidjson的doc
*/
static bool ReadDocFromString(__in const std::string& strIn,__out rapidjson::Document& doc)
{
if (strIn.empty())
return false;
doc.Parse(strIn.c_str());
return true;
}
#define RJSON_READ_DOC_FROM_STRING(strIn,doc) CRJSONWrapper::ReadDocFromString(strIn,doc) /* WriteDocToString,doc转成string,返回操作状态
* ret: 操作状态
* in: 输入本地路径
* out: 输出rapidjson的doc
*/
static bool WriteDocToFile(__in const rapidjson::Document& doc,__in const std::string& strFilePath)
{
StringBuffer buffer;
rapidjson::Writer<StringBuffer> writer(buffer);
doc.Accept(writer);
std::string strJson(buffer.GetString(), buffer.GetSize());
std::ofstream outfile;
outfile.open(strFilePath.c_str());
if (outfile.fail())
return false;
outfile << strJson;
outfile.close();
return true;
}
#define RJSON_WRITE_VALUE_TO_FILE(value,strFilePath) CRJSONWrapper::WriteDocToFile((Document&)value,strFilePath) /* WriteDocToString,doc转成string,返回操作状态
* ret: 操作状态
* in: 输入本地路径
* out: 输出rapidjson的doc
*/
static bool WriteDocToString(__in const rapidjson::Document& doc,__out std::string& strOut)
{
StringBuffer buffer;
rapidjson::Writer<StringBuffer> writer(buffer);
doc.Accept(writer);
strOut = buffer.GetString();
return true;
}
#define RJSON_WRITE_DOC_TO_STRING(value,strOut) CRJSONWrapper::WriteDocToString((Document&)value,strOut) // 定义document [6/23/2017 by whg]
#define RJSON_DOC(doc) rapidjson::Document doc; // 定义Value,type如kArrayType等[6/23/2017 by whg]
#define RJSON_VALUE(value,valuetype) Value value(valuetype); // 从Document获取allocator[6/23/2017 by whg]
#define RJSON_DOC_GET_ALLOC(doc,ALLOC) Document::AllocatorType& ALLOC = doc.GetAllocator(); // 从VALUE中获取member的value [7/13/2017 by whg]
#define RJSON_VALUE_GET_MEMBER_STRING(value,memName,memValue) \
if (value.IsObject())\
{\
if (value.HasMember(memName) && value[memName].IsInt())\
{\
int id = value[memName].GetInt();\
char ch[]={};\
sprintf_s(ch,"%d",id);\
memValue = std::string(ch);\
}\
else if (value.HasMember(memName) && value[memName].IsString())\
{\
memValue = value[memName].GetString();\
}\
} // 从value中获取object对象[8/10/2017 by whg]
#define RJSON_VALUE_GET_OBJECT_MEMBER(father,memName,child) \
if (father.IsObject())\
{\
if (father.HasMember(memName) && father[memName].IsObject())\
{\
child = father[memName];\
}\
} // 从value中获取array对象[8/10/2017 by whg]
#define RJSON_VALUE_GET_ARRAY_MEMBER(father,memName,child) \
if (father.IsObject())\
{\
if (father.HasMember(memName) && father[memName].IsArray())\
{\
child = father[memName];\
}\
} /* rapidjson-增操作*/
// 为Document增加成员 [6/23/2017 by whg]
#define RJSON_DOC_ADD_STRING_MEMBER(doc,strKEY,strVALUE) doc.AddMember(Value(strKEY, doc.GetAllocator()).Move(), Value(strVALUE, doc.GetAllocator()).Move(), doc.GetAllocator()); // 为value增加成员 [6/23/2017 by whg]
#define RJSON_VALUE_ADD_STRING_MEMBER(value,strKEY,strVALUE,ALLOC) value.AddMember(Value(strKEY, ALLOC).Move(), Value(strVALUE, ALLOC).Move(), ALLOC); // 为Document增加Value [6/23/2017 by whg]
#define RJSON_DOC_ADD_VALUE(doc,strKEY,value) doc.AddMember(strKEY, value, doc.GetAllocator()); // 为Value增加成员 [6/23/2017 by whg]
#define RJSON_VALUE_ADD_STRING_MEMBER(value,strKEY,strVALUE,ALLOC) value.AddMember(Value(strKEY, ALLOC).Move(), Value(strVALUE, ALLOC).Move(), ALLOC); // 为Document增加Value [6/23/2017 by whg]
#define RJSON_VALUE_ADD_VALUE(father,strKEY,child,ALLOC) father.AddMember(strKEY, child, ALLOC); // 为类型为Array的Value增加成员 [6/23/2017 by whg]
#define RJSON_ARRAY_ADD_VALUE(array,value,ALLOC) if(array.IsArray()){array.PushBack(value, ALLOC);} // value的member迭代操作[6/23/2017 by whg]
#define RJSON_ITER_MEMBER_BEGIN(value,mem_iter) for(RJSON_ValueMemberIter mem_iter=value.MemberBegin();mem_iter!=value.MemberEnd();mem_iter++)\
{
#define RJSON_ITER_MEMBER_END \
} // 获取迭代器mem_iter的name,所有name都是string [6/23/2017 by whg]
#define RJSON_ITER_MEMBER_GETNAME(mem_iter,strName) {strName = mem_iter->name.GetString();} // 获取迭代器mem_iter的value,所有name都是string [6/23/2017 by whg]
#define RJSON_ITER_MEMBER_GETSTRVALUE(mem_iter,strValue) if(mem_iter->value.IsString()){\
strValue = mem_iter->value.GetString();} // 数组value的iterator迭代操作[6/23/2017 by whg]
#define RJSON_ITER_ARRAY_BEGIN(value,array_iter) for (RJSON_ValueArrayIter array_iter=value.Begin();array_iter!=value.End();array_iter++)\
{
#define RJSON_ITER_ARRAY_End\
} // 根据数组项的name取出数组为string类型的值 [6/23/2017 by whg]
#define RJSON_VALUE_GET_STRVALUE(value,strName,strValue) if (value.HasMember(strName) && value[strName].IsString()){\
strValue = value[strName].GetString();} // object删除member成员[6/23/2017 by whg]
#define RJSON_REMOVE_MEMBER(value,strMemName) if (value.IsObject() && value.HasMember(std::string(strMemName).c_str())){\
value.RemoveMember(std::string(strMemName).c_str());} /* DeleteArrayItem,元素为object的array,删除含有指定name和value的对应object,返回操作状态
* ret: 操作状态
* in: 元素为object的array,member的name和value(string类型),删除所有查找到的object还是删除第一次
*/
// 未能实现删除所有相关元素,待测试[6/23/2017 by whg]
static bool DeleteArrayItem(__in const rapidjson::Value& value,__in std::string strKey,__in std::string strValue,__in bool bOnlyFirst = true)
{
if (!value.IsArray())
return false;
//查找操作
RJSON_ITER_ARRAY_BEGIN(value,iterObject)
{
RJSON_ITER_MEMBER_BEGIN((*iterObject),iterMem)
{
string strname,strvalue;
RJSON_ITER_MEMBER_GETNAME(iterMem,strname);
RJSON_ITER_MEMBER_GETSTRVALUE(iterMem,strvalue);
//RJSON_DOC_REMOVE_MEMBER((*iter2),strname);
if (strname.compare(strKey) == && strvalue.compare(strValue) == )
{
Value& v = const_cast<Value&>(value);
if (iterObject != v.End()+)
{
iterObject = v.Erase(iterObject);
}
else
v.Erase(iterObject); if (bOnlyFirst)
return true;
break;
}
}
RJSON_ITER_MEMBER_END
}
RJSON_ITER_ARRAY_End
return true;
}
#define RJSON_ARRAY_REMOVE_OBJECT(value,strKey,strValue) CRJSONWrapper::DeleteArrayItem(value,strKey,strValue)
};
#endif

  增删改查操作:

  增操作的一个例子:

   std::string res;
RJSON_DOC(doc);
RJSON_DOC_GET_ALLOC(doc,alloc);
doc.SetObject();
RJSON_DOC_ADD_MEMBER(doc,"type","config");
// 添加data节点 [8/8/2017 by whg]
RJSON_VALUE(data,kObjectType);
// data下添加music节点 [8/8/2017 by whg]
RJSON_VALUE(music,kObjectType);
RJSON_VALUE_ADD_MEMBER(music,"vo","",alloc);
RJSON_VALUE_ADD_MEMBER(music,"si","",alloc);
RJSON_VALUE_ADD_VALUE(data,"mu",music,alloc);
// data下添加percent节点 [8/8/2017 by whg]
RJSON_VALUE(percent,kObjectType);
RJSON_VALUE_ADD_MEMBER(percent,"volume","",alloc);
RJSON_VALUE_ADD_VALUE(data,"percent",percent,alloc);
// data下添加audio节点 [8/8/2017 by whg]
RJSON_VALUE(audio,kObjectType);
RJSON_VALUE_ADD_MEMBER(audio,"volume","",alloc);
RJSON_VALUE_ADD_VALUE(data,"audio",audio,alloc);
// doc下添加sp_data节点 [8/8/2017 by whg]
RJSON_DOC_ADD_VALUE(doc,"sp_data",data);
RJSON_WRITE_DOC_TO_STRING(doc,res);
05-16 00:34