QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
JsonValidation.cc
Go to the documentation of this file.
1#include "JsonValidation.h"
2
3#include <QtCore/QObject>
4#include <QtCore/QSet>
5
6namespace {
7
8QString jsonValueTypeToString(QJsonValue::Type type)
9{
10 struct TypeToString
11 {
12 QJsonValue::Type type;
13 const char* string;
14 };
15
16 static constexpr const TypeToString typeToStringMap[] = {
17 {QJsonValue::Null, "NULL"}, {QJsonValue::Bool, "Bool"}, {QJsonValue::Double, "Double"},
18 {QJsonValue::String, "String"}, {QJsonValue::Array, "Array"}, {QJsonValue::Object, "Object"},
19 {QJsonValue::Undefined, "Undefined"},
20 };
21
22 for (const TypeToString& entry : typeToStringMap) {
23 if (type == entry.type) {
24 return entry.string;
25 }
26 }
27
28 return QObject::tr("Unknown type: %1").arg(type);
29}
30
31bool validateMissingKeys(const QStringList& missingKeys, QString& errorString)
32{
33 if (missingKeys.isEmpty()) {
34 return true;
35 }
37 QObject::tr("The following required keys are missing: %1").arg(missingKeys.join(QStringLiteral(", ")));
38 return false;
39}
40
41bool validateValueType(const QString& key, QJsonValue::Type actual, QJsonValue::Type expected, QString& errorString)
42{
43 // Undefined accepts any type; Null allows a double or a JSON null representing NaN.
44 if (expected == QJsonValue::Undefined || actual == expected ||
45 (actual == QJsonValue::Double && expected == QJsonValue::Null)) {
46 return true;
47 }
48 errorString = QObject::tr("Incorrect value type - key:type:expected %1:%2:%3")
49 .arg(key, jsonValueTypeToString(actual), jsonValueTypeToString(expected));
50 return false;
51}
52
53} // namespace
54
55namespace JsonParsing {
56
57bool validateRequiredKeys(const QJsonObject& jsonObject, const QStringList& keys, QString& errorString)
58{
59 QStringList missingKeys;
60 for (const QString& key : keys) {
61 if (!jsonObject.contains(key)) {
62 missingKeys.append(key);
63 }
64 }
65 return validateMissingKeys(missingKeys, errorString);
66}
67
68bool validateKeyTypes(const QJsonObject& jsonObject, const QStringList& keys, const QList<QJsonValue::Type>& types,
69 QString& errorString)
70{
71 if (keys.count() != types.count()) {
73 QObject::tr("Mismatched key and type list sizes: keys=%1 types=%2").arg(keys.count()).arg(types.count());
74 return false;
75 }
76
77 for (qsizetype i = 0; i < types.size(); ++i) {
78 const auto value = jsonObject.constFind(keys[i]);
79 if (value != jsonObject.constEnd() && !validateValueType(keys[i], value->type(), types[i], errorString)) {
80 return false;
81 }
82 }
83 return true;
84}
85
86bool validateKeys(const QJsonObject& jsonObject, const QList<KeyValidateInfo>& keyInfo, QString& errorString)
87{
88 QStringList missingKeys;
89 for (const KeyValidateInfo& info : keyInfo) {
90 const QString key = QString::fromUtf8(info.key);
91 if (info.required && !jsonObject.contains(key)) {
92 missingKeys.append(key);
93 }
94 }
95 if (!validateMissingKeys(missingKeys, errorString)) {
96 return false;
97 }
98 for (const KeyValidateInfo& info : keyInfo) {
99 const QString key = QString::fromUtf8(info.key);
100 const auto value = jsonObject.constFind(key);
101 if (value != jsonObject.constEnd() && !validateValueType(key, value->type(), info.type, errorString)) {
102 return false;
103 }
104 }
105 return true;
106}
107
108bool validateKeysStrict(const QJsonObject& jsonObject, const QList<KeyValidateInfo>& keyInfo, QString& errorString)
109{
110 if (!validateKeys(jsonObject, keyInfo, errorString)) {
111 return false;
112 }
113
114 QSet<QString> expectedKeys;
115 expectedKeys.reserve(keyInfo.size());
116 for (const KeyValidateInfo& info : keyInfo) {
117 expectedKeys.insert(QString::fromUtf8(info.key));
118 }
119
120 for (auto it = jsonObject.constBegin(); it != jsonObject.constEnd(); ++it) {
121 if (!expectedKeys.contains(it.key())) {
122 errorString = QObject::tr("Unknown key: %1").arg(it.key());
123 return false;
124 }
125 }
126
127 return true;
128}
129
130} // namespace JsonParsing
QString errorString
std::array< MsgOutKey, 4 > keys
Definition UBXPlan.cc:13
bool validateKeyTypes(const QJsonObject &jsonObject, const QStringList &keys, const QList< QJsonValue::Type > &types, QString &errorString)
bool validateKeys(const QJsonObject &jsonObject, const QList< KeyValidateInfo > &keyInfo, QString &errorString)
Validates that all required keys are present and that listed keys have the expected type.
bool validateKeysStrict(const QJsonObject &jsonObject, const QList< KeyValidateInfo > &keyInfo, QString &errorString)
Validates keys like validateKeys but also rejects any keys not listed in keyInfo.
bool validateRequiredKeys(const QJsonObject &jsonObject, const QStringList &keys, QString &errorString)
Validates that all listed keys are present in the object.