123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306 |
- #define CATCH_CONFIG_MAIN
- #include <catch2.hpp>
- #include "JsonConvertHelper.hpp"
- 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, \"intValue\":1234, \"strValue\":\"demo object!\"}")));
- REQUIRE(demoObj.boolValue == true);
- REQUIRE(demoObj.intValue == 1234);
- 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("从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, \"intValue\":1234, \"strValue\":\"demo object!\"}")));
- REQUIRE(demoObj.boolValue == true);
- REQUIRE(demoObj.intValue == 1234);
- REQUIRE(demoObj.strValue == "demo object!");
- }
- TEST_CASE("从JSON字符串中获取结构体内容,支持重命名", "[json]")
- {
- struct DemoObjct
- {
- bool boolValue;
- int intValue;
- std::string strValue;
- JSONCONVERT2OBJECT_MEMEBER_REGISTER(boolValue, intValue, strValue)
- JSONCONVERT2OBJECT_MEMEBER_RENAME_REGISTER("bValue", "iValue", "sValue")
- };
- DemoObjct demoObj;
- 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!");
- }
- TEST_CASE("单结构体转换为字符串,有做字段转换声明", "[json]")
- {
- struct DemoObjct
- {
- bool boolValue;
- int intValue;
- std::string strValue;
- JSONCONVERT2OBJECT_MEMEBER_REGISTER(boolValue, intValue, strValue)
- JSONCONVERT2OBJECT_MEMEBER_RENAME_REGISTER("bValue", "iValue", "sValue")
- };
- DemoObjct demoObj;
- 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!");
- std::string rawString;
- REQUIRE(Object2Json(rawString, demoObj));
- std::cout << "returned json format: " << rawString << std::endl;
- }
- TEST_CASE("单结构体转换为字符串", "[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, \"intValue\":1234, \"strValue\":\"demo object!\"}")));
- REQUIRE(demoObj.boolValue == true);
- REQUIRE(demoObj.intValue == 1234);
- REQUIRE(demoObj.strValue == "demo object!");
- std::string rawString;
- REQUIRE(Object2Json(rawString, demoObj));
- std::cout << "returned json format: " << rawString << std::endl;
- }
- 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;
- DemoObjct demoObj2;
- REQUIRE(Json2Object(demoObj2, std::string(rawString)));
- REQUIRE(demoObj2.boolValue == true);
- REQUIRE(demoObj2.intValue == 1234);
- REQUIRE(demoObj2.strValue == "demo object!");
- REQUIRE(demoObj2.parent.boolValue == true);
- REQUIRE(demoObj2.parent.intValue == 1234);
- REQUIRE(demoObj2.parent.strValue == "demo object!");
- }
- TEST_CASE("从Json串中解析结构体数组", "[json]")
- {
- struct DemoChildrenObject
- {
- bool boolValue;
- int intValue;
- std::string strValue;
- JSONCONVERT2OBJECT_MEMEBER_REGISTER(boolValue, intValue, strValue)
- };
- struct DemoObjct
- {
- bool boolValue;
- int intValue;
- std::string strValue;
- std::vector< DemoChildrenObject> children;
- JSONCONVERT2OBJECT_MEMEBER_REGISTER(boolValue, intValue, strValue, children)
- };
- DemoObjct demoObj;
- REQUIRE(Json2Object(demoObj, std::string("{\"boolValue\":true, \"intValue\":1234, \"strValue\":\"demo object!\", \"children\" : [ {\"boolValue\":true, \"intValue\":3333, \"strValue\":\"demo child1 object!\"},{\"boolValue\":false, \"intValue\":44444, \"strValue\":\"demo child2 object!\"} ] }")));
- REQUIRE(demoObj.boolValue == true);
- REQUIRE(demoObj.intValue == 1234);
- REQUIRE(demoObj.strValue == "demo object!");
- REQUIRE(demoObj.children.size() == 2);
- REQUIRE(demoObj.children[0].boolValue == true);
- REQUIRE(demoObj.children[0].intValue == 3333);
- REQUIRE(demoObj.children[0].strValue == "demo child1 object!");
- REQUIRE(demoObj.children[1].boolValue == false);
- REQUIRE(demoObj.children[1].intValue == 44444);
- REQUIRE(demoObj.children[1].strValue == "demo child2 object!");
- }
- TEST_CASE("解析结构体数组到JSON串", "[json]")
- {
- struct DemoChildrenObject
- {
- bool boolValue;
- int intValue;
- std::string strValue;
- JSONCONVERT2OBJECT_MEMEBER_REGISTER(boolValue, intValue, strValue)
- };
- struct DemoObjct
- {
- bool boolValue;
- int intValue;
- std::string strValue;
- std::vector< DemoChildrenObject> children;
- JSONCONVERT2OBJECT_MEMEBER_REGISTER(boolValue, intValue, strValue, children)
- };
- DemoObjct demoObj;
- 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);
- std::string jsonStr;
- REQUIRE(Object2Json(jsonStr, demoObj));
- std::cout << "returned json format: " << jsonStr << std::endl;
- DemoObjct demoObj2;
- REQUIRE(Json2Object(demoObj2, jsonStr));
- 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");
- }
|