6#include <QtCore/QObject>
7#include <QtCore/QUtf8StringView>
8#include <QtCore/QVarLengthArray>
12QString jsonValueTypeToString(QJsonValue::Type type)
16 QJsonValue::Type type;
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"},
26 for (
const TypeToString& entry : typeToStringMap) {
27 if (type == entry.type) {
32 return QObject::tr(
"Unknown type: %1").arg(type);
35bool validateMissingKeys(
const QStringList& missingKeys, QString& errorString)
37 if (missingKeys.isEmpty()) {
41 QObject::tr(
"The following required keys are missing: %1").arg(missingKeys.join(QStringLiteral(
", ")));
45bool valueTypeMatches(QJsonValue::Type actual, QJsonValue::Type expected)
48 return expected == QJsonValue::Undefined || actual == expected ||
49 (actual == QJsonValue::Double && expected == QJsonValue::Null);
52bool validateValueType(
const QString& key, QJsonValue::Type actual, QJsonValue::Type expected, QString& errorString)
54 if (valueTypeMatches(actual, expected)) {
57 errorString = QObject::tr(
"Incorrect value type - key:type:expected %1:%2:%3")
58 .arg(key, jsonValueTypeToString(actual), jsonValueTypeToString(expected));
62using PresentKeyTypes = QVarLengthArray<std::optional<QJsonValue::Type>, 32>;
63using KnownJsonKeys = QVarLengthArray<bool, 32>;
67QJsonObject::const_iterator findKey(
const QJsonObject& jsonObject,
const char* key)
69 const QUtf8StringView utf8Key(key);
71 std::all_of(utf8Key.cbegin(), utf8Key.cend(), [](
char c) { return static_cast<uchar>(c) < 0x80; });
73 return jsonObject.constFind(QLatin1StringView(utf8Key.data(), utf8Key.size()));
75 return jsonObject.constFind(utf8Key.toString());
80PresentKeyTypes findPresentKeyTypes(
const QJsonObject& jsonObject,
const QList<JsonParsing::KeyValidateInfo>& keyInfo,
81 KnownJsonKeys* knownJsonKeys =
nullptr)
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()) {
89 presentTypes[i] = value->type();
91 (*knownJsonKeys)[value - jsonObject.constBegin()] =
true;
97bool validatePresentKeys(
const QList<JsonParsing::KeyValidateInfo>& keyInfo,
const PresentKeyTypes& presentTypes,
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));
106 if (!validateMissingKeys(missingKeys, errorString)) {
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);
124 QStringList missingKeys;
125 for (
const QString& key : keys) {
126 if (!jsonObject.contains(key)) {
127 missingKeys.append(key);
130 return validateMissingKeys(missingKeys, errorString);
133bool validateKeyTypes(
const QJsonObject& jsonObject,
const QStringList& keys,
const QList<QJsonValue::Type>& types,
134 QString& errorString)
136 if (keys.count() != types.count()) {
138 QObject::tr(
"Mismatched key and type list sizes: keys=%1 types=%2").arg(keys.count()).arg(types.count());
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)) {
151bool validateKeys(
const QJsonObject& jsonObject,
const QList<KeyValidateInfo>& keyInfo, QString& errorString)
153 return validatePresentKeys(keyInfo, findPresentKeyTypes(jsonObject, keyInfo), errorString);
156bool validateKeysStrict(
const QJsonObject& jsonObject,
const QList<KeyValidateInfo>& keyInfo, QString& errorString)
158 KnownJsonKeys knownJsonKeys(jsonObject.size(),
false);
159 if (!validatePresentKeys(keyInfo, findPresentKeyTypes(jsonObject, keyInfo, &knownJsonKeys), errorString)) {
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());
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.