|
|
@@ -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;
|
|
|
+}
|