reworked JLoader as JSON_Loader, will require rework of rest of obj model

This commit is contained in:
Chris Punches
2017-06-08 23:49:13 -04:00
parent 245ee4323a
commit 9fe2d4923a
37 changed files with 854 additions and 1034 deletions

View File

@@ -1,16 +1,9 @@
//
// Created by phanes on 4/22/17.
//
#include "Conf.h"
#include "JLoader.h"
#include <string>
Conf::Conf( std::string filename ): JLoader( filename )
Conf::Conf( std::string filename ): JSON_Loader( filename )
{
this->plan_path = this->get_root()["plan_path"].asString();
this->units_path = this->get_root()["units_path"].asString();
this->plan_path = this->as_serialized()["plan_path"].asString();
this->units_path = this->as_serialized()["units_path"].asString();
};
std::string Conf::get_plan_path()

View File

@@ -4,16 +4,15 @@
#ifndef FTESTS_CONF_H
#define FTESTS_CONF_H
#include "JLoader.h"
#include "JSON_Loader.h"
class Conf: public JLoader
class Conf: public JSON_Loader
{
private:
std::string plan_path;
std::string units_path;
public:
using JLoader::JLoader;
Conf( std::string filename );
std::string get_plan_path();
std::string get_units_path();

View File

@@ -1,34 +0,0 @@
//
// Created by phanes on 4/22/17.
//
#include "JLoader.h"
#include "helpers.h"
JLoader::JLoader( std::string filename )
{
Json::Value json_root;
Json::Reader reader;
if (! exists( filename ) )
{
std::cerr << "File '" << filename << "' does not exist.";
exit(1);
}
std::ifstream jfile( filename, std::ifstream::binary );
bool parsingSuccessful = reader.parse( jfile, this->json_root );
if (! parsingSuccessful )
{
std::cerr << "Failed to parse " << filename << ":\n\t" << reader.getFormattedErrorMessages();
std::exit( 1 );
} else {
std::cout << "Parsed " << filename << " with " << this->json_root.size() << " elements." << std::endl;
}
}
Json::Value JLoader::get_root()
{
return this->json_root;
}

View File

@@ -1,21 +0,0 @@
//
// Created by phanes on 4/22/17.
//
#ifndef FTESTS_JLOADER_H
#define FTESTS_JLOADER_H
#include "../json/json.h"
#include <iostream>
#include <fstream>
#include <cstdlib>
class JLoader
{
private:
Json::Value json_root;
public:
JLoader( std::string filename );
Json::Value get_root();
};
#endif //FTESTS_JLOADER_H

View File

@@ -0,0 +1,95 @@
//
// Created by phanes on 4/22/17.
//
#include "JSON_Loader.h"
#include "helpers.h"
JSON_Loader::JSON_Loader()
{
// empty
}
// loads json from a file into a deserializable type and sets to private member json_root
int JSON_Loader::load_json_file( std::string filename, bool verbose )
{
// reads from a file into a Json::Value type.
Json::Reader json_reader;
// the a deserialized json type to contain what's read by the reader
Json::Value json_root;
// first, check if the file exists
if (! exists( filename ) )
{
std::cerr << "File '" << filename << "' does not exist.";
// exit with failure signal
return 1;
}
// create the ifstream file handle
std::ifstream json_file_ifstream( filename, std::ifstream::binary );
// use the reader to parse the ifstream to the local property
bool parsingSuccessful = json_reader.parse( json_file_ifstream, this->json_root );
if (! parsingSuccessful )
{
std::cerr << "Failed to parse " << filename << ":\n\t" << json_reader.getFormattedErrorMessages();
// exit with failure signal
return 1;
} else {
// if in verbose mode, give the user an "it worked" message
if (verbose)
{
std::cout << "Parsed " << filename << " with " << this->json_root.size() << " elements." << std::endl;
}
}
// exit successfully
return 0;
}
// loads json from std::string into a serialized type and sets to private member json_root
int JSON_Loader::load_json_string( std::string input, bool verbose )
{
// reads from a string into a Json::Value type.
Json::Reader json_reader;
// the deserialized json type to contain what's read by the reader
Json::Value json_root;
// create the ifstream file handle
std::ifstream json_file_ifstream( input.c_str(), std::ifstream::binary );
// use the reader to parse the ifstream to the local property
bool parsingSuccessful = json_reader.parse( json_file_ifstream, this->json_root );
if (! parsingSuccessful )
{
std::cerr << "Failed to parse adhoc JSON value." << std::endl << input << std::endl << std::endl << json_reader.getFormattedErrorMessages();
// exit with failure signal
return 1;
} 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 << input << std::endl;
}
}
// exit successfully
return 0;
}
// returns the serialized representation of json_root
Json::Value JSON_Loader::as_serialized()
{
return this->json_root;
}
// returns the string representation of json_root
std::string JSON_Loader::as_string()
{
return this->json_root.asString();
}

31
src/loaders/JSON_Loader.h Normal file
View File

@@ -0,0 +1,31 @@
//
// Created by phanes on 4/22/17.
//
#ifndef FTESTS_JLOADER_H
#define FTESTS_JLOADER_H
#include "../json/json.h"
#include <iostream>
#include <fstream>
#include <cstdlib>
class JSON_Loader
{
private:
Json::Value json_root;
public:
// constructor
JSON_Loader();
// load from json file
int load_json_file( std::string filename, bool verbose );
// load from std::string json
int load_json_string( std::string input, bool verbose );
// return as a JSONCPP serialized object
Json::Value as_serialized();
std::string as_string();
};
#endif //FTESTS_JLOADER_H

View File

@@ -41,12 +41,12 @@ Task Plan::get_task(std::string provided_name)
return * returnable;
}
Plan::Plan( std::string filename ): JLoader( filename )
Plan::Plan( std::string filename ): JSON_Loader( filename )
/* Plan loads a file and deserializes the Unit JSON object to Task types as a vector member
* Plan { vector<Task> }
*/
{
Json::Value raw_tasks = this->get_root()["plan"];
Json::Value raw_tasks = this->as_serialized()["plan"];
for ( int index = 0; index < raw_tasks.size(); index++ )
{

View File

@@ -3,11 +3,11 @@
#include <string>
#include "../json/json.h"
#include "JLoader.h"
#include "JSON_Loader.h"
#include "Task.h"
class Plan: public JLoader
class Plan: public JSON_Loader
{
private:
std::vector<Task> tasks;

View File

@@ -1,17 +1,25 @@
#include "Suite.h"
Suite::Suite( std::string filename ): JLoader( filename )
Suite::Suite(): JSON_Loader() {}
Suite::Suite( std::string filename ): JSON_Loader( filename )
/* Suite loads a file and deserializes the Unit JSON object to Unit types as a vector member
* Suite { vector<Unit> }
*/
{
Json::Value raw_units = this->get_root()["units"];
load_file( filename );
};
int Suite::load_file(std::string filename): JSON_Loader( filename )
{
Json::Value raw_units = this->get_root()["units"];
for ( int index = 0; index < raw_units.size(); index++ )
{
this->units.push_back( Unit( raw_units[ index ] ) );
}
};
std::cout << raw_units.size() << std::endl;
return EXIT_SUCCESS;
}
Unit Suite::get_unit(std::string provided_name)
/*
@@ -38,6 +46,5 @@ Unit Suite::get_unit(std::string provided_name)
std::cerr << "Unit name \"" << provided_name << "\" was referenced but not defined!" << std::endl;
std::exit(1);
}
return * returnable;
}

View File

@@ -1,17 +1,12 @@
//
// Created by phanes on 4/22/17.
//
#ifndef FTESTS_UNITS_H
#define FTESTS_UNITS_H
#include <vector>
#include "../json/json.h"
#include "JLoader.h"
#include "JSON_Loader.h"
#include "Unit.h"
class Suite: public JLoader
class Suite: public JSON_Loader
{
private:
std::vector<Unit> units;
@@ -19,11 +14,11 @@ class Suite: public JLoader
public:
// constructor
Suite( std::string filename );
Suite();
int load_file( std::string filename );
// returns the unit type identified by name or null
Unit get_unit(std::string provided_name);
};
#endif //FTESTS_UNITS_H

View File

@@ -1,20 +1,41 @@
#include "Task.h"
Task::Task() {}
Task::Task( Json::Value loader_root )
{
this->load_root( loader_root );
}
int Task::load_root(Json::Value loader_root)
{
this->name = loader_root.get("name", "?").asString();
this->dependencies = loader_root.get("depends on", "");
this->has_succeeded = false;
}
std::string Task::get_name() { return this->name; }
bool Task::isDone() { return this->has_succeeded; }
void Task::finish() { this->has_succeeded = true; }
std::string Task::get_name()
{
return this->name;
}
bool Task::isDone()
{
return this->has_succeeded;
}
void Task::finish()
{
this->has_succeeded = true;
}
// returns Json::Value for dependencies
Json::Value Task::get_dependencies() { return this->dependencies;}
Json::Value Task::get_dependencies()
{
return this->dependencies;
}
Json::Value Task::set_dependencies()
{

View File

@@ -1,11 +1,9 @@
//
// Created by phanes on 4/30/17.
//
#ifndef FTESTS_TASK_H
#define FTESTS_TASK_H
#include <string>
#include "../json/json.h"
#include "Unit.h"
#include "Suite.h"
class Task
{
@@ -16,9 +14,15 @@ class Task
public:
Task( Json::Value loader_root );
Task();
int load_root( Json::Value loader_root );
std::string get_name();
Json::Value get_dependencies();
Json::Value set_dependencies();
// appends associated_unit as child member
bool isDone();
void finish();
};

View File

@@ -1,9 +1,16 @@
#include "Unit.h"
Unit::Unit() {}
Unit::Unit( Json::Value loader_root )
/*
* Constructor for Unit type. Receives a UnitHolder loader_root.
*/
{
this->load_root( loader_root );
}
int Unit::load_root(Json::Value loader_root)
{
this->name = loader_root.get("name", "?").asString();
this->target = loader_root.get("target", "?").asString();
@@ -12,8 +19,9 @@ Unit::Unit( Json::Value loader_root )
this->active = loader_root.get("active", "?").asString();
this->required = loader_root.get("required", "?").asString();
this->rectify = loader_root.get("rectify", "?").asString();
}
return EXIT_SUCCESS;
}
/*
* getters for Unit type.
*/

View File

@@ -19,7 +19,11 @@ private:
std::string rectify;
public:
Unit();
Unit( Json::Value loader_root );
int load_root( Json::Value loader_root );
std::string get_name();
std::string get_target();
std::string get_output();

View File

@@ -4,7 +4,7 @@
#ifndef FTESTS_LOADERS_H
#define FTESTS_LOADERS_H
#include "JLoader.h"
#include "JSON_Loader.h"
#include "Suite.h"
#include "Plan.h"
#include "Conf.h"