rapidjson代码封装类
以下代码封装了rapidjson增删改查等基本操作:
1 /************************************************************************/ 2 /* CRJSONWrapper,简单封装rapidjson的一些操作 [6/22/2017 by whg] */ 3 /* 部分代码待调试 [6/23/2017 by whg] */ 4 /************************************************************************/ 5 #pragma once 6 #ifndef WHG_RJSON_WRAPPER 7 #define WHG_RJSON_WRAPPER 8 #include <iostream> 9 #include <windows.h> 10 #include <string> 11 #include <fstream> 12 #include "document.h" 13 #include "writer.h" 14 #include "stringbuffer.h" 15 #include "filereadstream.h" 16 #include "filewritestream.h" 17 18 using namespace rapidjson; 19 using namespace std; 20 #define RJSON 21 22 #ifndef RJSON_NEW_RETURN 23 #define RJSON_NEW_RETURN(pt,classname,ret_val) 24 do{ pt = new classname; 25 if(pt == 0) {return ret_val;} 26 } while (0); 27 #endif 28 29 typedef Value::ConstMemberIterator RJSON_ValueMemberIter; 30 typedef Value::ConstValueIterator RJSON_ValueArrayIter; 31 32 class CRJSONWrapper 33 { 34 public: 35 /* ReadJsonFromFile,本地目录读取json文件,返回读取状态 36 * ret: 操作状态 37 * in: 输入本地路径 38 * out: 输出rapidjson的doc 39 */ 40 static bool ReadDocFromFile(__in const char* filepath,__out rapidjson::Document& doc) 41 { 42 std::ifstream infile(filepath); 43 if (infile) 44 { 45 std::string str,temp; 46 while (getline(infile,temp)) 47 str += temp; 48 infile.close(); 49 rapidjson::StringStream buf(str.c_str()); 50 doc.ParseStream(buf); 51 return true; 52 } 53 return false; 54 } 55 #define RJSON_READ_DOC_FROM_FILE(filepath,doc) CRJSONWrapper::ReadDocFromFile(filepath,doc) 56 57 /* ReadJsonFromString,string读取json文件,返回读取状态 58 * ret: 操作状态 59 * in: 输入本地路径 60 * out: 输出rapidjson的doc 61 */ 62 static bool ReadDocFromString(__in const std::string& strIn,__out rapidjson::Document& doc) 63 { 64 if (strIn.empty()) 65 return false; 66 doc.Parse(strIn.c_str()); 67 return true; 68 } 69 #define RJSON_READ_DOC_FROM_STRING(strIn,doc) CRJSONWrapper::ReadDocFromString(strIn,doc) 70 71 /* WriteDocToString,doc转成string,返回操作状态 72 * ret: 操作状态 73 * in: 输入本地路径 74 * out: 输出rapidjson的doc 75 */ 76 static bool WriteDocToFile(__in const rapidjson::Document& doc,__in const std::string& strFilePath) 77 { 78 StringBuffer buffer; 79 rapidjson::Writer<StringBuffer> writer(buffer); 80 doc.Accept(writer); 81 std::string strJson(buffer.GetString(), buffer.GetSize()); 82 std::ofstream outfile; 83 outfile.open(strFilePath.c_str()); 84 if (outfile.fail()) 85 return false; 86 outfile << strJson; 87 outfile.close(); 88 return true; 89 } 90 #define RJSON_WRITE_VALUE_TO_FILE(value,strFilePath) CRJSONWrapper::WriteDocToFile((Document&)value,strFilePath) 91 92 /* WriteDocToString,doc转成string,返回操作状态 93 * ret: 操作状态 94 * in: 输入本地路径 95 * out: 输出rapidjson的doc 96 */ 97 static bool WriteDocToString(__in const rapidjson::Document& doc,__out std::string& strOut) 98 { 99 StringBuffer buffer; 100 rapidjson::Writer<StringBuffer> writer(buffer); 101 doc.Accept(writer); 102 strOut = buffer.GetString(); 103 return true; 104 } 105 #define RJSON_WRITE_DOC_TO_STRING(value,strOut) CRJSONWrapper::WriteDocToString((Document&)value,strOut) 106 107 // 定义document [6/23/2017 by whg] 108 #define RJSON_DOC(doc) rapidjson::Document doc; 109 110 // 定义Value,type如kArrayType等[6/23/2017 by whg] 111 #define RJSON_VALUE(value,valuetype) Value value(valuetype); 112 113 // 从Document获取allocator[6/23/2017 by whg] 114 #define RJSON_DOC_GET_ALLOC(doc,ALLOC) Document::AllocatorType& ALLOC = doc.GetAllocator(); 115 116 // 从VALUE中获取member的value [7/13/2017 by whg] 117 #define RJSON_VALUE_GET_MEMBER_STRING(value,memName,memValue) 118 if (value.IsObject()) 119 { 120 if (value.HasMember(memName) && value[memName].IsInt()) 121 { 122 int id = value[memName].GetInt(); 123 char ch[100]={0}; 124 sprintf_s(ch,"%d",id); 125 memValue = std::string(ch); 126 } 127 else if (value.HasMember(memName) && value[memName].IsString()) 128 { 129 memValue = value[memName].GetString(); 130 } 131 } 132 133 // 从value中获取object对象[8/10/2017 by whg] 134 #define RJSON_VALUE_GET_OBJECT_MEMBER(father,memName,child) 135 if (father.IsObject()) 136 { 137 if (father.HasMember(memName) && father[memName].IsObject()) 138 { 139 child = father[memName]; 140 } 141 } 142 143 // 从value中获取array对象[8/10/2017 by whg] 144 #define RJSON_VALUE_GET_ARRAY_MEMBER(father,memName,child) 145 if (father.IsObject()) 146 { 147 if (father.HasMember(memName) && father[memName].IsArray()) 148 { 149 child = father[memName]; 150 } 151 } 152 153 /* rapidjson-增操作*/ 154 // 为Document增加成员 [6/23/2017 by whg] 155 #define RJSON_DOC_ADD_STRING_MEMBER(doc,strKEY,strVALUE) doc.AddMember(Value(strKEY, doc.GetAllocator()).Move(), Value(strVALUE, doc.GetAllocator()).Move(), doc.GetAllocator()); 156 157 // 为value增加成员 [6/23/2017 by whg] 158 #define RJSON_VALUE_ADD_STRING_MEMBER(value,strKEY,strVALUE,ALLOC) value.AddMember(Value(strKEY, ALLOC).Move(), Value(strVALUE, ALLOC).Move(), ALLOC); 159 160 // 为Document增加Value [6/23/2017 by whg] 161 #define RJSON_DOC_ADD_VALUE(doc,strKEY,value) doc.AddMember(strKEY, value, doc.GetAllocator()); 162 163 // 为Value增加成员 [6/23/2017 by whg] 164 #define RJSON_VALUE_ADD_STRING_MEMBER(value,strKEY,strVALUE,ALLOC) value.AddMember(Value(strKEY, ALLOC).Move(), Value(strVALUE, ALLOC).Move(), ALLOC); 165 166 // 为Document增加Value [6/23/2017 by whg] 167 #define RJSON_VALUE_ADD_VALUE(father,strKEY,child,ALLOC) father.AddMember(strKEY, child, ALLOC); 168 169 // 为类型为Array的Value增加成员 [6/23/2017 by whg] 170 #define RJSON_ARRAY_ADD_VALUE(array,value,ALLOC) if(array.IsArray()){array.PushBack(value, ALLOC);} 171 172 // value的member迭代操作[6/23/2017 by whg] 173 #define RJSON_ITER_MEMBER_BEGIN(value,mem_iter) for(RJSON_ValueMemberIter mem_iter=value.MemberBegin();mem_iter!=value.MemberEnd();mem_iter++) 174 { 175 #define RJSON_ITER_MEMBER_END 176 } 177 178 // 获取迭代器mem_iter的name,所有name都是string [6/23/2017 by whg] 179 #define RJSON_ITER_MEMBER_GETNAME(mem_iter,strName) {strName = mem_iter->name.GetString();} 180 181 // 获取迭代器mem_iter的value,所有name都是string [6/23/2017 by whg] 182 #define RJSON_ITER_MEMBER_GETSTRVALUE(mem_iter,strValue) if(mem_iter->value.IsString()){ 183 strValue = mem_iter->value.GetString();} 184 185 // 数组value的iterator迭代操作[6/23/2017 by whg] 186 #define RJSON_ITER_ARRAY_BEGIN(value,array_iter) for (RJSON_ValueArrayIter array_iter=value.Begin();array_iter!=value.End();array_iter++) 187 { 188 #define RJSON_ITER_ARRAY_End 189 } 190 191 // 根据数组项的name取出数组为string类型的值 [6/23/2017 by whg] 192 #define RJSON_VALUE_GET_STRVALUE(value,strName,strValue) if (value.HasMember(strName) && value[strName].IsString()){ 193 strValue = value[strName].GetString();} 194 195 // object删除member成员[6/23/2017 by whg] 196 #define RJSON_REMOVE_MEMBER(value,strMemName) if (value.IsObject() && value.HasMember(std::string(strMemName).c_str())){ 197 value.RemoveMember(std::string(strMemName).c_str());} 198 199 /* DeleteArrayItem,元素为object的array,删除含有指定name和value的对应object,返回操作状态 200 * ret: 操作状态 201 * in: 元素为object的array,member的name和value(string类型),删除所有查找到的object还是删除第一次 202 */ 203 // 未能实现删除所有相关元素,待测试[6/23/2017 by whg] 204 static bool DeleteArrayItem(__in const rapidjson::Value& value,__in std::string strKey,__in std::string strValue,__in bool bOnlyFirst = true) 205 { 206 if (!value.IsArray()) 207 return false; 208 //查找操作 209 RJSON_ITER_ARRAY_BEGIN(value,iterObject) 210 { 211 RJSON_ITER_MEMBER_BEGIN((*iterObject),iterMem) 212 { 213 string strname,strvalue; 214 RJSON_ITER_MEMBER_GETNAME(iterMem,strname); 215 RJSON_ITER_MEMBER_GETSTRVALUE(iterMem,strvalue); 216 //RJSON_DOC_REMOVE_MEMBER((*iter2),strname); 217 if (strname.compare(strKey) == 0 && strvalue.compare(strValue) == 0) 218 { 219 Value& v = const_cast<Value&>(value); 220 if (iterObject != v.End()+1) 221 { 222 iterObject = v.Erase(iterObject); 223 } 224 else 225 v.Erase(iterObject); 226 227 if (bOnlyFirst) 228 return true; 229 break; 230 } 231 } 232 RJSON_ITER_MEMBER_END 233 } 234 RJSON_ITER_ARRAY_End 235 return true; 236 } 237 #define RJSON_ARRAY_REMOVE_OBJECT(value,strKey,strValue) CRJSONWrapper::DeleteArrayItem(value,strKey,strValue) 238 }; 239 #endif
增删改查操作:
增操作的一个例子:
1 std::string res; 2 RJSON_DOC(doc); 3 RJSON_DOC_GET_ALLOC(doc,alloc); 4 doc.SetObject(); 5 RJSON_DOC_ADD_MEMBER(doc,"type","config"); 6 // 添加data节点 [8/8/2017 by whg] 7 RJSON_VALUE(data,kObjectType); 8 // data下添加music节点 [8/8/2017 by whg] 9 RJSON_VALUE(music,kObjectType); 10 RJSON_VALUE_ADD_MEMBER(music,"vo","1",alloc); 11 RJSON_VALUE_ADD_MEMBER(music,"si","123",alloc); 12 RJSON_VALUE_ADD_VALUE(data,"mu",music,alloc); 13 // data下添加percent节点 [8/8/2017 by whg] 14 RJSON_VALUE(percent,kObjectType); 15 RJSON_VALUE_ADD_MEMBER(percent,"volume","11",alloc); 16 RJSON_VALUE_ADD_VALUE(data,"percent",percent,alloc); 17 // data下添加audio节点 [8/8/2017 by whg] 18 RJSON_VALUE(audio,kObjectType); 19 RJSON_VALUE_ADD_MEMBER(audio,"volume","70",alloc); 20 RJSON_VALUE_ADD_VALUE(data,"audio",audio,alloc); 21 // doc下添加sp_data节点 [8/8/2017 by whg] 22 RJSON_DOC_ADD_VALUE(doc,"sp_data",data); 23 RJSON_WRITE_DOC_TO_STRING(doc,res);