JsonUtils & improvements (#476)

* add JsonUtils

* update

* repair

* update

* ident

* Schema correct msg other adjusts

* fix effDel, ExceptionLog, cleanup

* fix travis qt5.2

* not so funny

* use Qthread interrupt instead abort bool

* update services
This commit is contained in:
brindosch
2017-10-12 11:55:03 +02:00
committed by GitHub
parent 47641012ee
commit 838008568a
42 changed files with 940 additions and 701 deletions

View File

@@ -1,10 +1,64 @@
#pragma once
// qt includes
#include <QFile>
#include <QString>
#include <QByteArray>
// util includes
#include "Logger.h"
namespace FileUtils {
QString getBaseName( QString sourceFile);
QString getDirName( QString sourceFile);
///
/// @brief check if the file exists
/// @param[in] path The file path to check
/// @param[in] log The logger of the caller to print errors
/// @param[in] ignError Ignore errors during file read (no log output)
/// @return true on success else false
///
bool fileExists(const QString& path, Logger* log, bool ignError=false);
///
/// @brief read a file given by path.
/// @param[in] path The file path to read
/// @param[out] data The read data o success
/// @param[in] log The logger of the caller to print errors
/// @param[in] ignError Ignore errors during file read (no log output)
/// @return true on success else false
///
bool readFile(const QString& path, QString& data, Logger* log, bool ignError=false);
///
/// write a file given by path.
/// @param[in] path The file path to read
/// @param[in] data The data to write
/// @param[in] log The logger of the caller to print errors
/// @return true on success else false
///
bool writeFile(const QString& path, const QByteArray& data, Logger* log);
///
/// @brief delete a file by given path
/// @param[in] path The file path to delete
/// @param[in] log The logger of the caller to print errors
/// @return true on success else false
///
bool removeFile(const QString& path, Logger* log);
///
/// @brief Convert a path that may contain special placeholders
/// @param[in] path The path to convert
///
QString convertPath(const QString path);
///
/// @brief resolve the file error and print a message
/// @param[in] file The file which caused the error
/// @param[in] log The logger of the caller
///
void resolveFileError(const QFile& file, Logger* log);
};

View File

@@ -45,9 +45,10 @@ public:
/// Constructor
///
/// @param peerAddress provide the Address of the peer
/// @param noListener if true, this instance won't listen for hyperion push events
/// @param log The Logger class of the creator
/// @param noListener if true, this instance won't listen for hyperion push events
///
JsonProcessor(QString peerAddress, bool noListener = false);
JsonProcessor(QString peerAddress, Logger* log, bool noListener = false);
~JsonProcessor();
///
@@ -272,16 +273,4 @@ private:
/// @param error String describing the error
///
void sendErrorReply(const QString & error, const QString &command="", const int tan=0);
///
/// Check if a JSON messag is valid according to a given JSON schema
///
/// @param message JSON message which need to be checked
/// @param schemaResource Qt Resource identifier with the JSON schema
/// @param errors Output error message
/// @param ignoreRequired ignore the required value in JSON schema
///
/// @return true if message conforms the given JSON schema
///
bool checkJson(const QJsonObject & message, const QString &schemaResource, QString & errors, bool ignoreRequired = false);
};

65
include/utils/JsonUtils.h Normal file
View File

@@ -0,0 +1,65 @@
#pragma once
#include <utils/FileUtils.h>
#include <QJsonObject>
#include <utils/Logger.h>
namespace JsonUtils{
///
/// @brief read a json file and get the parsed result on success
/// @param[in] path The file path to read
/// @param[out] obj Returns the parsed QJsonObject
/// @param[in] log The logger of the caller to print errors
/// @param[in] ignError Ignore errors during file read (no log output)
/// @return true on success else false
///
bool readFile(const QString& path, QJsonObject& obj, Logger* log, bool ignError=false);
///
/// @brief read a schema file and resolve $refs
/// @param[in] path The file path to read
/// @param[out] obj Returns the parsed QJsonObject
/// @param[in] log The logger of the caller to print errors
/// @return true on success else false
///
bool readSchema(const QString& path, QJsonObject& obj, Logger* log);
///
/// @brief parse a json QString and get the result on success
/// @param[in] path The file path/name just used for log messages
/// @param[in] data Data to parse
/// @param[out] obj Retuns the parsed QJsonObject
/// @param[in] log The logger of the caller to print errors
/// @return true on success else false
///
bool parse(const QString& path, const QString& data, QJsonObject& obj, Logger* log);
///
/// @brief Validate json data against a schema
/// @param[in] file The path/name of json file just used for log messages
/// @param[in] json The json data
/// @param[in] schemaP The schema path
/// @param[in] log The logger of the caller to print errors
/// @return true on success else false
///
bool validate(const QString& file, const QJsonObject& json, const QString& schemaPath, Logger* log);
///
/// @brief Write json data to file
/// @param[in] filenameThe file path to write
/// @param[in] json The json data to write
/// @param[in] log The logger of the caller to print errors
/// @return true on success else false
///
bool write(const QString& filename, const QJsonObject& json, Logger* log);
///
/// @brief resolve schema $ref attribute
/// @param[in] schema the schema to iterate
/// @param[out] obj the resolved object
/// @param[in] log The logger of the caller to print errors
/// @return true on success else false
///
bool resolveRefs(const QJsonObject& schema, QJsonObject& obj, Logger* log);
};

View File

@@ -25,7 +25,7 @@ public:
// create the validator
QJsonSchemaChecker schemaChecker;
schemaChecker.setSchema(schemaTree);
QStringList messages = schemaChecker.getMessages();
if (!schemaChecker.validate(configTree).first)
@@ -40,7 +40,7 @@ public:
json = configTree;
return 0;
}
static QJsonObject readConfig(const QString& path)
{
QFile file(path);
@@ -54,7 +54,7 @@ public:
//Allow Comments in Config
QString config = QString(file.readAll());
config.remove(QRegularExpression("([^:]?\\/\\/.*)"));
QJsonDocument doc = QJsonDocument::fromJson(config.toUtf8(), &error);
file.close();
@@ -94,7 +94,7 @@ public:
QByteArray schema = schemaData.readAll();
QJsonDocument doc = QJsonDocument::fromJson(schema, &error);
schemaData.close();
if (error.error != QJsonParseError::NoError)
{
// report to the user the failure and their locations in the document.

View File

@@ -22,6 +22,8 @@
/// - addtionalProperties
/// - minItems
/// - maxItems
/// - minLength
/// - maxLength
class QJsonSchemaChecker
{
@@ -39,7 +41,7 @@ public:
/// @brief Validate a JSON structure
/// @param value The JSON value to check
/// @param ignoreRequired Ignore the "required" keyword in hyperion schema. Default is false
/// @return The first boolean is true when the arguments is valid according to the schema. The second is true when the schema contains no errors
/// @return The first boolean is true when the arguments is valid according to the schema. The second is true when the schema contains no errors
/// @return TODO: Check the Schema in SetSchema() function and remove the QPair result
///
QPair<bool, bool> validate(const QJsonObject & value, bool ignoreRequired = false);
@@ -84,7 +86,7 @@ private:
/// @param[in] schema The specified type (as json-value)
///
void checkType(const QJsonValue & value, const QJsonValue & schema, const QJsonValue & defaultValue);
///
/// Checks is required properties of an json-object exist and if all properties are of the
/// correct format. If this is not the case _error is set to true and an error-message is added
@@ -127,7 +129,7 @@ private:
/// Checks if the given value is hugher than the specified value. If this is the
/// case _error is set to true and an error-message is added to the message-queue.
///
/// @param value The given value
/// @param value The given value
/// @param schema The minimum size specification (as json-value)
///
void checkMinLength(const QJsonValue & value, const QJsonValue & schema, const QJsonValue & defaultValue);
@@ -136,7 +138,7 @@ private:
/// Checks if the given value is smaller than the specified value. If this is the
/// case _error is set to true and an error-message is added to the message-queue.
///
/// @param value The given value
/// @param value The given value
/// @param schema The maximum size specification (as json-value)
///
void checkMaxLength(const QJsonValue & value, const QJsonValue & schema, const QJsonValue & defaultValue);