From: Baptiste L. <bl...@us...> - 2005-07-27 22:28:18
|
Update of /cvsroot/jsoncpp/jsoncpp/src/jsontest In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv27056/src/jsontest Modified Files: main.cpp Log Message: * added FastWriter implementation and updated test (read the written input and check if correct). Index: main.cpp =================================================================== RCS file: /cvsroot/jsoncpp/jsoncpp/src/jsontest/main.cpp,v retrieving revision 1.1.1.1 retrieving revision 1.2 diff -C2 -d -r1.1.1.1 -r1.2 *** main.cpp 27 Jul 2005 07:09:23 -0000 1.1.1.1 --- main.cpp 27 Jul 2005 22:28:08 -0000 1.2 *************** *** 24,52 **** static void ! printValueTree( Json::Value &value, const std::string &path = "." ) { switch ( value.type() ) { case Json::nullValue: ! printf( "%s=null\n", path.c_str() ); break; case Json::intValue: ! printf( "%s=%d\n", path.c_str(), value.asInt() ); break; case Json::uintValue: ! printf( "%s=%u\n", path.c_str(), value.asUInt() ); break; case Json::realValue: ! printf( "%s=%.16g\n", path.c_str(), value.asDouble() ); break; case Json::stringValue: ! printf( "%s=\"%s\"\n", path.c_str(), value.asString().c_str() ); break; case Json::booleanValue: ! printf( "%s=%s\n", path.c_str(), value.asBool() ? "true" : "false" ); break; case Json::arrayValue: { ! printf( "%s=[]\n", path.c_str() ); int size = value.size(); for ( int index =0; index < size; ++index ) --- 24,52 ---- static void ! printValueTree( FILE *fout, Json::Value &value, const std::string &path = "." ) { switch ( value.type() ) { case Json::nullValue: ! fprintf( fout, "%s=null\n", path.c_str() ); break; case Json::intValue: ! fprintf( fout, "%s=%d\n", path.c_str(), value.asInt() ); break; case Json::uintValue: ! fprintf( fout, "%s=%u\n", path.c_str(), value.asUInt() ); break; case Json::realValue: ! fprintf( fout, "%s=%.16g\n", path.c_str(), value.asDouble() ); break; case Json::stringValue: ! fprintf( fout, "%s=\"%s\"\n", path.c_str(), value.asString().c_str() ); break; case Json::booleanValue: ! fprintf( fout, "%s=%s\n", path.c_str(), value.asBool() ? "true" : "false" ); break; case Json::arrayValue: { ! fprintf( fout, "%s=[]\n", path.c_str() ); int size = value.size(); for ( int index =0; index < size; ++index ) *************** *** 54,58 **** static char buffer[16]; sprintf( buffer, "[%d]", index ); ! printValueTree( value[index], path + buffer ); } } --- 54,58 ---- static char buffer[16]; sprintf( buffer, "[%d]", index ); ! printValueTree( fout, value[index], path + buffer ); } } *************** *** 60,64 **** case Json::objectValue: { ! printf( "%s={}\n", path.c_str() ); Json::Value::Members members( value.getMemberNames() ); std::string suffix = *(path.end()-1) == '.' ? "" : "."; --- 60,64 ---- case Json::objectValue: { ! fprintf( fout, "%s={}\n", path.c_str() ); Json::Value::Members members( value.getMemberNames() ); std::string suffix = *(path.end()-1) == '.' ? "" : "."; *************** *** 68,72 **** { const std::string &name = *it; ! printValueTree( value[name], path + suffix + name ); } } --- 68,72 ---- { const std::string &name = *it; ! printValueTree( fout, value[name], path + suffix + name ); } } *************** *** 78,81 **** --- 78,140 ---- + static int + parseAndSaveValueTree( const std::string &input, + const std::string &actual, + const std::string &kind, + Json::Value &root ) + { + Json::Reader reader; + bool parsingSuccessful = reader.parse( input, root ); + if ( !parsingSuccessful ) + { + printf( "Failed to parse %s file: \n%s\n", + kind.c_str(), + reader.getFormatedErrorMessages().c_str() ); + return 1; + } + + FILE *factual = fopen( actual.c_str(), "wt" ); + if ( !factual ) + { + printf( "Failed to create %s actual file.\n", kind.c_str() ); + return 2; + } + printValueTree( factual, root ); + fclose( factual ); + return 0; + } + + + static int + rewriteValueTree( const std::string &rewritePath, + const Json::Value &root, + std::string &rewrite ) + { + Json::FastWriter writer; + rewrite = writer.write( root ); + FILE *fout = fopen( rewritePath.c_str(), "wt" ); + if ( !fout ) + { + printf( "Failed to create rewrite file: %s\n", rewritePath.c_str() ); + return 2; + } + fprintf( fout, "%s\n", rewrite.c_str() ); + fclose( fout ); + return 0; + } + + + static std::string + removeSuffix( const std::string &path, + const std::string &extension ) + { + if ( extension.length() >= path.length() ) + return std::string(""); + std::string suffix = path.substr( path.length() - extension.length() ); + if ( suffix != extension ) + return std::string(""); + return path.substr( 0, path.length() - extension.length() ); + } + int main( int argc, const char *argv[] ) { *************** *** 83,87 **** { printf( "Usage: %s input-json-file", argv[0] ); ! return 2; } --- 142,146 ---- { printf( "Usage: %s input-json-file", argv[0] ); ! return 3; } *************** *** 93,104 **** } ! Json::Reader reader; Json::Value root; ! bool parsingSuccessful = reader.parse( input, root ); ! if ( parsingSuccessful ) ! printValueTree( root ); ! else ! printf( "%s\n", reader.getFormatedErrorMessages().c_str() ); ! return parsingSuccessful ? 0 : 1; } \ No newline at end of file --- 152,179 ---- } ! std::string basePath = removeSuffix( argv[1], ".json" ); ! if ( basePath.empty() ) ! { ! printf( "Bad input path. Path does not end with '.expected':\n%s\n", argv[1] ); ! return 3; ! } ! ! std::string actualPath = basePath + ".actual"; ! std::string rewritePath = basePath + ".rewrite"; ! std::string rewriteActualPath = basePath + ".actual-rewrite"; ! Json::Value root; ! int exitCode = parseAndSaveValueTree( input, actualPath, "input", root ); ! if ( exitCode == 0 ) ! { ! std::string rewrite; ! exitCode = rewriteValueTree( rewritePath, root, rewrite ); ! if ( exitCode == 0 ) ! { ! Json::Value rewriteRoot; ! exitCode = parseAndSaveValueTree( rewrite, rewriteActualPath, "rewrite", rewriteRoot ); ! } ! } ! return exitCode; } \ No newline at end of file |