rework of unit

master
Chris Punches 2017-06-20 22:53:47 -04:00
parent 64efd0898a
commit f5c8894670
2 changed files with 19 additions and 27 deletions

View File

@ -12,7 +12,7 @@
class JSON_Loader class JSON_Loader
{ {
private: protected:
Json::Value json_root; Json::Value json_root;
bool populated; bool populated;

View File

@ -5,51 +5,43 @@
Unit::Unit() {} Unit::Unit() {}
// where the serialized json is broken down into object members
int Unit::load_root(Json::Value loader_root) int Unit::load_root(Json::Value loader_root)
{ {
this->name = loader_root.get("name", "?").asString(); this->name = loader_root.get("name", "?").asString();
this->target = loader_root.get("target", "?").asString(); this->target = loader_root.get("target", "?").asString();
this->output = loader_root.get("output", "?").asString(); this->output = loader_root.get("output", "?").asString();
this->rectifier = loader_root.get("rectifier", "?").asString(); this->rectifier = loader_root.get("rectifier", "?").asString();
this->active = loader_root.get("active", "?").asString(); this->active = loader_root.get("active", "?").asString();
this->required = loader_root.get("required", "?").asString(); this->required = loader_root.get("required", "?").asString();
this->rectify = loader_root.get("rectify", "?").asString(); this->rectify = loader_root.get("rectify", "?").asString();
return EXIT_SUCCESS; this->populated = true;
return 0;
} }
// TODO CHANGE HOW THIS WORKS // populates from a string json object
int Unit::load_string(std::string json_val) int Unit::load_string(std::string json_val)
{ {
// reads from a string into a Json::Value type. // serialize
Json::Reader json_reader; this->load_json_string( json_val, true );
// the deserialized json type to contain what's read by the reader // deserialize
Json::Value serialized; this->load_root( this->json_root );
// create the ifstream file handle for the parser method to consume
std::ifstream json_file_ifstream( json_val.c_str(), std::ifstream::binary );
// use the reader to parse the ifstream to the local property
bool parsingSuccessful = json_reader.parse( json_file_ifstream, serialized );
if (! parsingSuccessful )
{
std::cerr << "Failed to parse adhoc JSON value." << std::endl << json_val << std::endl << std::endl << json_reader.getFormattedErrorMessages();
throw JSON_Loader_InvalidJSON();
} else {
// if in verbose mode, give the user an "it worked" message
if ( verbose )
{
std::cout << "Successfully parsed JSON string with " << this->json_root.size() << " elements. Value:" << std::endl;
std::cout << json_val << std::endl << std::endl;
}
}
// exit successfully // exit successfully
this->populated = true; this->populated = true;
}
return 0;
}
// getters for Unit type. // getters for Unit type.
std::string Unit::get_name() std::string Unit::get_name()