C++對(duì)Json數(shù)據(jù)的友好處理實(shí)現(xiàn)過程
背景
C/C++客戶端需要接收和發(fā)送JSON格式的數(shù)據(jù)到后端以實(shí)現(xiàn)通訊和數(shù)據(jù)交互。C++沒有現(xiàn)成的處理JSON格式數(shù)據(jù)的接口,直接引用第三方庫(kù)還是避免不了拆解拼接。考慮到此項(xiàng)目將會(huì)有大量JSON數(shù)據(jù)需要處理,避免不了重復(fù)性的拆分拼接。所以打算封裝一套C++結(jié)構(gòu)體對(duì)象轉(zhuǎn)JSON數(shù)據(jù)、JSON數(shù)據(jù)直接裝C++結(jié)構(gòu)體對(duì)象的接口,類似于數(shù)據(jù)傳輸中常見的序列化和反序列化,以方便后續(xù)處理數(shù)據(jù),提高開發(fā)效率。
設(shè)計(jì)
目標(biāo):
- 通過簡(jiǎn)單接口就能將C++結(jié)構(gòu)體對(duì)象實(shí)例轉(zhuǎn)換為JSON字符串?dāng)?shù)據(jù),或?qū)⒁淮甁SON字符串?dāng)?shù)據(jù)加載賦值到一個(gè)C++結(jié)構(gòu)體對(duì)象實(shí)例。理想接口:
Json2Object(inJsonString, outStructObject),或者Object2Json(inStructObject, outJsonString) - 支持內(nèi)置基本類型如bool,int,double的Json轉(zhuǎn)換,支持自定義結(jié)構(gòu)體的Json轉(zhuǎn)換,支持上述類型作為元素?cái)?shù)組的Json轉(zhuǎn)換,以及支持嵌套的結(jié)構(gòu)體的Json轉(zhuǎn)換
效果:
先上單元測(cè)試代碼
TEST_CASE("解析結(jié)構(gòu)體數(shù)組到JSON串", "[json]")
{
struct DemoChildrenObject
{
bool boolValue;
int intValue;
std::string strValue;
/*JSON相互轉(zhuǎn)換成員變量聲明(必需)*/
JSONCONVERT2OBJECT_MEMEBER_REGISTER(boolValue, intValue, strValue)
};
struct DemoObjct
{
bool boolValue;
int intValue;
std::string strValue;
/*嵌套的支持JSON轉(zhuǎn)換的結(jié)構(gòu)體成員變量,數(shù)組形式*/
std::vector< DemoChildrenObject> children;
/*JSON相互轉(zhuǎn)換成員變量聲明(必需)*/
JSONCONVERT2OBJECT_MEMEBER_REGISTER(boolValue, intValue, strValue, children)
};
DemoObjct demoObj;
/*開始對(duì)demoObj對(duì)象的成員變量進(jìn)行賦值*/
demoObj.boolValue = true;
demoObj.intValue = 321;
demoObj.strValue = "hello worLd";
DemoChildrenObject child1;
child1.boolValue = true;
child1.intValue = 1000;
child1.strValue = "hello worLd child1";
DemoChildrenObject child2;
child2.boolValue = true;
child2.intValue = 30005;
child2.strValue = "hello worLd child2";
demoObj.children.push_back(child1);
demoObj.children.push_back(child2);
/*結(jié)束對(duì)demoObj對(duì)象的成員變量的賦值*/
std::string jsonStr;
/*關(guān)鍵轉(zhuǎn)換函數(shù)*/
REQUIRE(Object2Json(jsonStr, demoObj));
std::cout << "returned json format: " << jsonStr << std::endl;
/*打印的內(nèi)容如下:
returned json format: {
"boolValue" : true,
"children" : [
{
"boolValue" : true,
"intValue" : 1000,
"strValue" : "hello worLd child1"
},
{
"boolValue" : true,
"intValue" : 30005,
"strValue" : "hello worLd child2"
}
],
"intValue" : 321,
"strValue" : "hello worLd"
}
*/
DemoObjct demoObj2;
/*關(guān)鍵轉(zhuǎn)換函數(shù)*/
REQUIRE(Json2Object(demoObj2, jsonStr));
/*校驗(yàn)轉(zhuǎn)換后的結(jié)構(gòu)體變量中各成員變量的內(nèi)容是否如預(yù)期*/
REQUIRE(demoObj2.boolValue == true);
REQUIRE(demoObj2.intValue == 321);
REQUIRE(demoObj2.strValue == "hello worLd");
REQUIRE(demoObj2.children.size() == 2);
REQUIRE(demoObj.children[0].boolValue == true);
REQUIRE(demoObj.children[0].intValue == 1000);
REQUIRE(demoObj.children[0].strValue == "hello worLd child1");
REQUIRE(demoObj.children[1].boolValue == true);
REQUIRE(demoObj.children[1].intValue == 30005);
REQUIRE(demoObj.children[1].strValue == "hello worLd child2");
}
實(shí)現(xiàn)
本次我們只關(guān)注怎么友好地在結(jié)構(gòu)體與Json字符串之間進(jìn)行轉(zhuǎn)換,而不深入關(guān)注JSon字符串具體如何與基本數(shù)據(jù)類型進(jìn)行轉(zhuǎn)換。這個(gè)已經(jīng)有不少的第三方庫(kù)幫我們解決這個(gè)問題,如cJSON、Jsoncpp、rapidjson,不必再重復(fù)造輪子。
此次我們選擇了JsonCPP作為底層的JSON解析支持,如果想替換成其他三方庫(kù)也比較簡(jiǎn)單,修改對(duì)應(yīng)嵌入的內(nèi)容即可。
我們的目標(biāo)是實(shí)現(xiàn)兩個(gè)接口:
Json2Object(inJsonString, outStructObject)Object2Json(inStructObject, outJsonString)
結(jié)合JsonCPP自身定義的類型,我們進(jìn)一步需要實(shí)現(xiàn)的是:
Json2Object(const Json::Value& jsonTypeValue, outStructObject)Object2Json(inStructObject, const std::string& key, Json::Value& jsonTypeValue)
基本數(shù)據(jù)類型轉(zhuǎn)換
對(duì)于如bool、int、double、string等基本數(shù)據(jù)類型,該實(shí)現(xiàn)均較為簡(jiǎn)單:
/*int 類型支持*/
static bool Json2Object(int& aimObj, const Json::Value& jsonTypeValue)
{
if (jsonTypeValue.isNull() || !jsonTypeValue.isInt()) {
return false;
} else {
aimObj = jsonTypeValue.asInt();
return true;
}
}
static bool Object2Json(Json::Value& jsonTypeValue, const std::string& key, const int& value)
{
jsonTypeValue[key] = value;
return true;
}
/*std::string 字符串類型支持*/
static bool Json2Object(std::string& aimObj, const Json::Value& jsonTypeValue)
{
if (jsonTypeValue.isNull() || !jsonTypeValue.isString()) {
return false;
} else {
aimObj = jsonTypeValue.asString();
return true;
}
}
static bool Object2Json(Json::Value& jsonTypeValue, const std::string& key, const std::string& value)
{
jsonTypeValue[key] = value;
return true;
}
自定義數(shù)據(jù)結(jié)構(gòu)類型
對(duì)于自定義的結(jié)構(gòu)體類型,我們要做的就是要保證其成員變量能夠與JSON節(jié)點(diǎn)一一對(duì)應(yīng),并能夠匹配進(jìn)行數(shù)據(jù)填充。
/*Json字符串:
{
"boolValue" : true,
"intValue" : 1234,
"strValue" : "demo object!"
}*/
struct DemoObjct
{
bool boolValue;
int intValue;
std::string strValue;
};
如上面示例,在相互轉(zhuǎn)換過程中,"boolValue"能與DemoObjct對(duì)象中名為boolValue的成員變量對(duì)應(yīng),"strValue"與DemoObjct對(duì)象中名為strValue的成員變量對(duì)應(yīng)。
正常情況下,對(duì)于這種場(chǎng)景,我們只能對(duì)DemoObjct結(jié)構(gòu)體額外實(shí)現(xiàn)處理函數(shù)進(jìn)行數(shù)據(jù)轉(zhuǎn)換,因不同的結(jié)構(gòu)體聲明定義的成員變量都不一樣,所以針對(duì)每個(gè)結(jié)構(gòu)體均需要單獨(dú)實(shí)現(xiàn),工作繁瑣,不通用。
從這里下手,我們要做的就是“隱藏”針對(duì)類結(jié)構(gòu)體實(shí)現(xiàn)的轉(zhuǎn)換函數(shù),利用語(yǔ)言自身的特性(函數(shù)模板等)讓他們幫我們?nèi)プ鲞@些事情。
- 聲明轉(zhuǎn)換成員函數(shù),在這個(gè)成員函數(shù)實(shí)現(xiàn)里,讓每個(gè)成員變量能從JSON原生數(shù)據(jù)中讀取或?qū)懭胫怠?/li>
- 注冊(cè)成員變量,目的是讓轉(zhuǎn)換成員函數(shù)知道需要處理哪些成員變量,每個(gè)成員變量又對(duì)應(yīng)JSON原生數(shù)據(jù)中的哪個(gè)節(jié)點(diǎn)字段,以便匹配讀寫。
- 在外部調(diào)用
Json2Object和Object2Json函數(shù)時(shí),觸發(fā)調(diào)用該轉(zhuǎn)換成員函數(shù),以便填充或輸出成員變量的內(nèi)容。
把大象裝進(jìn)冰箱里只需要三步,我們來看這三步怎么走。
成員變量處理
- 考慮到每個(gè)結(jié)構(gòu)體的成員變量類型和數(shù)量不可控,并且需要將每個(gè)成員變量作為左值(
Json2Object時(shí)),不能簡(jiǎn)單采用數(shù)組枚舉方式處理,可以采用C++11的特性——可變參數(shù)模板,從里到外遍歷處理每個(gè)成員變量
template <typename T>
static bool JsonParse(const std::vector<std::string>& names, int index, const Json::Value& jsonTypeValue, T& arg)
{
const auto key = names[index];
if (!jsonTypeValue.isMember(key) || Json2Object(arg, jsonTypeValue[key])) {
return true;
} else {
return false;
}
}
template <typename T, typename... Args>
static bool JsonParse(const std::vector<std::string>& names, int index, const Json::Value& jsonTypeValue, T& arg, Args&... args)
{
if (!JsonParse(names, index, jsonTypeValue, arg)) {
return false;
} else {
return JsonParse(names, index + 1, jsonTypeValue, args...);
}
}
- 成員變量與JSON原生節(jié)點(diǎn)key字段有對(duì)應(yīng)關(guān)系,初步先簡(jiǎn)單考慮,將成員變量名稱先視為JSON中對(duì)應(yīng)節(jié)點(diǎn)的key名稱。這個(gè)可以通過宏定義的特性實(shí)現(xiàn),將聲明注冊(cè)的成員變量?jī)?nèi)容作為字符串拆分出key名稱列表。
#define JSONCONVERT2OBJECT_MEMEBER_REGISTER(...) \
bool ParseHelpImpl(const Json::Value& jsonTypeValue)
{
std::vector<std::string> names = Member2KeyParseWithStr(#__VA_ARGS__);
return JsonParse(names, 0, jsonTypeValue, __VA_ARGS__);
}
成員變量注冊(cè)
例如DemoObjct這個(gè)類結(jié)構(gòu)體,添加JSONCONVERT2OBJECT_MEMEBER_REGISTER并帶上成員變量的注冊(cè)聲明:
struct DemoObjct
{
bool boolValue;
int intValue;
std::string strValue;
JSONCONVERT2OBJECT_MEMEBER_REGISTER(boolValue, intValue, strValue)
};
等同于:
struct DemoObjct
{
bool boolValue;
int intValue;
std::string strValue;
bool ParseHelpImpl(const Json::Value& jsonTypeValue,
std::vector<std::string> &names)
{
names = Member2KeyParseWithStr("boolValue, intValue, strValue");
//names 得到 ["boolValue","intValue", "strValue"]
//然后帶著這些key逐一從Json中取值賦值到成員變量中
return JsonParse(names, 0, jsonTypeValue, boolValue, intValue, strValue);
}
};
模板匹配防止編譯報(bào)錯(cuò)
到目前為止,核心的功能已經(jīng)實(shí)現(xiàn)。如果目標(biāo)結(jié)構(gòu)體類未添加JSON轉(zhuǎn)換的聲明注冊(cè),外部在使用Json2Object接口時(shí)會(huì)導(dǎo)致編譯報(bào)錯(cuò),提示找不到ParseHelpImpl這個(gè)成員函數(shù)的聲明定義……,我們可以采用enable_if來給未聲明注冊(cè)宏的結(jié)構(gòu)體提供缺省函數(shù)。
template <typename TClass, typename enable_if<HasConverFunction<TClass>::has, int>::type = 0>
static inline bool Json2Object(TClass& aimObj, const Json::Value& jsonTypeValue)
{
std::vector<std::string> names = PreGetCustomMemberNameIfExists(aimObj);
return aimObj.JSONCONVERT2OBJECT_MEMEBER_REGISTER_RESERVERD_IMPLE(jsonTypeValue, names);
}
template <typename TClass, typename enable_if<!HasConverFunction<TClass>::has, int>::type = 0>
static inline bool Json2Object(TClass& aimObj, const Json::Value& jsonTypeValue)
{
return false;
}
成員變量匹配Key重命名
目前的實(shí)現(xiàn)均為將成員變量的名稱作為JSON串中的Key名稱,為靈活處理,再補(bǔ)充一個(gè)宏用于重新聲明結(jié)構(gòu)體成員變量中對(duì)應(yīng)到JSON串中的key,例如:
struct DemoObjct
{
bool boolValue;
int intValue;
std::string strValue;
JSONCONVERT2OBJECT_MEMEBER_REGISTER(boolValue, intValue, strValue)
/*重新聲明成員變量對(duì)應(yīng)到JSON串的key,注意順序一致*/
JSONCONVERT2OBJECT_MEMEBER_RENAME_REGISTER("bValue", "iValue", "sValue")
};
DemoObjct demoObj;
/*boolValue <--> bValue; intValue <--> iValue; ...*/
REQUIRE(Json2Object(demoObj, std::string("{\"bValue\":true, \"iValue\":1234, \"sValue\":\"demo object!\"}")));
REQUIRE(demoObj.boolValue == true);
REQUIRE(demoObj.intValue == 1234);
REQUIRE(demoObj.strValue == "demo object!");
Object2Json實(shí)現(xiàn)
上面提到為大多為實(shí)現(xiàn)Json2Object接口所提供的操作,從結(jié)構(gòu)體對(duì)象轉(zhuǎn)成Json也是類似的操作,這里就不再闡述,詳細(xì)可參考源碼。
亮點(diǎn)
- 簡(jiǎn)化C++對(duì)JSON數(shù)據(jù)的處理,屏蔽注意拆分處理JSON數(shù)據(jù)的操作;
- 提供簡(jiǎn)易接口,從結(jié)構(gòu)體到JSON串、JSON串轉(zhuǎn)結(jié)構(gòu)體切換自如
源碼
#include "json/json.h"
#include <string>
#include <vector>
#include <initializer_list>
#define JSONCONVERT2OBJECT_MEMEBER_REGISTER(...) \
bool JSONCONVERT2OBJECT_MEMEBER_REGISTER_RESERVERD_IMPLE(const Json::Value& jsonTypeValue, std::vector<std::string> &names) \
{ \
if(names.size() <= 0) { \
names = Member2KeyParseWithStr(#__VA_ARGS__); \
} \
return JsonParse(names, 0, jsonTypeValue, __VA_ARGS__); \
} \
bool OBJECTCONVERT2JSON_MEMEBER_REGISTER_RESERVERD_IMPLE(Json::Value& jsonTypeValue, std::vector<std::string> &names) const \
{ \
if(names.size() <= 0) { \
names = Member2KeyParseWithStr(#__VA_ARGS__); \
} \
return ParseJson(names, 0, jsonTypeValue, __VA_ARGS__); \
} \
#define JSONCONVERT2OBJECT_MEMEBER_RENAME_REGISTER(...) \
std::vector<std::string> JSONCONVERT2OBJECT_MEMEBER_RENAME_REGISTER_RESERVERD_IMPLE() const \
{ \
return Member2KeyParseWithMultiParam({ __VA_ARGS__ }); \
}
namespace JSON
{
template <bool, class TYPE = void>
struct enable_if
{
};
template <class TYPE>
struct enable_if<true, TYPE>
{
typedef TYPE type;
};
} //JSON
template <typename T>
struct HasConverFunction
{
template <typename TT>
static char func(decltype(&TT::JSONCONVERT2OBJECT_MEMEBER_REGISTER_RESERVERD_IMPLE)); //@1
template <typename TT>
static int func(...); //@2
const static bool has = (sizeof(func<T>(NULL)) == sizeof(char));
template <typename TT>
static char func2(decltype(&TT::JSONCONVERT2OBJECT_MEMEBER_RENAME_REGISTER_RESERVERD_IMPLE)); //@1
template <typename TT>
static int func2(...); //@2
const static bool has2 = (sizeof(func2<T>(NULL)) == sizeof(char));
};
static std::vector<std::string> Member2KeyParseWithMultiParam(std::initializer_list<std::string> il)
{
std::vector<std::string> result;
for (auto it = il.begin(); it != il.end(); it++) {
result.push_back(*it);
}
return result;
}
inline static std::string NormalStringTrim(std::string const& str)
{
static char const* whitespaceChars = "\n\r\t ";
std::string::size_type start = str.find_first_not_of(whitespaceChars);
std::string::size_type end = str.find_last_not_of(whitespaceChars);
return start != std::string::npos ? str.substr(start, 1 + end - start) : std::string();
}
inline static std::vector<std::string> NormalStringSplit(std::string str, char splitElem)
{
std::vector<std::string> strs;
std::string::size_type pos1, pos2;
pos2 = str.find(splitElem);
pos1 = 0;
while (std::string::npos != pos2) {
strs.push_back(str.substr(pos1, pos2 - pos1));
pos1 = pos2 + 1;
pos2 = str.find(splitElem, pos1);
}
strs.push_back(str.substr(pos1));
return strs;
}
static std::vector<std::string> Member2KeyParseWithStr(const std::string& values)
{
std::vector<std::string> result;
auto enumValues = NormalStringSplit(values, ',');
result.reserve(enumValues.size());
for (auto const& enumValue : enumValues) {
result.push_back(NormalStringTrim(enumValue));
}
return result;
}
//////////////////////////////////////////////////////////////////////////////
static bool Json2Object(bool& aimObj, const Json::Value& jsonTypeValue)
{
if (jsonTypeValue.isNull() || !jsonTypeValue.isBool()) {
return false;
} else {
aimObj = jsonTypeValue.asBool();
return true;
}
}
static bool Object2Json(Json::Value& jsonTypeValue, const std::string& key, bool value)
{
jsonTypeValue[key] = value;
return true;
}
static bool Json2Object(int& aimObj, const Json::Value& jsonTypeValue)
{
if (jsonTypeValue.isNull() || !jsonTypeValue.isInt()) {
return false;
} else {
aimObj = jsonTypeValue.asInt();
return true;
}
}
static bool Object2Json(Json::Value& jsonTypeValue, const std::string& key, const int& value)
{
jsonTypeValue[key] = value;
return true;
}
static bool Json2Object(unsigned int& aimObj, const Json::Value& jsonTypeValue)
{
if (jsonTypeValue.isNull() || !jsonTypeValue.isUInt()) {
return false;
} else {
aimObj = jsonTypeValue.asUInt();
return true;
}
}
static bool Object2Json(Json::Value& jsonTypeValue, const std::string& key, const unsigned int& value)
{
jsonTypeValue[key] = value;
return true;
}
static bool Json2Object(double& aimObj, const Json::Value& jsonTypeValue)
{
if (jsonTypeValue.isNull() || !jsonTypeValue.isDouble()) {
return false;
} else {
aimObj = jsonTypeValue.asDouble();
return true;
}
}
static bool Object2Json(Json::Value& jsonTypeValue, const std::string& key, const double& value)
{
jsonTypeValue[key] = value;
return true;
}
static bool Json2Object(std::string& aimObj, const Json::Value& jsonTypeValue)
{
if (jsonTypeValue.isNull() || !jsonTypeValue.isString()) {
return false;
} else {
aimObj = jsonTypeValue.asString();
return true;
}
}
static bool Object2Json(Json::Value& jsonTypeValue, const std::string& key, const std::string& value)
{
jsonTypeValue[key] = value;
return true;
}
template <typename TClass, typename JSON::enable_if<HasConverFunction<TClass>::has2, int>::type = 0>
static inline std::vector<std::string> PreGetCustomMemberNameIfExists(const TClass& aimObj)
{
return aimObj.JSONCONVERT2OBJECT_MEMEBER_RENAME_REGISTER_RESERVERD_IMPLE();
}
template <typename TClass, typename JSON::enable_if<!HasConverFunction<TClass>::has2, int>::type = 0>
static inline std::vector<std::string> PreGetCustomMemberNameIfExists(const TClass& aimObj)
{
return std::vector<std::string>();
}
template <typename TClass, typename JSON::enable_if<HasConverFunction<TClass>::has, int>::type = 0>
static inline bool Json2Object(TClass& aimObj, const Json::Value& jsonTypeValue)
{
std::vector<std::string> names = PreGetCustomMemberNameIfExists(aimObj);
return aimObj.JSONCONVERT2OBJECT_MEMEBER_REGISTER_RESERVERD_IMPLE(jsonTypeValue, names);
}
template <typename TClass, typename JSON::enable_if<!HasConverFunction<TClass>::has, int>::type = 0>
static inline bool Json2Object(TClass& aimObj, const Json::Value& jsonTypeValue)
{
return false;
}
template <typename T>
static bool Json2Object(std::vector<T>& aimObj, const Json::Value& jsonTypeValue)
{
if (jsonTypeValue.isNull() || !jsonTypeValue.isArray()) {
return false;
} else {
aimObj.clear();
bool result(true);
for (int i = 0; i < jsonTypeValue.size(); ++i) {
T item;
if (!Json2Object(item, jsonTypeValue[i])) {
result = false;
}
aimObj.push_back(item);
}
return result;
}
}
template <typename T>
static bool JsonParse(const std::vector<std::string>& names, int index, const Json::Value& jsonTypeValue, T& arg)
{
const auto key = names[index];
if (!jsonTypeValue.isMember(key) || Json2Object(arg, jsonTypeValue[key])) {
return true;
} else {
return false;
}
}
template <typename T, typename... Args>
static bool JsonParse(const std::vector<std::string>& names, int index, const Json::Value& jsonTypeValue, T& arg, Args&... args)
{
if (!JsonParse(names, index, jsonTypeValue, arg)) {
return false;
} else {
return JsonParse(names, index + 1, jsonTypeValue, args...);
}
}
/** Provider interface*/
template<typename TClass>
bool Json2Object(TClass& aimObj, const std::string& jsonTypeStr)
{
Json::Reader reader;
Json::Value root;
if (!reader.parse(jsonTypeStr, root) || root.isNull()) {
return false;
}
return Json2Object(aimObj, root);
}
static bool GetJsonRootObject(Json::Value& root, const std::string& jsonTypeStr)
{
Json::Reader reader;
if (!reader.parse(jsonTypeStr, root)) {
return false;
}
return true;
}
////////////////////////////////////////////////////////////////////////
template <typename TClass, typename JSON::enable_if<HasConverFunction<TClass>::has, int>::type = 0>
static inline bool Object2Json(Json::Value& jsonTypeOutValue, const std::string& key, const TClass& objValue)
{
std::vector<std::string> names = PreGetCustomMemberNameIfExists(objValue);
if (key.empty()) {
return objValue.OBJECTCONVERT2JSON_MEMEBER_REGISTER_RESERVERD_IMPLE(jsonTypeOutValue, names);
} else {
Json::Value jsonTypeNewValue;
const bool result = objValue.OBJECTCONVERT2JSON_MEMEBER_REGISTER_RESERVERD_IMPLE(jsonTypeNewValue, names);
if (result) {
jsonTypeOutValue[key] = jsonTypeNewValue;
}
return result;
}
}
template <typename TClass, typename JSON::enable_if<!HasConverFunction<TClass>::has, int>::type = 0>
static inline bool Object2Json(Json::Value& jsonTypeOutValue, const std::string& key, const TClass& objValue)
{
return false;
}
template <typename T>
static bool Object2Json(Json::Value& jsonTypeOutValue, const std::string& key, const std::vector<T>& objValue)
{
bool result(true);
for (int i = 0; i < objValue.size(); ++i) {
Json::Value item;
if (!Object2Json(item, "", objValue[i])) {
result = false;
} else {
if (key.empty()) jsonTypeOutValue.append(item);
else jsonTypeOutValue[key].append(item);
}
}
return result;
}
template <typename T>
static bool ParseJson(const std::vector<std::string>& names, int index, Json::Value& jsonTypeValue, const T& arg)
{
if (names.size() > index) {
const std::string key = names[index];
return Object2Json(jsonTypeValue, key, arg);
} else {
return false;
}
}
template <typename T, typename... Args>
static bool ParseJson(const std::vector<std::string>& names, int index, Json::Value& jsonTypeValue, T& arg, Args&... args)
{
if (names.size() - (index + 0) != 1 + sizeof...(Args)) {
return false;
}
const std::string key = names[index];
Object2Json(jsonTypeValue, key, arg);
return ParseJson(names, index + 1, jsonTypeValue, args...);
}
/** Provider interface*/
template<typename T>
bool Object2Json(std::string& jsonTypeStr, const T& obj)
{
//std::function<Json::Value()>placehoder = [&]()->Json::Value { return Json::Value(); };
//auto func = [&](std::function<Json::Value()>f) { return f(); };
//Json::Value val = func(placehoder);
Json::StyledWriter writer;
Json::Value root;
const bool result = Object2Json(root, "", obj);
if (result) {
jsonTypeStr = writer.write(root);
}
return result;
}
參考文檔
總結(jié)
到此這篇關(guān)于C++對(duì)Json數(shù)據(jù)的友好處理的文章就介紹到這了,更多相關(guān)C++對(duì)Json數(shù)據(jù)的處理內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
QT+OpenGL實(shí)現(xiàn)簡(jiǎn)單圖形的繪制
這篇文章主要為大家詳細(xì)介紹了如何利用QT和OpenGL實(shí)現(xiàn)簡(jiǎn)單圖形的繪制,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,需要的可以參考一下2022-12-12
詳解C++編程中的輸入輸相關(guān)的類和對(duì)象
這篇文章主要介紹了詳解C++編程中的輸入輸相關(guān)的類和對(duì)象,是C++入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-09-09
C++實(shí)現(xiàn)LeetCode(132.拆分回文串之二)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(132.拆分回文串之二),本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07
C語(yǔ)言重難點(diǎn)之內(nèi)存對(duì)齊和位段
這篇文章主要介紹了C語(yǔ)言重難點(diǎn)之內(nèi)存對(duì)齊和位段,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05
VC++實(shí)現(xiàn)選擇排序算法簡(jiǎn)單示例
這篇文章主要介紹了VC++實(shí)現(xiàn)選擇排序算法簡(jiǎn)單示例,代碼簡(jiǎn)潔易懂,有助于讀者對(duì)數(shù)據(jù)結(jié)構(gòu)與算法的學(xué)習(xí),需要的朋友可以參考下2014-08-08
C語(yǔ)言讀取文件流的相關(guān)函數(shù)用法簡(jiǎn)介
這篇文章主要介紹了C語(yǔ)言讀取文件流的相關(guān)函數(shù)用法簡(jiǎn)介,包括fread()函數(shù)和feof()函數(shù)的使用,需要的朋友可以參考下2015-08-08

