diff --git a/sdrbase/CMakeLists.txt b/sdrbase/CMakeLists.txt index be734bbf1..df5ef4d1f 100644 --- a/sdrbase/CMakeLists.txt +++ b/sdrbase/CMakeLists.txt @@ -172,6 +172,7 @@ set(sdrbase_SOURCES webapi/webapiadapterinterface.cpp webapi/webapirequestmapper.cpp webapi/webapiserver.cpp + webapi/webapiutils.cpp websockets/wsspectrum.cpp @@ -333,6 +334,7 @@ set(sdrbase_HEADERS webapi/webapiadapterinterface.h webapi/webapirequestmapper.h webapi/webapiserver.h + webapi/webapiutils.h websockets/wsspectrum.h diff --git a/sdrbase/webapi/webapiutils.cpp b/sdrbase/webapi/webapiutils.cpp new file mode 100644 index 000000000..86df2e98d --- /dev/null +++ b/sdrbase/webapi/webapiutils.cpp @@ -0,0 +1,116 @@ +/////////////////////////////////////////////////////////////////////////////////// +// Copyright (C) 2020 Jon Beniston, M7RCE // +// Copyright (C) 2020 Edouard Griffiths, F4EXB // +// // +// This program is free software; you can redistribute it and/or modify // +// it under the terms of the GNU General Public License as published by // +// the Free Software Foundation as version 3 of the License, or // +// (at your option) any later version. // +// // +// This program is distributed in the hope that it will be useful, // +// but WITHOUT ANY WARRANTY; without even the implied warranty of // +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // +// GNU General Public License V3 for more details. // +// // +// You should have received a copy of the GNU General Public License // +// along with this program. If not, see . // +/////////////////////////////////////////////////////////////////////////////////// + +#include +#include + +#include "webapiutils.h" + +// Get integer value from within JSON object +bool WebAPIUtils::getObjectInt(const QJsonObject &json, const QString &key, int &value) +{ + if (json.contains(key)) + { + value = json[key].toInt(); + return true; + } + + return false; +} + +// Get string value from within JSON object +bool WebAPIUtils::getObjectString(const QJsonObject &json, const QString &key, QString &value) +{ + if (json.contains(key)) + { + value = json[key].toString(); + return true; + } + + return false; +} + +// Get a list of JSON objects from within JSON object +bool WebAPIUtils::getObjectObjects(const QJsonObject &json, const QString &key, QList &objects) +{ + bool processed = false; + + if (json.contains(key)) + { + if (json[key].isArray()) + { + QJsonArray a = json[key].toArray(); + + for (QJsonArray::const_iterator it = a.begin(); it != a.end(); it++) + { + if (it->isObject()) + { + objects.push_back(it->toObject()); + processed = true; + } + } + } + } + + return processed; +} + +// Get double value from within nested JSON object +bool WebAPIUtils::getSubObjectDouble(const QJsonObject &json, const QString &key, double &value) +{ + for (QJsonObject::const_iterator it = json.begin(); it != json.end(); it++) + { + QJsonValue jsonValue = it.value(); + + if (jsonValue.isObject()) + { + QJsonObject subObject = jsonValue.toObject(); + + if (subObject.contains(key)) + { + value = subObject[key].toDouble(); + return true; + } + } + } + + return false; +} + +// Set double value withing nested JSON object +bool WebAPIUtils::setSubObjectDouble(QJsonObject &json, const QString &key, double value) +{ + for (QJsonObject::iterator it = json.begin(); it != json.end(); it++) + { + QJsonValue jsonValue = it.value(); + + if (jsonValue.isObject()) + { + QJsonObject subObject = jsonValue.toObject(); + + if (subObject.contains(key)) + { + subObject[key] = value; + it.value() = subObject; + return true; + } + } + } + + return false; +} diff --git a/sdrbase/webapi/webapiutils.h b/sdrbase/webapi/webapiutils.h new file mode 100644 index 000000000..3a99167f9 --- /dev/null +++ b/sdrbase/webapi/webapiutils.h @@ -0,0 +1,37 @@ +/////////////////////////////////////////////////////////////////////////////////// +// Copyright (C) 2020 Jon Beniston, M7RCE // +// Copyright (C) 2020 Edouard Griffiths, F4EXB // +// // +// This program is free software; you can redistribute it and/or modify // +// it under the terms of the GNU General Public License as published by // +// the Free Software Foundation as version 3 of the License, or // +// (at your option) any later version. // +// // +// This program is distributed in the hope that it will be useful, // +// but WITHOUT ANY WARRANTY; without even the implied warranty of // +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // +// GNU General Public License V3 for more details. // +// // +// You should have received a copy of the GNU General Public License // +// along with this program. If not, see . // +/////////////////////////////////////////////////////////////////////////////////// + +#ifndef SDRBASE_WEBAPI_WEBAPIUTILS_H_ +#define SDRBASE_WEBAPI_WEBAPIUTILS_H_ + +#include +#include + +#include "export.h" + +class SDRBASE_API WebAPIUtils +{ +public: + static bool getObjectInt(const QJsonObject &json, const QString &key, int &value); + static bool getObjectString(const QJsonObject &json, const QString &key, QString &value); + static bool getObjectObjects(const QJsonObject &json, const QString &key, QList &objects); + static bool getSubObjectDouble(const QJsonObject &json, const QString &key, double &value); + static bool setSubObjectDouble(QJsonObject &json, const QString &key, double value); +}; + +#endif