Commit 66c981d1 authored by Karel Slaný's avatar Karel Slaný

Added forgotten files.

parent 78ccf0ba
/*
* Copyright (C) 2014-2019 CZ.NIC
*
* 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, either 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 for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* In addition, as a special exception, the copyright holders give
* permission to link the code of portions of this program with the
* OpenSSL library under certain conditions as described in each
* individual source file, and distribute linked combinations including
* the two.
*/
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonParseError>
#include <QJsonValue>
#include "src/datovka_shared/json/helper.h"
#include "src/datovka_shared/log/log.h"
bool Json::Helper::readRootObject(const QByteArray &json, QJsonObject &jsonObj)
{
QJsonDocument jsonDoc;
{
QJsonParseError parseErr;
jsonDoc = QJsonDocument::fromJson(json, &parseErr);
if (jsonDoc.isNull()) {
logErrorNL("Error parsing JSON: %s",
parseErr.errorString().toUtf8().constData());
return false;
}
}
if (!jsonDoc.isObject()) {
logErrorNL("%s", "JSON document contains no object.");
return false;
}
QJsonObject jsonTmpObj(jsonDoc.object());
if (jsonTmpObj.isEmpty()) {
logErrorNL("%s", "JSON object is empty.");
return false;
}
jsonObj = jsonTmpObj;
return true;
}
bool Json::Helper::readValue(const QJsonObject &jsonObj, const QString &key,
QJsonValue &jsonVal)
{
if (jsonObj.isEmpty() || key.isEmpty()) {
logErrorNL("%s", "JSON object or sought key is empty.");
return false;
}
jsonVal = jsonObj.value(key);
if (jsonVal.isUndefined()) {
logErrorNL("Missing key '%s' in JSON object.",
key.toUtf8().constData());
return false;
}
return true;
}
bool Json::Helper::readBool(const QJsonObject &jsonObj, const QString &key,
bool &val, bool acceptNull)
{
QJsonValue jsonVal;
if (!readValue(jsonObj, key, jsonVal)) {
return false;
}
if (jsonVal.isNull()) {
val = false; /* Null value. */
return acceptNull;
}
if (!jsonVal.isBool()) {
logErrorNL("value related to key '%s' is not a boolean value.",
key.toUtf8().constData());
return false;
}
val = jsonVal.toBool();
return true;
}
bool Json::Helper::readInt(const QJsonObject &jsonObj, const QString &key,
int &val, bool acceptNull)
{
QJsonValue jsonVal;
if (!readValue(jsonObj, key, jsonVal)) {
return false;
}
if (jsonVal.isNull()) {
val = 0; /* Null value. */
return acceptNull;
}
int readVal = jsonVal.toInt(-1);
if (readVal == -1) {
logErrorNL("Value related to key '%s' is not an integer.",
key.toUtf8().constData());
return false;
}
val = readVal;
return true;
}
bool Json::Helper::readString(const QJsonObject &jsonObj, const QString &key,
QString &val, bool acceptNull)
{
QJsonValue jsonVal;
if (!readValue(jsonObj, key, jsonVal)) {
return false;
}
if (jsonVal.isNull()) {
val = QString(); /* Null string. */
return acceptNull;
}
if (!jsonVal.isString()) {
logErrorNL("Value related to key '%s' is not a string.",
key.toUtf8().constData());
return false;
}
val = jsonVal.toString();
return true;
}
bool Json::Helper::readArray(const QJsonObject &jsonObj, const QString &key,
QJsonArray &arr, bool acceptNull)
{
QJsonValue jsonVal;
if (!readValue(jsonObj, key, jsonVal)) {
return false;
}
if (jsonVal.isNull()) {
arr = QJsonArray(); /* Null array. */
return acceptNull;
}
if (!jsonVal.isArray()) {
logErrorNL("Value related to key '%s' is not an array.",
key.toUtf8().constData());
return false;
}
arr = jsonVal.toArray();
return true;
}
bool Json::Helper::readStringList(const QJsonObject &jsonObj,
const QString &key, QStringList &val, bool acceptNull)
{
QJsonArray jsonArr;
if (!readArray(jsonObj, key, jsonArr, acceptNull)) {
return false;
}
QStringList tmpList;
foreach (const QJsonValue &jsonVal, jsonArr) {
if (jsonVal.isNull()) {
logErrorNL("%s", "Found null value in array.");
return false;
}
if (!jsonVal.isString()) {
logErrorNL("%s", "Found non-string value in array.");
return false;
}
tmpList.append(jsonVal.toString());
}
val = tmpList;
return true;
}
QString Json::Helper::toIndentedString(const QByteArray &json)
{
if (json.isEmpty()) {
return QString();
}
QJsonDocument jsonDoc;
{
QJsonParseError parseErr;
jsonDoc = QJsonDocument::fromJson(json, &parseErr);
if (jsonDoc.isNull()) {
logErrorNL("Error parsing JSON: %s",
parseErr.errorString().toUtf8().constData());
return QString();
}
}
return QString::fromUtf8(jsonDoc.toJson(QJsonDocument::Indented));
}
/*
* Copyright (C) 2014-2019 CZ.NIC
*
* 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, either 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 for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* In addition, as a special exception, the copyright holders give
* permission to link the code of portions of this program with the
* OpenSSL library under certain conditions as described in each
* individual source file, and distribute linked combinations including
* the two.
*/
#pragma once
#include <QByteArray>
#include <QJsonObject>
#include <QString>
#include <QStringList>
namespace Json {
/*!
* @brief JSON conversion helper functions.
*/
namespace Helper {
/*!
* @brief Reads a JSON object which comprises the document.
*
* @param[in] json JSON data.
* @param[out] jsonObj JSON object from the document.
* @return True on success, false else.
*/
bool readRootObject(const QByteArray &json,
QJsonObject &jsonObj);
/*!
* @brief Searches for a value on JSON object.
*
* @param[in] jsonObject Object to search in.
* @param[in] key Key to search for.
* @param[out] jsonVal Found value.
* @return True if key found, false else.
*/
bool readValue(const QJsonObject &jsonObj, const QString &key,
QJsonValue &jsonVal);
/*!
* @brief Reads a boolean value from supplied JSON object.
*
* @param[in] jsonObj JSON object.
* @param[in] key Key identifying the string.
* @param[out] val Value to be stored.
* @param[in] acceptNull True if null value should also be accepted.
* @return True on success, false else.
*/
bool readBool(const QJsonObject &jsonObj, const QString &key,
bool &val, bool acceptNull);
/*!
* @brief Reads an integer value from supplied JSON object.
*
* @param[in] jsonObj JSON object.
* @param[in] key Key identifying the string.
* @param[out] val Value to be stored.
* @param[in] acceptNull True if null value should also be accepted.
* @return True on success, false else.
*/
bool readInt(const QJsonObject &jsonObj, const QString &key,
int &val, bool acceptNull);
/*!
* @brief Reads a string value from supplied JSON object.
*
* @param[in] jsonObj JSON object.
* @param[in] key Key identifying the string.
* @param[out] val Value to be stored.
* @param[in] acceptNull True if null value should also be accepted.
* @return True on success, false else.
*/
bool readString(const QJsonObject &jsonObj, const QString &key,
QString &val, bool acceptNull);
/*!
* @brief Reads an array value from supplied JSON object.
*
* @param[in] jsonObj JSON object.
* @param[in] key Key identifying the string.
* @param[out] arr Array to be stored.
* @param[in] acceptNull True if null value should also be accepted.
* @return True on success, false else.
*/
bool readArray(const QJsonObject &jsonObj, const QString &key,
QJsonArray &arr, bool acceptNull);
/*!
* @brief Reads a string list from supplied JSON object.
*
* @param[in] jsonObj JSON object.
* @param[in] key Key identifying the string list.
* @param[out] val Values to be stored.
* @param[in] acceptNull True if null value should also be accepted.
* @return True on success, false else.
*/
bool readStringList(const QJsonObject &jsonObj,
const QString &key, QStringList &val, bool acceptNull);
/*!
* @brief Converts JSON document to indented string.
*
* @param[in] json JSON data.
* @retunr Non-empty indented string if JSON data could be read.
*/
QString toIndentedString(const QByteArray &json);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment