#pragma once #include #include // JSON-Schema includes #include #include #include #include #include #include class QJsonFactory { public: static int load(const QString& schema, const QString& config, QJsonObject& json) { // Load the schema and the config trees QJsonObject schemaTree = readSchema(schema); QJsonObject configTree = readConfig(config); // create the validator QJsonSchemaChecker schemaChecker; schemaChecker.setSchema(schemaTree); QStringList messages = schemaChecker.getMessages(); if (!schemaChecker.validate(configTree).first) { for (int i = 0; i < messages.size(); ++i) std::cout << messages[i].toStdString() << std::endl; std::cerr << "Validation failed for configuration file: " << config.toStdString() << std::endl; return -3; } json = configTree; return 0; } static QJsonObject readConfig(const QString& path) { QFile file(path); QJsonParseError error; if (!file.open(QIODevice::ReadOnly)) { throw std::runtime_error(QString("Configuration file not found: '" + path + "' (" + file.errorString() + ")").toStdString()); } //Allow Comments in Config QString config = QString(file.readAll()); config.remove(QRegularExpression("([^:]?\\/\\/.*)")); QJsonDocument doc = QJsonDocument::fromJson(config.toUtf8(), &error); file.close(); if (error.error != QJsonParseError::NoError) { // report to the user the failure and their locations in the document. int errorLine(0), errorColumn(0); for( int i=0, count=qMin( error.offset,config.size()); i