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=%s\n", path.c_str(), Json::valueToString( value.asLargestInt() ).c_str() ); break; case Json::uintValue: fprintf( fout, "%s=%s\n", path.c_str(), Json::valueToString( value.asLargestUInt() ).c_str() ); break; case Json::realValue: fprintf( fout, "%s=%s\n", path.c_str(), normalizeFloatingPointStr(value.asDouble()).c_str() ); 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 ) { static char buffer[16]; #if defined(_MSC_VER) && defined(__STDC_SECURE_LIB__) sprintf_s( buffer, sizeof(buffer), "[%d]", index ); #else snprintf( buffer, sizeof(buffer), "[%d]", index ); #endif printValueTree( fout, value[index], path + buffer ); } } break; case Json::objectValue: { fprintf( fout, "%s={}\n", path.c_str() ); Json::Value::Members members( value.getMemberNames() ); std::sort( members.begin(), members.end() ); std::string suffix = *(path.end()-1) == '.' ? "" : "."; for ( Json::Value::Members::iterator it = members.begin(); it != members.end(); ++it ) { const std::string &name = *it; printValueTree( fout, value[name], path + suffix + name ); } } break; default: break; } }
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=%ld\n", path.c_str(), value.asInt() ); break; case Json::uintValue: fprintf( fout, "%s=%lu\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 ) { static char buffer[16]; sprintf( buffer, "[%d]", index ); printValueTree( fout, value[index], path + buffer ); } } break; case Json::objectValue: { fprintf( fout, "%s={}\n", path.c_str() ); Json::Value::Members members( value.getMemberNames() ); std::sort( members.begin(), members.end() ); std::string suffix = *(path.end()-1) == '.' ? "" : "."; for ( Json::Value::Members::iterator it = members.begin(); it != members.end(); ++it ) { const std::string &name = *it; printValueTree( fout, value[name], path + suffix + name ); } } break; default: break; } }
static int parseAndSaveValueTree(const JSONCPP_STRING& input, const JSONCPP_STRING& actual, const JSONCPP_STRING& kind, const Json::Features& features, bool parseOnly, Json::Value* root) { Json::Reader reader(features); bool parsingSuccessful = reader.parse(input.data(), input.data() + input.size(), *root); if (!parsingSuccessful) { printf("Failed to parse %s file: \n%s\n", kind.c_str(), reader.getFormattedErrorMessages().c_str()); return 1; } if (!parseOnly) { 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 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; }
int ForTheRecordJSONRPC(const std::string& command, const std::string& arguments, Json::Value& json_response) { std::string response; int retval = E_FAILED; retval = ForTheRecordRPC(command, arguments, response); if (retval == CURLE_OK) { #ifdef DEBUG // Print only the first 512 bytes, otherwise XBMC will crash... XBMC->Log(LOG_DEBUG, "Response: %s\n", response.substr(0,512).c_str()); #endif if (response.length() != 0) { Json::Reader reader; bool parsingSuccessful = reader.parse(response, json_response); if ( !parsingSuccessful ) { XBMC->Log(LOG_DEBUG, "Failed to parse %s: \n%s\n", response.c_str(), reader.getFormatedErrorMessages().c_str() ); return E_FAILED; } } else { XBMC->Log(LOG_DEBUG, "Empty response"); return E_EMPTYRESPONSE; } #ifdef DEBUG printValueTree(stdout, json_response); #endif } return retval; }