Sfoglia il codice sorgente

#IQBX #comment [BugFix][LibRestfulFunc] Json转换的一个接口分配

gifur 3 anni fa
parent
commit
2621a33a23

+ 10 - 1
Other/libRestfulFunc/JsonConvertHelper.hpp

@@ -270,7 +270,16 @@ template <typename TClass, typename enable_if<HasConverFunction<TClass>::has, in
 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);
+    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 enable_if<!HasConverFunction<TClass>::has, int>::type = 0>

+ 89 - 2
Other/libRestfulFunc/test/testJsonConvert.cpp

@@ -20,6 +20,24 @@ TEST_CASE("test normal class", "[json]")
     REQUIRE(demoObj.strValue == "demo object!");
 }
 
+TEST_CASE("JSON字段数据不对应", "[json]")
+{
+    struct DemoObjct
+    {
+        bool boolValue;
+        int intValue;
+        std::string strValue;
+
+        JSONCONVERT2OBJECT_MEMEBER_REGISTER(boolValue, intValue, strValue)
+    };
+
+    DemoObjct demoObj;
+    REQUIRE(Json2Object(demoObj, std::string("{\"boolValue\":true, \"strValue\":\"demo object!\", \"intValue\":1234}")));
+    REQUIRE(demoObj.boolValue == true);
+    REQUIRE(demoObj.intValue == 1234);
+    REQUIRE(demoObj.strValue == "demo object!");
+}
+
 TEST_CASE("test normal space class", "[json]")
 {
     struct DemoObjct
@@ -58,7 +76,7 @@ TEST_CASE("test rename class", "[json]")
 }
 
 
-TEST_CASE("test objct 2 json class", "[json]")
+TEST_CASE("单结构体转换为字符串,有做字段转换声明", "[json]")
 {
     struct DemoObjct
     {
@@ -81,7 +99,7 @@ TEST_CASE("test objct 2 json class", "[json]")
     std::cout << "returned json format: " << rawString << std::endl;
 }
 
-TEST_CASE("test objct 2 json  2 class", "[json]")
+TEST_CASE("单结构体转换为字符串", "[json]")
 {
     struct DemoObjct
     {
@@ -104,4 +122,73 @@ TEST_CASE("test objct 2 json  2 class", "[json]")
 }
 
 
+TEST_CASE("从JSON中解析中嵌套结构体", "[json]")
+{
+    struct DemoParentObjct
+    {
+        bool boolValue;
+        int intValue;
+        std::string strValue;
+
+        JSONCONVERT2OBJECT_MEMEBER_REGISTER(boolValue, intValue, strValue)
+    };
 
+    struct DemoObjct
+    {
+        bool boolValue;
+        int intValue;
+        std::string strValue;
+
+        DemoParentObjct parent;
+
+        JSONCONVERT2OBJECT_MEMEBER_REGISTER(boolValue, intValue, strValue, parent)
+    };
+
+    DemoObjct demoObj;
+    REQUIRE(Json2Object(demoObj, std::string("{\"boolValue\":true, \"intValue\":1234, \"strValue\":\"demo object!\", \"parent\"  : {\"boolValue\":true, \"intValue\":1234, \"strValue\":\"demo object!\"}}")));
+    REQUIRE(demoObj.boolValue == true);
+    REQUIRE(demoObj.intValue == 1234);
+    REQUIRE(demoObj.strValue == "demo object!");
+
+    REQUIRE(demoObj.parent.boolValue == true);
+    REQUIRE(demoObj.parent.intValue == 1234);
+    REQUIRE(demoObj.parent.strValue == "demo object!");
+}
+
+TEST_CASE("嵌套结构体转换转换为JSON字符串", "[json]")
+{
+    struct DemoParentObjct
+    {
+        bool boolValue;
+        int intValue;
+        std::string strValue;
+        double dValue;
+
+        JSONCONVERT2OBJECT_MEMEBER_REGISTER(boolValue, intValue, strValue, dValue)
+    };
+
+    struct DemoObjct
+    {
+        bool boolValue;
+        int intValue;
+        std::string strValue;
+
+        DemoParentObjct parent;
+
+        JSONCONVERT2OBJECT_MEMEBER_REGISTER(boolValue, intValue, strValue, parent)
+    };
+
+    DemoObjct demoObj;
+    REQUIRE(Json2Object(demoObj, std::string("{\"boolValue\":true, \"intValue\":1234, \"strValue\":\"demo object!\", \"parent\"  : {\"dValue\":1.45, \"boolValue\":true, \"intValue\":1234, \"strValue\":\"demo object!\"}}")));
+    REQUIRE(demoObj.boolValue == true);
+    REQUIRE(demoObj.intValue == 1234);
+    REQUIRE(demoObj.strValue == "demo object!");
+
+    REQUIRE(demoObj.parent.boolValue == true);
+    REQUIRE(demoObj.parent.intValue == 1234);
+    REQUIRE(demoObj.parent.strValue == "demo object!");
+
+    std::string rawString;
+    REQUIRE(Object2Json(rawString, demoObj));
+    std::cout << "returned json format: " << rawString << std::endl;
+}