#define CATCH_CONFIG_MAIN #include #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"); }