|
@@ -9,10 +9,19 @@
|
|
|
#define JSONCONVERT2OBJECT_MEMEBER_REGISTER(...) \
|
|
|
bool JSONCONVERT2OBJECT_MEMEBER_REGISTER_RESERVERD_IMPLE(const Json::Value& jsonTypeValue, std::vector<std::string> &names) \
|
|
|
{ \
|
|
|
- if(names.size() <= 0) \
|
|
|
+ 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 \
|
|
@@ -90,6 +99,7 @@ static std::vector<std::string> Member2KeyParseWithStr(const std::string& values
|
|
|
auto enumValues = NormalStringSplit(values, ',');
|
|
|
result.reserve(enumValues.size());
|
|
|
for (auto const& enumValue : enumValues) {
|
|
|
+ /** 修复喜欢加空格或代码格式化导致的问题 [Gifur@2022122]*/
|
|
|
result.push_back(NormalStringTrim(enumValue));
|
|
|
}
|
|
|
return result;
|
|
@@ -106,6 +116,12 @@ static bool Json2Object(bool& aimObj, const Json::Value& jsonTypeValue)
|
|
|
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()) {
|
|
@@ -115,6 +131,12 @@ static bool Json2Object(int& aimObj, const Json::Value& jsonTypeValue)
|
|
|
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()) {
|
|
@@ -124,6 +146,12 @@ static bool Json2Object(unsigned int& aimObj, const Json::Value& jsonTypeValue)
|
|
|
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()) {
|
|
@@ -133,6 +161,12 @@ static bool Json2Object(double& aimObj, const Json::Value& jsonTypeValue)
|
|
|
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()) {
|
|
@@ -142,16 +176,20 @@ static bool Json2Object(std::string& aimObj, const Json::Value& jsonTypeValue)
|
|
|
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 enable_if<HasConverFunction<TClass>::has2, int>::type = 0>
|
|
|
-static inline std::vector<std::string> PreGetCustomMemberNameIfExists(TClass& aimObj)
|
|
|
+static inline std::vector<std::string> PreGetCustomMemberNameIfExists(const TClass& aimObj)
|
|
|
{
|
|
|
return aimObj.JSONCONVERT2OBJECT_MEMEBER_RENAME_REGISTER_RESERVERD_IMPLE();
|
|
|
}
|
|
|
|
|
|
template <typename TClass, typename enable_if<!HasConverFunction<TClass>::has2, int>::type = 0>
|
|
|
-static inline std::vector<std::string> PreGetCustomMemberNameIfExists(TClass& aimObj)
|
|
|
+static inline std::vector<std::string> PreGetCustomMemberNameIfExists(const TClass& aimObj)
|
|
|
{
|
|
|
return std::vector<std::string>();
|
|
|
}
|
|
@@ -228,12 +266,59 @@ bool Json2Object(TClass& aimObj, const std::string& jsonTypeStr)
|
|
|
}
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
+template <typename TClass, typename 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);
|
|
|
+ return objValue.OBJECTCONVERT2JSON_MEMEBER_REGISTER_RESERVERD_IMPLE(jsonTypeOutValue, names);
|
|
|
+}
|
|
|
+
|
|
|
+template <typename TClass, typename 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 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];
|
|
|
+ ///**TODO(Gifur@1/22/2022): 需要扩展其他类型实现,这里不直接用 JsonCPP 的内容,要考虑到其他自定义结构体 */
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+ /** 通过低柜调用实现 [Gifur@2022122]*/
|
|
|
+ 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)
|
|
|
{
|
|
|
- return false;
|
|
|
+ //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;
|
|
|
}
|
|
|
|
|
|
#define __func_1(func,member) func(member);
|