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, features, parseOnly ); if ( exitCode == 0 && !parseOnly ) { std::string rewrite; exitCode = rewriteValueTree( rewritePath, root, rewrite ); if ( exitCode == 0 ) { Json::Value rewriteRoot; //分析和保存树值 exitCode = parseAndSaveValueTree( rewrite, rewriteActualPath, "rewrite", rewriteRoot, features, 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, ValueTest, isObject ); JSONTEST_REGISTER_FIXTURE( runner, ValueTest, isArray ); JSONTEST_REGISTER_FIXTURE( runner, ValueTest, isBool ); JSONTEST_REGISTER_FIXTURE( runner, ValueTest, isInt ); JSONTEST_REGISTER_FIXTURE( runner, ValueTest, isUInt ); JSONTEST_REGISTER_FIXTURE( runner, ValueTest, isDouble ); JSONTEST_REGISTER_FIXTURE( runner, ValueTest, isString ); JSONTEST_REGISTER_FIXTURE( runner, ValueTest, isNull ); JSONTEST_REGISTER_FIXTURE( runner, ValueTest, isNull ); JSONTEST_REGISTER_FIXTURE( runner, ValueTest, accessArray ); JSONTEST_REGISTER_FIXTURE( runner, ValueTest, asFloat ); JSONTEST_REGISTER_FIXTURE( runner, ValueTest, compareNull ); JSONTEST_REGISTER_FIXTURE( runner, ValueTest, compareInt ); JSONTEST_REGISTER_FIXTURE( runner, ValueTest, compareUInt ); JSONTEST_REGISTER_FIXTURE( runner, ValueTest, compareDouble ); JSONTEST_REGISTER_FIXTURE( runner, ValueTest, compareString ); JSONTEST_REGISTER_FIXTURE( runner, ValueTest, compareBoolean ); JSONTEST_REGISTER_FIXTURE( runner, ValueTest, compareArray ); JSONTEST_REGISTER_FIXTURE( runner, ValueTest, compareObject ); JSONTEST_REGISTER_FIXTURE( runner, ValueTest, compareType ); return runner.runCommandLine( argc, argv );
源码包下载