JSON(JavaScript对象符号)是一种轻量级的数据交换格式。它可以是整数,实数,字符串,值的有序序列,名称/值对的集合。
是一个简单的API来操纵 JSON值,处理序列化和反序列化到字符串。在反序列化/序列化的步骤,它也可以保留现有的注释,一种方便的格式来存储用户输入的文件。
工程如图:
测试效果如图:
测试代码A:
int main( int argc,const char *argv[] ) { std::string path = "C:\\Users\\Administrator\\Desktop\\JsonCpp\\jsoncpp-src-0.6.0-rc2\\jsoncpp-src-0.6.0-rc2\\test\\data\\test_complex_01.json"; Json::Features features; bool parSEOnly; //模拟测试参数 argc = 2; argv[0] = ""; argv[1] = "C:\\Users\\Administrator\\Desktop\\JsonCpp\\jsoncpp-src-0.6.0-rc2\\jsoncpp-src-0.6.0-rc2\\test\\data\\test_complex_01.json"; //分析命令行参数 int exitCode = parseCommandLine( argc,argv,features,path,parSEOnly ); if ( exitCode != 0 ) { return exitCode; } try { //读取json文件 // readInputTestFile 返回 "{ //"count" : 1234,//"name" : { "aka" : "T.E.S.T.","id" : 123987 },//"attribute" : [ // "random",// "short",// "bold",// 12,// { "height" : 7,"width" : 64 } // ],//"test": { "1" : // { "2" : // { "3" : { "coord" : [ 1,2] } // } // } //} //} std::string input = readInputTestFile( path.c_str() ); if ( input.empty() ) { printf( "Failed to read input or empty input: %s\n",path.c_str() ); return 3; } std::string basePath = removeSuffix( argv[1],".json" ); if ( !parSEOnly && basePath.empty() ) { printf( "Bad input path. Path does not end with '.expected':\n%s\n",path.c_str() ); return 3; } std::string actualPath = basePath + ".actual"; std::string rewritePath = basePath + ".rewrite"; std::string rewriteActualPath = basePath + ".actual-rewrite"; Json::Value root; //分析和保存树值 exitCode = parseAndSaveValueTree( input,actualPath,"input",root,parSEOnly ); if ( exitCode == 0 && !parSEOnly ) { std::string rewrite; exitCode = rewriteValueTree( rewritePath,rewrite ); if ( exitCode == 0 ) { Json::Value rewriteRoot; //分析和保存树值 exitCode = parseAndSaveValueTree( rewrite,rewriteActualPath,"rewrite",rewriteRoot,parSEOnly ); } } } catch ( const std::exception &e ) { printf( "Unhandled exception:\n%s\n",e.what() ); exitCode = 1; } return exitCode; }
测试代码B:
JsonTest::Runner runner; JSONTEST_REGISTER_FIXTURE( runner,ValueTest,size ); JSONTEST_REGISTER_FIXTURE( runner,isObject ); JSONTEST_REGISTER_FIXTURE( runner,isArray ); JSONTEST_REGISTER_FIXTURE( runner,isBool ); JSONTEST_REGISTER_FIXTURE( runner,isInt ); JSONTEST_REGISTER_FIXTURE( runner,isUInt ); JSONTEST_REGISTER_FIXTURE( runner,isDouble ); JSONTEST_REGISTER_FIXTURE( runner,isString ); JSONTEST_REGISTER_FIXTURE( runner,isNull ); JSONTEST_REGISTER_FIXTURE( runner,accessArray ); JSONTEST_REGISTER_FIXTURE( runner,asFloat ); JSONTEST_REGISTER_FIXTURE( runner,compareNull ); JSONTEST_REGISTER_FIXTURE( runner,compareInt ); JSONTEST_REGISTER_FIXTURE( runner,compareUInt ); JSONTEST_REGISTER_FIXTURE( runner,compareDouble ); JSONTEST_REGISTER_FIXTURE( runner,compareString ); JSONTEST_REGISTER_FIXTURE( runner,compareBoolean ); JSONTEST_REGISTER_FIXTURE( runner,compareArray ); JSONTEST_REGISTER_FIXTURE( runner,compareObject ); JSONTEST_REGISTER_FIXTURE( runner,compareType ); return runner.runCommandLine( argc,argv );
学习的目标是成熟!~~~ 原文链接:https://www.f2er.com/json/290606.html