rex/src/loaders/JSON_Loader.cpp

126 lines
3.7 KiB
C++
Raw Normal View History

//
// 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 )
{
2017-06-09 04:09:42 +00:00
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)
{
2017-06-09 04:09:42 +00:00
std::cout << "Parsed '" << filename << "' with " << this->json_root.size() << " element(s)." << 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();
}
// 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);
}
}
}