#ifndef _SP_UTILITY_JSON_CONVERT_HELPER_ #define _SP_UTILITY_JSON_CONVERT_HELPER_ #include "json/json.h" #include #include #include #define JSONCONVERT2OBJECT_MEMEBER_REGISTER(...) \ bool JSONCONVERT2OBJECT_MEMEBER_REGISTER_RESERVERD_IMPLE(const Json::Value& jsonTypeValue, std::vector &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 &names) const \ { \ if(names.size() <= 0) { \ names = Member2KeyParseWithStr(#__VA_ARGS__); \ } \ return ParseJson(names, 0, jsonTypeValue, __VA_ARGS__); \ } /* std::string GetOutputString() const\ { \ std::ostringstream str; \ std::vector names = Member2KeyParseWithStr(#__VA_ARGS__); \ for(auto it=names.begin(); it!=names.end(); ++it) { if(it != names.begin()) { str << " | "; } str << *it; } \ if (names.size() > 0) { str << "\r\n"; } \ DbgPrint(str, __VA_ARGS__); \ return str.str(); \ } */ #define JSONCONVERT2OBJECT_MEMEBER_RENAME_REGISTER(...) \ std::vector JSONCONVERT2OBJECT_MEMEBER_RENAME_REGISTER_RESERVERD_IMPLE() const \ { \ return Member2KeyParseWithMultiParam({ __VA_ARGS__ }); \ } namespace SP { namespace Utility { namespace JSON { template struct enable_if { }; template struct enable_if { typedef TYPE type; }; } //JSON } //Utility } //SP template struct HasConverFunction { template static char func(decltype(&TT::JSONCONVERT2OBJECT_MEMEBER_REGISTER_RESERVERD_IMPLE)); //@1 template static int func(...); //@2 /* * 如果类型T没有 JSONCONVERT2OBJECT_MEMEBER_REGISTER_RESERVERD_IMPLE 方法, * func(NULL) 匹配 @1 时会产生错误,由于 SFINAE 准则,只能匹配@2 * 的func,此时返回值 4 个字节,has 变量为 false,反之,has 变量为 true */ const static bool has = (sizeof(func(NULL)) == sizeof(char)); template static char func2(decltype(&TT::JSONCONVERT2OBJECT_MEMEBER_RENAME_REGISTER_RESERVERD_IMPLE)); //@1 template static int func2(...); //@2 const static bool has2 = (sizeof(func2(NULL)) == sizeof(char)); }; static std::vector Member2KeyParseWithMultiParam(std::initializer_list il) { std::vector 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 NormalStringSplit(std::string str, char splitElem) { std::vector 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 Member2KeyParseWithStr(const std::string& values) { std::vector result; auto enumValues = NormalStringSplit(values, ','); result.reserve(enumValues.size()); for (auto const& enumValue : enumValues) { /** 修复喜欢加空格或代码格式化导致的问题 [Gifur@2022122]*/ result.push_back(NormalStringTrim(enumValue)); } return result; } ////////////////////////////////////////////////////////////////////////////// static bool Json2Object(bool& aimObj, const Json::Value& jsonTypeValue) { if (jsonTypeValue.isNull()) { aimObj = false; return true; } 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()) { aimObj = 0; return true; } 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()) { aimObj = 0; return true; } 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()) { aimObj = 0.0; return true; } 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()) { aimObj.clear(); return true; } 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 ::has2, int>::type = 0> static inline std::vector PreGetCustomMemberNameIfExists(const TClass& aimObj) { return aimObj.JSONCONVERT2OBJECT_MEMEBER_RENAME_REGISTER_RESERVERD_IMPLE(); } template ::has2, int>::type = 0> static inline std::vector PreGetCustomMemberNameIfExists(const TClass& aimObj) { return std::vector(); } template ::has, int>::type = 0> static inline bool Json2Object(TClass& aimObj, const Json::Value& jsonTypeValue) { std::vector names = PreGetCustomMemberNameIfExists(aimObj); return aimObj.JSONCONVERT2OBJECT_MEMEBER_REGISTER_RESERVERD_IMPLE(jsonTypeValue, names); } template ::has, int>::type = 0> static inline bool Json2Object(TClass& aimObj, const Json::Value& jsonTypeValue) { return false; } template static bool Json2Object(std::vector& 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 static bool JsonParse(const std::vector& 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 static bool JsonParse(const std::vector& 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 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 ::has, int>::type = 0> static inline bool Object2Json(Json::Value& jsonTypeOutValue, const std::string& key, const TClass& objValue) { std::vector 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 ::has, int>::type = 0> static inline bool Object2Json(Json::Value& jsonTypeOutValue, const std::string& key, const TClass& objValue) { return false; } template static bool Object2Json(Json::Value& jsonTypeOutValue, const std::string& key, const std::vector& 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 static bool ParseJson(const std::vector& names, int index, Json::Value& jsonTypeValue, const T& arg) { if (names.size() > index) { const std::string key = names[index]; ///**TODO(Gifur@1/22/2022): 需要扩展其他类型实现,这里不直接用 JsonCPP 的内容,要考虑到其他自定义结构体 */ return Object2Json(jsonTypeValue, key, arg); } else { return false; } } template static bool ParseJson(const std::vector& names, int index, Json::Value& jsonTypeValue, T& arg, Args&... args) { if (names.size() - (index + 0) != 1 + sizeof...(Args)) { return false; } /** 通过低柜调用实现 [Gifur@2022122]*/ const std::string key = names[index]; Object2Json(jsonTypeValue, key, arg); return ParseJson(names, index + 1, jsonTypeValue, args...); } /** Provider interface*/ template bool Object2Json(std::string& jsonTypeStr, const T& obj) { //std::functionplacehoder = [&]()->Json::Value { return Json::Value(); }; //auto func = [&](std::functionf) { 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; } template std::ostringstream& DbgPrint(std::ostringstream& os, T& t) { return os << t; } template std::ostringstream& DbgPrint(std::ostringstream& os, T& t, Args&... rest) { os << t << ", "; return DbgPrint(os, rest...); } #define __func_1(func,member) func(member); #define __func_2(func,member,...) __func_1(func,member) __func_1(func,__VA_ARGS__) #define __func_3(func,member,...) __func_1(func,member) __func_2(func,__VA_ARGS__) #define __func_4(func,member,...) __func_1(func,member) __func_3(func,__VA_ARGS__) #define __func_5(func,member,...) __func_1(func,member) __func_4(func,__VA_ARGS__) //eg: COUNT(a,b,c) === 3 #define COUNT(...) __count__(0, ##__VA_ARGS__, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0) #define __count__(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, N, ...) N #define __macro_cat__(a,b) a##b #define MACRO_CAT(a,b) __macro_cat__(a,b) #define FOR_EACH(func,...) \ MACRO_CAT(__func_,COUNT(__VA_ARGS__))(func, __VA_ARGS__) #define JSON2OBJECT_EACH_FILED__(field) \ if(!::Json2Object(field, jsonTypeValue[#field])){ \ result = false; \ } #endif //_SP_UTILITY_JSON_CONVERT_HELPER_