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 <algorithm>
4#include <optional>
5
6#include <QtCore/QObject>
7#include <QtCore/QUtf8StringView>
8#include <QtCore/QVarLengthArray>
9
10namespace {
11
12QString jsonValueTypeToString(QJsonValue::Type type)
13{
14 struct TypeToString
15 {
16 QJsonValue::Type type;
17 const char* string;
18 };
19
20 static constexpr const TypeToString typeToStringMap[] = {
21 {QJsonValue::Null, "NULL"}, {QJsonValue::Bool, "Bool"}, {QJsonValue::Double, "Double"},
22 {QJsonValue::String, "String"}, {QJsonValue::Array, "Array"}, {QJsonValue::Object, "Object"},
23 {QJsonValue::Undefined, "Undefined"},
24 };
25
26 for (const TypeToString& entry : typeToStringMap) {
27 if (type == entry.type) {
28 return entry.string;
29 }
30 }
31
32 return QObject::tr("Unknown type: %1").arg(type);
33}
34
35bool validateMissingKeys(const QStringList& missingKeys, QString& errorString)
36{
37 if (missingKeys.isEmpty()) {
38 return true;
39 }
40 errorString =
41 QObject::tr("The following required keys are missing: %1").arg(missingKeys.join(QStringLiteral(", ")));
42 return false;
43}
44
45bool valueTypeMatches(QJsonValue::Type actual, QJsonValue::Type expected)
46{
47 // Undefined accepts any type; Null allows a double or a JSON null representing NaN.
48 return expected == QJsonValue::Undefined || actual == expected ||
49 (actual == QJsonValue::Double && expected == QJsonValue::Null);
50}
51
52bool validateValueType(const QString& key, QJsonValue::Type actual, QJsonValue::Type expected, QString& errorString)
53{
54 if (valueTypeMatches(actual, expected)) {
55 return true;
56 }
57 errorString = QObject::tr("Incorrect value type - key:type:expected %1:%2:%3")
58 .arg(key, jsonValueTypeToString(actual), jsonValueTypeToString(expected));
59 return false;
60}
61
62using PresentKeyTypes = QVarLengthArray<std::optional<QJsonValue::Type>, 32>;
63using KnownJsonKeys = QVarLengthArray<bool, 32>;
64
67QJsonObject::const_iterator findKey(const QJsonObject& jsonObject, const char* key)
68{
69 const QUtf8StringView utf8Key(key);
70 const bool ascii =
71 std::all_of(utf8Key.cbegin(), utf8Key.cend(), [](char c) { return static_cast<uchar>(c) < 0x80; });
72 if (ascii) {
73 return jsonObject.constFind(QLatin1StringView(utf8Key.data(), utf8Key.size()));
74 }
75 return jsonObject.constFind(utf8Key.toString());
76}
77
80PresentKeyTypes findPresentKeyTypes(const QJsonObject& jsonObject, const QList<JsonParsing::KeyValidateInfo>& keyInfo,
81 KnownJsonKeys* knownJsonKeys = nullptr)
82{
83 PresentKeyTypes presentTypes(keyInfo.size(), std::nullopt);
84 for (qsizetype i = 0; i < keyInfo.size(); ++i) {
85 const auto value = findKey(jsonObject, keyInfo[i].key);
86 if (value == jsonObject.constEnd()) {
87 continue;
88 }
89 presentTypes[i] = value->type();
90 if (knownJsonKeys) {
91 (*knownJsonKeys)[value - jsonObject.constBegin()] = true;
92 }
93 }
94 return presentTypes;
95}
96
97bool validatePresentKeys(const QList<JsonParsing::KeyValidateInfo>& keyInfo, const PresentKeyTypes& presentTypes,
98 QString& errorString)
99{
100 QStringList missingKeys;
101 for (qsizetype i = 0; i < keyInfo.size(); ++i) {
102 if (keyInfo[i].required && !presentTypes[i]) {
103 missingKeys.append(QString::fromUtf8(keyInfo[i].key));
104 }
105 }
106 if (!validateMissingKeys(missingKeys, errorString)) {
107 return false;
108 }
109 for (qsizetype i = 0; i < keyInfo.size(); ++i) {
110 const std::optional<QJsonValue::Type>& actual = presentTypes[i];
111 if (actual && !valueTypeMatches(*actual, keyInfo[i].type)) {
112 return validateValueType(QString::fromUtf8(keyInfo[i].key), *actual, keyInfo[i].type, errorString);
113 }
114 }
115 return true;
116}
117
118} // namespace
119
120namespace JsonParsing {
121
122bool validateRequiredKeys(const QJsonObject& jsonObject, const QStringList& keys, QString& errorString)
123{
124 QStringList missingKeys;
125 for (const QString& key : keys) {
126 if (!jsonObject.contains(key)) {
127 missingKeys.append(key);
128 }
129 }
130 return validateMissingKeys(missingKeys, errorString);
131}
132
133bool validateKeyTypes(const QJsonObject& jsonObject, const QStringList& keys, const QList<QJsonValue::Type>& types,
134 QString& errorString)
135{
136 if (keys.count() != types.count()) {
137 errorString =
138 QObject::tr("Mismatched key and type list sizes: keys=%1 types=%2").arg(keys.count()).arg(types.count());
139 return false;
140 }
141
142 for (qsizetype i = 0; i < types.size(); ++i) {
143 const auto value = jsonObject.constFind(keys[i]);
144 if (value != jsonObject.constEnd() && !validateValueType(keys[i], value->type(), types[i], errorString)) {
145 return false;
146 }
147 }
148 return true;
149}
150
151bool validateKeys(const QJsonObject& jsonObject, const QList<KeyValidateInfo>& keyInfo, QString& errorString)
152{
153 return validatePresentKeys(keyInfo, findPresentKeyTypes(jsonObject, keyInfo), errorString);
154}
155
156bool validateKeysStrict(const QJsonObject& jsonObject, const QList<KeyValidateInfo>& keyInfo, QString& errorString)
157{
158 KnownJsonKeys knownJsonKeys(jsonObject.size(), false);
159 if (!validatePresentKeys(keyInfo, findPresentKeyTypes(jsonObject, keyInfo, &knownJsonKeys), errorString)) {
160 return false;
161 }
162
163 const auto unknown = std::find(knownJsonKeys.cbegin(), knownJsonKeys.cend(), false);
164 if (unknown != knownJsonKeys.cend()) {
165 const auto value = jsonObject.constBegin() + (unknown - knownJsonKeys.cbegin());
166 errorString = QObject::tr("Unknown key: %1").arg(value.key());
167 return false;
168 }
169
170 return true;
171}
172
173} // namespace JsonParsing
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.