hyperion.ng/include/utils/jsonschema/JsonFactory.h
Paulchen-Panther 68fd395670 JSON RPC Writer (configSet) (#175)
* Remove "endOfJson" Value

Deprecated value from Hypercon

* Remove "endOfJson" Value

Deprecated value from Hypercon

* Add writeJson function to JsonFactory

* ability to ignore required value in schema file

* Remove "endOfJson" Value

* Add handleConfigSetCommand function

* Add handleConfigSetCommand function

* Update JsonSchemas.qrc

* Update schema.json

* Update JsonSchemaChecker.cpp

* Add configSet command to Hyperion-remote

* Add setConfigFile function

* Add setConfigFile function

* Add schema-configset.json
2016-08-14 20:17:12 +02:00

75 lines
1.7 KiB
C++

#pragma once
#include <string>
#include <istream>
#include <fstream>
#include <stdexcept>
#include <sstream>
// JSON-Schema includes
#include <utils/jsonschema/JsonSchemaChecker.h>
class JsonFactory
{
public:
static int load(const std::string& schema, const std::istream& config, Json::Value json);
static int load(const std::string& schema, const std::string& config, Json::Value& json)
{
// Load the schema and the config trees
Json::Value schemaTree = readJson(schema);
Json::Value configTree = readJson(config);
// create the validator
JsonSchemaChecker schemaChecker;
schemaChecker.setSchema(schemaTree);
bool valid = schemaChecker.validate(configTree);
for (const std::string& message : schemaChecker.getMessages())
{
std::cout << message << std::endl;
}
if (!valid)
{
std::cerr << "Validation failed for configuration file: " << config.c_str() << std::endl;
return -3;
}
json = configTree;
return 0;
}
static Json::Value readJson(const std::string& filename)
{
// Open the file input stream
std::ifstream ifs(filename.c_str());
return readJson(ifs);
}
static Json::Value readJson(std::istream& stream)
{
// will contains the root value after parsing.
Json::Value jsonTree;
Json::Reader reader;
if (! reader.parse(stream, jsonTree, false))
{
// report to the user the failure and their locations in the document.
std::stringstream sstream;
sstream << "Failed to parse configuration: " << reader.getFormattedErrorMessages().c_str();
throw std::runtime_error(sstream.str());
}
return jsonTree;
}
static void writeJson(const std::string& filename, Json::Value& jsonTree)
{
Json::StyledStreamWriter writer;
std::ofstream ofs(filename.c_str());
writer.write(ofs, jsonTree);
}
};