7 #include <utils/jsonschema/QJsonSchemaChecker.h> 11 #include <QJsonObject> 12 #include <QJsonDocument> 13 #include <QRegularExpression> 19 static int load(
const QString& schema,
const QString& config, QJsonObject& json)
22 QJsonObject schemaTree = readSchema(schema);
23 QJsonObject configTree = readConfig(config);
31 if (!schemaChecker.
validate(configTree).first)
33 for (
int i = 0; i < messages.size(); ++i)
34 std::cout << messages[i].toStdString() << std::endl;
36 std::cerr <<
"Validation failed for configuration file: " << config.toStdString() << std::endl;
44 static QJsonObject readConfig(
const QString& path)
47 QJsonParseError error;
49 if (!file.open(QIODevice::ReadOnly))
51 throw std::runtime_error(QString(
"Configuration file not found: '" + path +
"' (" + file.errorString() +
")").toStdString());
55 QString config = QString(file.readAll());
56 config.remove(QRegularExpression(
"([^:]?\\/\\/.*)"));
58 QJsonDocument doc = QJsonDocument::fromJson(config.toUtf8(), &error);
61 if (error.error != QJsonParseError::NoError)
64 int errorLine(0), errorColumn(0);
66 for(
int i=0, count=qMin( error.offset,config.size()); i<count; ++i )
69 if(config.at(i) ==
'\n' )
76 throw std::runtime_error (
77 QString(
"Failed to parse configuration: " + error.errorString() +
" at Line: " + QString::number(errorLine) +
", Column: " + QString::number(errorColumn)).toStdString()
84 static QJsonObject readSchema(
const QString& path)
86 QFile schemaData(path);
87 QJsonParseError error;
89 if (!schemaData.open(QIODevice::ReadOnly))
91 throw std::runtime_error(QString(
"Schema not found: '" + path +
"' (" + schemaData.errorString() +
")").toStdString());
94 QByteArray schema = schemaData.readAll();
95 QJsonDocument doc = QJsonDocument::fromJson(schema, &error);
98 if (error.error != QJsonParseError::NoError)
101 int errorLine(0), errorColumn(0);
103 for(
int i=0, count=qMin( error.offset,schema.size()); i<count; ++i )
106 if(schema.at(i) ==
'\n' )
113 throw std::runtime_error(QString(
"ERROR: Json schema wrong: " + error.errorString() +
114 " at Line: " + QString::number(errorLine) +
115 ", Column: " + QString::number(errorColumn)).toStdString());
118 return resolveReferences(doc.object());
121 static QJsonObject resolveReferences(
const QJsonObject& schema)
125 for (QJsonObject::const_iterator i = schema.begin(); i != schema.end(); ++i)
127 QString attribute = i.key();
128 const QJsonValue & attributeValue = *i;
130 if (attribute ==
"$ref" && attributeValue.isString())
134 result = readSchema(
":/" + attributeValue.toString());
136 catch (std::runtime_error& error)
138 throw std::runtime_error(error.what());
141 else if (attributeValue.isObject())
142 result.insert(attribute, resolveReferences(attributeValue.toObject()));
144 result.insert(attribute, attributeValue);
150 static bool writeJson(
const QString& filename, QJsonObject& jsonTree)
154 doc.setObject(jsonTree);
155 QByteArray configData = doc.toJson(QJsonDocument::Indented);
157 QFile configFile(filename);
158 if (!configFile.open(QFile::WriteOnly | QFile::Truncate))
161 configFile.write(configData);
163 QFile::FileError error = configFile.error();
164 if (error != QFile::NoError)
QPair< bool, bool > validate(const QJsonObject &value, bool ignoreRequired=false)
Validate a JSON structure.
Definition: QJsonSchemaChecker.cpp:29
const QStringList & getMessages() const
Definition: QJsonSchemaChecker.cpp:157
JsonSchemaChecker is a very basic implementation of json schema.
Definition: QJsonSchemaChecker.h:28
Definition: QJsonFactory.h:15
bool setSchema(const QJsonObject &schema)
Definition: QJsonSchemaChecker.cpp:20