File indexing completed on 2024-05-05 04:42:47

0001 /*
0002     SPDX-FileCopyrightText: 2023 Volker Krause <vkrause@kde.org>
0003     SPDX-License-Identifier: LGPL-2.0-or-later
0004 */
0005 
0006 #include "jsonpointer_p.h"
0007 
0008 #include <QDebug>
0009 #include <QJsonObject>
0010 
0011 using namespace KPublicTransport;
0012 
0013 QJsonValue JsonPointer::evaluate(const QJsonValue &obj, QStringView jsonPointer)
0014 {
0015     if (jsonPointer.startsWith(QLatin1Char('/'))) {
0016         jsonPointer = jsonPointer.mid(1);
0017     }
0018     if (jsonPointer.isEmpty()) {
0019         return obj;
0020     }
0021 
0022     const auto idx = jsonPointer.indexOf(QLatin1Char('/'));
0023     const auto key = idx >= 0 ? jsonPointer.left(idx) : jsonPointer;
0024 
0025     if (obj.isObject()) {
0026         const auto value = obj.toObject().value(key);
0027         if (idx >= 0) {
0028             return JsonPointer::evaluate(value, jsonPointer.mid(idx + 1));
0029         }
0030         return value;
0031     }
0032     if (obj.isArray()) {
0033         qWarning() << "JSON Pointer array indexing not implemented yet!";
0034         return {};
0035     }
0036 
0037     qWarning() << "JSON Pointer expression applied to primitive value!" << jsonPointer;
0038     return {};
0039 }