diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 2795eb2..0612696 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -15,6 +15,8 @@ + + @@ -34,7 +36,7 @@ - + @@ -50,8 +52,8 @@ - - + + @@ -71,6 +73,28 @@ + + + + + + + + + + + + + + + + + + + + + + @@ -84,86 +108,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -193,7 +137,6 @@ - @@ -943,14 +887,6 @@ - - - - - - - - @@ -1036,14 +972,6 @@ - - - - - - - - @@ -1060,17 +988,6 @@ - - - - - - - - - - - @@ -1133,10 +1050,37 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + - - + + @@ -1145,8 +1089,8 @@ - - + + diff --git a/src/loaders/Conf.cpp b/src/loaders/Conf.cpp index 5535a13..bfcaeae 100644 --- a/src/loaders/Conf.cpp +++ b/src/loaders/Conf.cpp @@ -1,9 +1,11 @@ #include "Conf.h" -Conf::Conf( std::string filename ): JSON_Loader( filename ) +Conf::Conf( std::string filename ): JSON_Loader() { - this->plan_path = this->as_serialized()["plan_path"].asString(); - this->units_path = this->as_serialized()["units_path"].asString(); + this->load_json_file( filename, true ); + + this->plan_path = this->get_key("plan_path", true, false).asString(); + this->units_path = this->get_key("units_path", true, false).asString(); }; std::string Conf::get_plan_path() diff --git a/src/loaders/JSON_Loader.cpp b/src/loaders/JSON_Loader.cpp index 7b0e6db..4f644d7 100644 --- a/src/loaders/JSON_Loader.cpp +++ b/src/loaders/JSON_Loader.cpp @@ -71,7 +71,7 @@ int JSON_Loader::load_json_string( std::string input, bool verbose ) return 1; } else { // if in verbose mode, give the user an "it worked" message - if (verbose) + if ( verbose ) { std::cout << "Successfully parsed JSON string with " << this->json_root.size() << " elements. Value:" << std::endl; std::cout << input << std::endl; @@ -92,4 +92,35 @@ Json::Value JSON_Loader::as_serialized() std::string JSON_Loader::as_string() { return this->json_root.asString(); +} + +// returns the string representation of the value of a key +// the Jason::Value object to assign the fetched value to +// verbosity flag +// exit or not on failure +int JSON_Loader::get_key( Json::Value &input, std::string key, bool verbose, bool safety) +{ + if ( this->json_root.isMember( key ) ) + { + // key was found so return it + input = this->json_root[ key ]; + return 0; + } else { + // key was not found + if ( verbose ) + { + // verbose mode tells the user what key we were looking for. + std::cerr << "Failed to find key '" << key << "'." << std::endl; + return 1; + } + + if ( ! safety ) + { + // if we're not ignoring fatal errors + std::cout << "Exiting." << std::endl; + // exit code for failure + // this should probably be a 'throw'.... + exit(1); + } + } } \ No newline at end of file diff --git a/src/loaders/JSON_Loader.h b/src/loaders/JSON_Loader.h index 02fb509..09a8079 100644 --- a/src/loaders/JSON_Loader.h +++ b/src/loaders/JSON_Loader.h @@ -28,5 +28,9 @@ public: // return as a JSONCPP serialized object Json::Value as_serialized(); std::string as_string(); + + // safely handle key retrieval (if we want it to be safe) + // next iter should be: + int JSON_Loader::get_key( Json::Value &input, std::string key, bool verbose, bool safety); }; #endif //FTESTS_JLOADER_H