mirror of
https://github.com/hyperion-project/hyperion.ng.git
synced 2025-03-01 10:33:28 +00:00
First working version with some test executables
This commit is contained in:
66
include/utils/jsonschema/JsonFactory.h
Normal file
66
include/utils/jsonschema/JsonFactory.h
Normal file
@@ -0,0 +1,66 @@
|
||||
#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;
|
||||
}
|
||||
};
|
75
include/utils/jsonschema/JsonSchemaChecker.h
Normal file
75
include/utils/jsonschema/JsonSchemaChecker.h
Normal file
@@ -0,0 +1,75 @@
|
||||
// Copyright (c) 2012 TNO, The Netherlands.
|
||||
//
|
||||
// This file contains information proprietary to TNO.
|
||||
//
|
||||
// Any disclosure or use of this information or any reproduction of this document or any part thereof for
|
||||
// other than the specified purpose for which it is intended is expressly prohibited except as TNO may
|
||||
// otherwise agree to in writing.
|
||||
|
||||
#pragma once
|
||||
|
||||
// stl includes
|
||||
#include <string>
|
||||
#include <list>
|
||||
|
||||
// jsoncpp includes
|
||||
#include <json/json.h>
|
||||
|
||||
/**
|
||||
* JsonSchemaChecker is a very basic implementation of json schema.
|
||||
* The json schema definition draft can be found at
|
||||
* http://tools.ietf.org/html/draft-zyp-json-schema-03
|
||||
*
|
||||
* The following keywords are supported:
|
||||
* - type
|
||||
* - required
|
||||
* - properties
|
||||
* - items
|
||||
* - enum
|
||||
* - minimum
|
||||
* - maximum
|
||||
*/
|
||||
class JsonSchemaChecker
|
||||
{
|
||||
public:
|
||||
JsonSchemaChecker();
|
||||
virtual ~JsonSchemaChecker();
|
||||
|
||||
bool setSchema(const Json::Value & schema);
|
||||
|
||||
bool validate(const Json::Value & value);
|
||||
|
||||
const std::list<std::string> & getMessages() const;
|
||||
|
||||
private:
|
||||
void collectReferences(const Json::Value & schema);
|
||||
|
||||
void validate(const Json::Value &value, const Json::Value & schema);
|
||||
|
||||
void setMessage(const std::string & message);
|
||||
|
||||
void collectDependencies(const Json::Value & value, const Json::Value &schema);
|
||||
|
||||
private:
|
||||
// attribute check functions
|
||||
void checkType(const Json::Value & value, const Json::Value & schema);
|
||||
void checkProperties(const Json::Value & value, const Json::Value & schema);
|
||||
void checkAdditionalProperties(const Json::Value & value, const Json::Value & schema, const Json::Value::Members & ignoredProperties);
|
||||
void checkDependencies(const Json::Value & value, const Json::Value & schemaLink);
|
||||
void checkMinimum(const Json::Value & value, const Json::Value & schema);
|
||||
void checkMaximum(const Json::Value & value, const Json::Value & schema);
|
||||
void checkItems(const Json::Value & value, const Json::Value & schema);
|
||||
void checkMinItems(const Json::Value & value, const Json::Value & schema);
|
||||
void checkMaxItems(const Json::Value & value, const Json::Value & schema);
|
||||
void checkUniqueItems(const Json::Value & value, const Json::Value & schema);
|
||||
void checkEnum(const Json::Value & value, const Json::Value & schema);
|
||||
|
||||
private:
|
||||
Json::Value _schema;
|
||||
|
||||
std::list<std::string> _currentPath;
|
||||
std::list<std::string> _messages;
|
||||
bool _error;
|
||||
|
||||
std::map<std::string, const Json::Value *> _references; // ref 2 value
|
||||
};
|
Reference in New Issue
Block a user